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??????????
Ravi ShekharPosted Jun 4, 2013, 1:04 AM
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.
Jignesh TrivediPosted May 30, 2013, 4:24 AM
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.