Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
MySqlDataReader myReader = null;
MySqlCommand myCommand = new MySqlCommand("select * from users where user_name='" + Session["user"] + "'", con);

myReader = myCommand.ExecuteReader();

while (myReader.Read())
{
TextBox4.Text = (myReader["user_name"].ToString());
}
HTML


i want to display data from db into textboxes but this code dont work help me please
Posted
Updated 27-Nov-20 13:44pm
v2
Comments
Ashi0891 15-Aug-14 5:21am    
ofcourse it doesn't. You are getting *(all coumns) from database and showing it in one textbox, and you think it will work?
secondly.. you dnt get value from reader this way. please check my solution.

Just make it really simple. If you only want 1 column to be display. Do not use "SELECT *", but select only does columns that you need.
C#
MySqlCommand myCommand = new MySqlCommand("SELECT lastlogin FROM users WHERE user_name=@userName", con);
myCommand.Parameters.AddWithValue("userName", Session["user"].ToString());

object result = myCommand.ExecuteScalar();

if (result == null)
{
  TextBox4.Text = "User not found";
}
else
{
  TextBox4.Text = result.ToString();
}
 
Share this answer
 
for SQL:

SqlCommand cmd = new SqlCommand(select <column_1>,<column_2> from <table_name> where <any_condition>, conn);
                conn.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    Label4.Text = dr[0].ToString(); // [0] for <column_1>
                    Label5.Text = dr[1].ToString(); // [1] for <column_2>
                }


in your case:

MySqlCommand myCommand = new MySqlCommand("select <column_name> from users where user_name='" + Session["user"] + "'", con);
con.open();
Mysqldatareader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
TextBox4.Text = myReader[0].ToString();
}
conn.close();


enjoy! :)
 
Share this answer
 
v3
MySqlConnection con = new MySqlConnection();

MySqlCommand com = new MySqlCommand();

MySqlDataReader dr;

con = new MySqlConnection(ConfigurationManager.ConnectionStrings["netConnectionString"].ToString());
con.Open();
com = new MySqlCommand("select password from users where user_name='" + Session["user"] + "'", con);
dr = com.ExecuteReader();
if (dr.Read())
{
Label6.Text = dr[0].ToString();
TextBox4.Text = dr[0].ToString();
}
 
Share this answer
 
Comments
Ashi0891 15-Aug-14 6:13am    
it is not if(dr.read()) - we dnt check for condition here
its while(dr.read()) - we do the following "while" the reader is reading.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900