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 ?
1 Reply
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Amit Kumar SinghPosted Feb 25, 2016, 10:51 AM