Reputation: 1
I think everything is good then why data is not shown in textboxes. I used this code a lot of times. Then what's error here? I am working in Visual Studio 2012.
string name = comboBox1.SelectedIndex.ToString();
query = "select *from Record Where Name='"+name+"'";
cmd = new SqlCommand(query, con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
textBox1.Text =(dr["Items1"].ToString());
textBox2.Text = (dr["Items1_Charge"].ToString());
textBox3.Text = (dr["Items2"].ToString());
textBox4.Text = (dr["Items2_Charge"].ToString());
textBox5.Text = (dr["Items3"].ToString());
textBox6.Text = (dr["Items3_Charge"].ToString());
textBox7.Text = (dr["Items4"].ToString());
textBox8.Text = (dr["Items4_Charge"].ToString());
}
Upvotes: 0
Views: 77
Reputation: 5102
I would suspect *from should be * from. Also I would suggest using parameters for the where condition as shown below especially when there is a chance of an embedded apostrophe. Also note I added HasRows so we are sure something came back. I also change how you are getting the current value from the ComboBox
public void Sample()
{
string name = comboBox1.Text;
query = "select * from Record Where Name= @Name";
cmd = new SqlCommand(query, con);
cmd.Parameters.AddWithValue("@Name", name);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
textBox1.Text = (dr["Items1"].ToString());
textBox2.Text = (dr["Items1_Charge"].ToString());
textBox3.Text = (dr["Items2"].ToString());
textBox4.Text = (dr["Items2_Charge"].ToString());
textBox5.Text = (dr["Items3"].ToString());
textBox6.Text = (dr["Items3_Charge"].ToString());
textBox7.Text = (dr["Items4"].ToString());
textBox8.Text = (dr["Items4_Charge"].ToString());
}
}
}
Upvotes: 1