error in storing data with the help of windows form

May 30 2013 4:04 AM

int a;

con = new SqlConnection("Data Source=DPSINTERIOR;Initial Catalog=studentzone;Persist Security Info=True;User ID=sa;Password=sa");

con.Open();

cmd = new SqlCommand("Select MAX(Student_id) from studentd", con);

dr = cmd.ExecuteReader();

if (dr.Read())

{

string val = dr.GetInt32(0).ToString();

if (val == "")

{

textBox1.Text = "1";

}

else

{

a = Convert.ToInt32(dr[0].ToString());

a = a + 1;

textBox1.Text = a.ToString();

}

}

i have no data in database. i want to auto generate number in first textbox1 column.....but i found this error "Data is Null. This method or property cannot be called on Null values." how can i solve this problem??????????


Answers (2)