Click here to Skip to main content
11,704,315 members (45,661 online)
Rate this: bad
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);
    DataSet ds = new DataSet();
    MySqlDataAdapter da = new MySqlDataAdapter("Select username, password, type from Admin", cs);
    MySqlCommandBuilder cmd = new MySqlCommandBuilder(da);

I want to do a if statement on values in ds in column type.
Posted 3-Apr-13 15:31pm
Edited 3-Apr-13 19:08pm
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
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,

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

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 740
1 OriginalGriff 520
2 Maciej Los 355
3 Andy Lanng 284
4 CPallini 275
0 OriginalGriff 8,824
1 Sergey Alexandrovich Kryukov 8,247
2 CPallini 5,197
3 Maciej Los 4,726
4 Mika Wendelius 3,606

Advertise | Privacy | Mobile
Web03 | 2.8.150819.1 | Last Updated 4 Apr 2013
Copyright © CodeProject, 1999-2015
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