Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
hello friends

for example
i m having two rows in database table as id and name.

id description

1 asasdfasdfsadfasdfasrf
2 aawerwerwetawtgagsdg
3 eewerwerwerwerwerwer

i want to get the text box value with respect to id.

please give me solution
Posted

1 solution

Try:
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT name FROM myTable WHERE id=@ID", con))
        {
        com.Parameters.AddWithValue("@ID", iD);
        using (SqlDataReader reader = com.ExecuteReader())
            {
            if (reader.Read())
                {
                myTextBox.Text = (string) reader["name"];
                }
            }
        }
    }



"tnx... one doubt instead of using datareader can we use datatable.. if we can means ...tel me with example..."


using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand com = new SqlCommand("SELECT name FROM myTable WHERE id=@ID", con))
        {
        com.Parameters.AddWithValue("@ID", iD);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(com);
        da.Fill(dt);
        myTextBox.Text = (string) dt.Rows[0]["name"];
        }
    }
 
Share this answer
 
v2
Comments
beginner in C#.net 23-May-11 6:15am    
tnx... one doubt instead of using datareader can we use datatable.. if we can means ...tel me with example...
Prerak Patel 23-May-11 6:18am    
ExecuteScalar would be more appropriate here, I guess. But for beginner, I think a link to tutorial would be best.
OriginalGriff 23-May-11 6:22am    
Possible - but then tomorrow we get a question asking how to return the name and address from ExecuteScalar! :laugh:
Prerak Patel 23-May-11 6:23am    
:laugh: :thumbs up: Have a 5 for the answer, and its datatable variant.
beginner in C#.net 23-May-11 6:39am    
tnx for ur ans... i know this al the things very silly question because i learnt from cp an online only... i dont have any person to guide.... u al my mentor.... i will again ask like this silly que... i think u ll ans... because u r legend...

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