Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# MySQL query Help if
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:
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 3-Apr-13 16:31pm
Edited 3-Apr-13 20:08pm
v3
Comments
ThePhantomUpvoter at 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.
Tadit Dash at 3-Apr-13 23:33pm
   
Have you tried anything ?
Please post the code and indicate where exactly you want to this ?
Member 9611735 at 4-Apr-13 0:54am
   
question updated
patel_vijay at 3-Apr-13 23:50pm
   
Are you using traditional ADO.Net or entity framework ?
Shine Ashraf at 4-Apr-13 1:22am
   
Have you tried ds.tables[]["columnType"] ?

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Now you can use your DataSet ds.
 
if you use indexes like table 0 and row 0:
string user = ds.Tables[0].Rows[0];
string pass = ds.Tables[0].Rows[1];
 
or you can use names of the tables and rows:
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();]
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 406
1 Marcin Kozub 225
2 Sergey Alexandrovich Kryukov 205
3 Raul Iloc 170
4 Maciej Los 164
0 OriginalGriff 8,289
1 Sergey Alexandrovich Kryukov 7,407
2 DamithSL 5,624
3 Maciej Los 4,989
4 Manas Bhardwaj 4,986


Advertise | Privacy | Mobile
Web01 | 2.8.1411023.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100