0

I can`t to return value from second query. Part of code...

 MySqlConnectionStringBuilder mysqlSB = new MySqlConnectionStringBuilder();
                mysqlSB.Server = "localhost";
                mysqlSB.Database = "test";
                mysqlSB.UserID = "admin";
                mysqlSB.Password = "1111";


                MySqlConnection con = new MySqlConnection();
                con.ConnectionString = mysqlSB.ConnectionString;

                MySqlCommand Select = new MySqlCommand("select name from table_1 where id='1' ", con);      
                MySqlDataReader myReader;
                con.Open();
                myReader = Select.ExecuteReader();

                while (myReader.Read())
            {
                count++;
            }
            string name = myReader["name"].ToString();

  if (count == 1)
                {

                    MySqlCommand Select2 = new MySqlCommand("select country from table_2 where name='"+name+"'", con);
                     MySqlDataReader myReader2;
                    myReader2 = Select2.ExecuteReader();
                    while (myReader2.Read())
                    {
                        count++;
                    }

                   return myReader2["id"].ToString();
                }

If I delete second part, after if(count==1) and return name = all ok, but when I return id will error. Plase Tell why, because I need to return second, third... values of query. Thank you!

user3313127
  • 93
  • 2
  • 10

2 Answers2

0

If I were you I'd fill a datatable with the results of your query

See here Connecting to a Mysql DB with C# - Need some with Datasets

I don't know what you want to do with the countries you select out but if its in a datatable you can then bind it to a dropdown or something

Community
  • 1
  • 1
theedam
  • 629
  • 3
  • 10
0

In your second you are returning id field and are only selecting the country field from the database. It isn't returning an id field for you to read from thus an error.

Chris
  • 37
  • 6