Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: ASP.NET
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());
}

i want to display data from db into textboxes but this code dont work help me please
		
Posted 14-Aug-14 22:51pm
Edited 14-Aug-14 23:09pm
v2
Comments
Ashi0891 at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

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.
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();
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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! Smile | :)
  Permalink  
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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();
}
  Permalink  
Comments
Ashi0891 at 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)



Advertise | Privacy | Mobile
Web04 | 2.8.141015.1 | Last Updated 15 Aug 2014
Copyright © CodeProject, 1999-2014
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