Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.80/5 (2 votes)
See more:
This is probably a silly and very easy question but how do you use a result from a query in a if statement. I am querying a table and if the result has a certain value I want it to carry out a function.
Code so far:
C#
private void ButtonLogin_Click(object sender, RoutedEventArgs e)
{
    string connectionSQL = @"server=xxxxxxxxxxx;user id=xxxxxxxxxxxx;password=xxxxxxxx;database=xxxxxxxx";
    MySqlConnection cs = new MySqlConnection(connectionSQL);
    cs.Open();
    
    DataSet ds = new DataSet();
    
    MySqlDataAdapter da = new MySqlDataAdapter("Select username, password, type from Admin", cs);
    
    MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);
    
    da.Fill(ds);
}

I want to do a if statement on values in ds in column type.
Posted
Updated 3-Apr-13 19:08pm
v3
Comments
[no name] 3-Apr-13 22:31pm    
You get your result, iterate through the result, check for your condition in the if statement, if the condition is met call your function.
Have you tried anything ?
Please post the code and indicate where exactly you want to this ?
Member 9611735 4-Apr-13 0:54am    
question updated
vijay__p 3-Apr-13 23:50pm    
Are you using traditional ADO.Net or entity framework ?
Shine Ashraf 4-Apr-13 1:22am    
Have you tried ds.tables[]["columnType"] ?

1 solution

Now you can use your DataSet ds.

if you use indexes like table 0 and row 0:
C#
string user = ds.Tables[0].Rows[0];
string pass = ds.Tables[0].Rows[1];


or you can use names of the tables and rows:
C#
string user = ds.Tables["MyTableName"].Rows["Username"];
string pass = ds.Tables["MyTableName"].Rows["Password"];


Good luck,
Edo

* Note that you missed closing the connection, [cs.Close();]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900