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

I want to know how to get values in textboxes from database using data reader property of ado.net.
Posted
Updated 14-Nov-10 20:27pm
v2

Hi,
the answer is to use while loop and Read() method of datareader:

http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.data.datareader.read(VS.80).aspx[^]

C#
if (dr.HasRows)
{
    while(dr.Read())
    {
        txtEmpName.Text = Convert.IsDBNull(dr["EmpName "]) ? "" : Convert.ToString(dr["EmpName "]);
    }
}
 
Share this answer
 
A sample code


string str1 = "Select Name from Login where Login='" + EmpID.Text + "'";
SqlCommand cmd1 = new SqlCommand(str1, con1);

cmd1.ExecuteNonQuery();

SqlDataReader dr;
dr = cmd1.ExecuteReader();
if (dr.HasRows)
{
dr.Read();

txtEmpName.Text = Convert.IsDBNull(dr["EmpName "]) ? "" : Convert.ToString(dr["EmpName "]);
}
 
Share this answer
 
This is simple example:
string str="select LastName,FirstName from Employee";
SqlCommand cmd=new SqlCommand(str);
con.Open();
SqlDataReader dr=cmd.ExecuteReader();
while(dr.Read())
{
textbox1.Text=dr[0].ToString();
}
con.Close();
 
Share this answer
 
v2
Comments
TweakBird 1-Feb-11 4:08am    
Please use <pre> tag for code blocks.
Try this....
C#
string strEmpName = TextBox1.Text;
cmd = new SqlCommand("SELECT empname FROM emptable WHERE id ='"+id+"'", sqlcon);
con.Open();
cmd.ExecuteNonQuery();
SqlDataReader dr;
dr = cmd1.ExecuteReader();
if (dr.HasRows)
{
dr.Read();

txtEmpName.Text = Convert.IsDBNull(dr["empname "]) ? "" : Convert.ToString(dr["strEmpName"]);
}
con.Close();
 
Share this answer
 
v2
Comments
TweakBird 1-Feb-11 4:09am    
Use <pre> tag for code blocks.
:laugh: erggewgbb
tiyukyu,
jf tyjkk
tuiiokuo
 
Share this answer
 
Comments
TweakBird 1-Feb-11 4:09am    
What is this.?
TweakBird 1-Feb-11 4:11am    
P.S: Please change display name to other than email id.

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