Click here to Skip to main content
15,896,557 members
Please Sign up or sign in to vote.
1.60/5 (2 votes)
See more:
hello




how to get max id in database table and show it in label
i do this code to get the max id +1 in SQL DB
SELECT MAX (patient_id)+1 FROM patient_data
and make this code in C#

C#
SqlConnection cnz = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
                SqlCommand cmdzs = new SqlCommand("SELECT MAX (patient_id) FROM patient_data ", cnz);
                    cmdzs.CommandType = CommandType.Text;
                    cmdzs.Connection.Open();
                    SqlDataReader dr = cmdzs.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {

                            max_id_lbl.Text = dr["MAX (patient_id)"].ToString();
                        
                        }
                    }





but i error here
C#
max_id_lbl.Text = dr["MAX (patient_id)"].ToString();

so what is wrong here
thanks and sorry for bad english
Posted
Comments
CHill60 10-Nov-13 8:45am    
Try changing your sql to read MAX (patient_id) as Result and then use max_id_lbl.Text = dr["Result"].ToString();
u mani 25-Jul-14 4:43am    
max_id_lbl.Text = dr["MAX (patient_id)"].ToString(); instead of this write like this

max_id_lbl.Text = dr[0].ToString();

I suggest you to use ExecuteScalar whenever you want to retrieve single value from database as it perform better than ExecuteReader and also always use using statement with SqlConnection object to make sure it get disposed properly. You can try below code snippet.

string conStr = ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString;
          using (SqlConnection cnz = new SqlConnection(conStr))
          {
              SqlCommand cmdzs = new SqlCommand("SELECT MAX (patient_id) as max_patient_id FROM patient_data ", cnz);
              cmdzs.CommandType = CommandType.Text;
              cnz.Open();
              max_id_lbl.Text = cmdzs.ExecuteScalar().ToString();
          }
 
Share this answer
 
v3
Comments
[no name] 30-Jul-14 0:27am    
Good option
try like this it will work
C#
SqlConnection cnz = new SqlConnection(ConfigurationManager.ConnectionStrings["lap_appConnectionString"].ConnectionString);
                SqlCommand cmdzs = new SqlCommand("SELECT MAX (patient_id) FROM patient_data ", cnz);
                    cmdzs.CommandType = CommandType.Text;
                    cmdzs.Connection.Open();
                    SqlDataReader dr = cmdzs.ExecuteReader();
                    if (dr.HasRows)
                    {
                        while (dr.Read())
                        {
 
                            max_id_lbl.Text = dr[0].ToString();
                        
                        }
                    }
 
Share this answer
 

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