Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted

Invalid attempt to read when no data present

Try changing this:

if (dr.Read() == false)


to this

if (dr.Read())


Your current code is trying to do something if the reader does NOT read. that's backwards from what you actually want.
 
Share this answer
 
v2
try use this code:

C#
cm = new SqlCommand("select isnull(MAX(id),0) from login",cn);        
dr = cm.ExecuteReader();                  
if(dr.Read())                                                     
{
     id = Convert.ToInt32(dr[0].ToString());                                                
}
 
Share this answer
 
v2
it is still inserting 0 in the database... i could not understand why it is taking 0 in the dbo.table
 
Share this answer
 
Comments
Pete O'Hanlon 19-Jul-11 14:16pm    
You are getting the maximum value, which means that you have to add one on to it. Suppose the maximum value is 3. If you get the max value, and then try to insert based on that, the max value is still 3.
NIKS SHINE 19-Jul-11 14:43pm    
yes.. u r right, but may i know if there is any solution regarding this bug.. thanks
NIKS SHINE 19-Jul-11 14:30pm    
yes, if 0 is the max value than again it is showing 0 in the table instead it should display or insert 1001...
NIKS SHINE 19-Jul-11 23:24pm    
has resolved... partially

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900