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??????????

Jignesh TrivediRank #16 · 50.3K reputationPosted May 30, 2013, 12:24 AM EDT
try following code.
if (dr.Read())
{
string val = dr[0].ToString();
if (val == "")
{
textBox1.Text = "1";
}
else
{
a = Convert.ToInt32(dr[0].ToString());
a = a + 1;
textBox1.Text = a.ToString();
}
}
hope this will help you.
Ravi ShekharRank #673 · 3.6K reputationPosted Jun 3, 2013, 9:04 PM EDT
Just think if you have no data in database then how you get or find data by using below
dr.GetInt32(0).ToString(); code
try Jignesh sir's code.