hi friends!!
i m trying to generate id using ASP.net + c# from sql server. I m getting these type of errors
"Invalid attempt to read when no data present"
while i have data in the table.
AFter some modification in code it is not able to insert the value i generate for ID in the table rather
it is inserting 0 only.
<pre lang="cs"> int id =0;
protected void Button1_Click(object sender, EventArgs e)
{
cm = new SqlCommand("select MAX(id) from login",cn);
dr = cm.ExecuteReader();
if(dr.Read()==false)
id = Convert.ToInt32(dr[0].ToString());
if (id == 0)
{
id = 1001;
TextBox4.Text = id.ToString();
}
else {
TextBox4.Text = dr[0].ToString();
id = Convert.ToInt32(dr[0].ToString()) + 1;
}
dr.Close();
TextBox3.Text = "My id is : "+id.ToString();
}
i have assumption that user will have provide blank table where he will insert his live data then it should generate id number 1001, 1002, 1003 ... n ( i dont want to use auto increment)
code for insertion of data into table
<pre lang="cs">protected void Button3_Click(object sender, EventArgs e)
{
cm = new SqlCommand("insert into login values('" + TextBox2.Text + "','" + TextBox1.Text + "','" + id.ToString() + "')", cn);
cm.ExecuteNonQuery();
}
Plz give me your hints why it is generating such error