Click here to Skip to main content
15,916,188 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi

Could you pls correct me where i am going wrong.
From database i am retriving the value to textbox.


C#
SqlDataReader reader = null;
        string connectionString = ConfigurationManager.ConnectionStrings["InvoiceConnectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connectionString);
        string sqlcommand = "SELECT DoNo from DO where DoNo = DoNo";
        SqlCommand cmd = new SqlCommand(sqlcommand, conn);
        conn.Open();
        reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            dono = reader["DoNo"].ToString();
            dono = DoNotb.Text;
        
        }

i am able to see the value here
C#
{
   dono = reader["DoNo"].ToString();
   dono = DoNotb.Text;
}

finally it is becoming null

Regards
Lancy
Posted
Updated 5-Nov-11 2:48am
v3

C#
while (reader.Read())
        {
            dono = reader["DoNo"].ToString();
            DoNotb.Text = dono;
        
        }
 
Share this answer
 
Comments
Sander Rossel 5-Nov-11 8:52am    
Same answer as me. Answered at the same time so my 5 :)
Lancy.net 5-Nov-11 8:56am    
Thanks i too find just now
I assume DoNotb is your TextBox? In that case your last line of code should be turned around:
C#
dono = reader["DoNo"].ToString();
DoNotb.Text = dono;

You are currently giving the dono variable the value of your TextBox, which is empty.
Instead you should give your TextBox.Text the value of the dono variable, as in my code.
Do notice that the Text in your TextBox is overridden in every new iteration. So you will always end up with the value of the last row in your database.
 
Share this answer
 
v2
Assignment is wrong.
you have assign Textbox value to variable. do exactly reverse.

C#
DoNotb.Text = dono;
 
Share this answer
 
Everythings fine. Just assignment is incorrect. You give the value from TextBox which is currently empty to a variable (dono). That is why, dono returning null value;

C#
while(reader.Read())
{
     dono=reader["DoNo"].ToString();
     DoNotb.Text=dono;
}
 
Share this answer
 

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