To check a value in a DataSet
use first get a DataTable
inside of it, then a DataRow
, then check one of the field values:
ds.Tables(0).Rows(0)("{field name}");
So to change the color based on some value:
If ds.Tables(0).Rows(0)("{field name}") = "Red" Then
textbox1.BackColor = Color.Red
End If
Some other comments:
- A
DataSet
may be a bit heavy for getting one value (unless you're binding to a control). You can just use ADO.NET objects and useExecuteScalar
) - It's safer to use parameters instead of concatenating SQL statements (prevents SQL Injection and errors from special characters)
- You can refactor your code to put the connection string in a single location rather than duplicating it across methods.