Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please what is an error within while loop? c#.net web application program.

C#
protected void Page_Load(object sender, EventArgs e)
    {
        string path = "Data Source=192.168.1.14;Initial Catalog=nazer;User ID=sa;Password=admin*123";
        SqlConnection con = new SqlConnection(path);
        con.Open();
        string query = "select MAX(id) from tbl_samp1";
        SqlCommand cmd = new SqlCommand(query, con);        
        SqlDataReader dre=cmd.ExecuteReader();
        while (dre.Read())
        {            
            int values = (int)dre["id"];          //what error in this line? please...
        TextBox1.Text = values.ToString();
            
        }
        con.Close();
        
    }
Posted
Updated 7-Feb-12 2:06am
v3
Comments
Uday P.Singh 7-Feb-12 7:58am    
what is the error message?

Try This

C#
int values = dre.GetInt32(id); 
 
Share this answer
 
1. The result of the query may be NULL when no rows in the table
2.
C#
dre["id"]
- the query does not have column name "id"
change to
SQL
select MAX(id) as id from tbl_samp1
 
Share this answer
 
Comments
murugeshchinnu 7-Feb-12 8:04am    
sorry that table having id,name and etc.. fields with records
Costica U 7-Feb-12 8:33am    
Yes, the table has the column name "id" but the query uses max(id) with no column name for the result. Look at the changed query within my solution.
USe
C#
int values = int.Parse(dre["id"].ToString());    
 
Share this answer
 
Comments
murugeshchinnu 7-Feb-12 7:59am    
Thanks Uma, but this line also have an error(Index bound of exception)
uspatel 7-Feb-12 23:55pm    
string query = "select MAX(id) as maxid from tbl_samp1";
and use
int values = int.Parse(dre["maxid"].ToString());

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