Adding a column value using DataReader

Using C # and MySQL

When I select a combobox value, the corresponding value should appear in the text box

C # code.

cmd2 = new OdbcCommand("Select name from users where username='" + cmbuser.Text  + "'", con);
dr= cmd2.ExecuteReader();
while (dr.Read())
{
    txtusername.Text = dr("user");
}

The above code works in VB.Net, but an error is displayed in C #, since the error "dr" is a "field", but is used as a "method"

This line displays an error txtusername.Text = dr("user");

How to solve this error, what is the problem in my code.

C # code help

+3
source share
2 answers

Perhaps you need to use txtusername.Text = dr.GetString(0);instead of your error string ...

+2
source

Use square brackets in C #:

txtusername.Text = dr["user"];

Edit: you need to pass the object to the desired type after.

+4

Source: https://habr.com/ru/post/1757661/


All Articles