Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a column value by using DataReader

Tags:

c#

.net

mysql

Using C# & MySQL

When I select the combobox value, the corresponding value should display in textbox

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 is working in VB.Net, but in C# showing error as Error "dr' is a 'field' but is used like a 'method' "

It is showing error in this line txtusername.Text = dr("user");

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

Need C# Code Help

like image 384
Gopal Avatar asked Aug 29 '26 11:08

Gopal


1 Answers

Use the rectangular brackets in c#:

txtusername.Text = dr["user"];

Edit: You have to cast the object to the desired type after.

like image 120
Fabian Avatar answered Aug 31 '26 00:08

Fabian