Usama Shaikh

Usama Shaikh

  • NA
  • 1
  • 653

Retrieve related data through id in text boxes ?

Feb 25 2016 8:54 AM
i want to retrieve data into text boxes through id or name. im using this code:
 public partial class Form3 : Form
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
//SqlDataAdapter sqlda;
//DataSet ds;
string str;
 
private void button4_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection();
con.Open();
str = "select * from Department where Code='" + codeTextBox.Text.Trim() + "'";
com = new SqlCommand(str, con);
SqlDataReader reader = com.ExecuteReader();
if (reader.Read())
{
dep_NameTextBox.Text = reader["Name"].ToString();
dep_TypeTextBox.Text = reader["Dep_Type"].ToString();
//TextBox4.Text = reader["ADDRESS"].ToString();
//TextBox5.Text = reader["PHNO"].ToString();
//TextBox6.Text = reader["STATE"].ToString();
reader.Close();
con.Close();
}
 
When i run the program it generate the error:
An unhandled exception of type 'System.NullReferenceException' occurred in voucher.exe
Additional information: Object reference not set to an instance of an object.
 
how to resolve it?
or how to retrieve related id data in text boxes in other query ?
 
 
 

Answers (1)