Hi!
im getting this error. I dont know why.
PLZ can anyone help me.
this is my code

public void getLastMemID()
        {
String concString = "Data Source=BJ-PC\\SQLEXPRESS;Initial Catalog=new;Integrated Security=SSPI";
           // int ID=0;
            try
            {


                CN = new SqlConnection(concString);
                CN.Open();
               
                SqlCommand com = new SqlCommand("SELECT MAX(ImageId) FROM ImagesStore", CN);
                SqlDataReader dr = com.ExecuteReader();
                MessageBox.Show("after com");
                if (dr.HasRows)
                {
                    MessageBox.Show("after hasrows");
                    while (dr.Read())
                    {
                       
                        MessageBox.Show(dr["ImageId"].ToString());
                   }
            }
            else
            {
            MessageBox.Show("No rows");
            }
                
       


            }
            catch (SqlException ex)
            {
                MessageBox.Show("A SqlException")
                MessageBox.Show(ex.ToString());

            }
            finally
            {
                CN.Close();


            }
          
        }

This is not how we do the method in "correct" way. I put the correct word into quotation marks, becuase everything is correct as long as the code works, but there wre have some rules we have to follow, so the code is more appropriate and looks better and works better (better performance - which is one of the goals of every programmer).
So try to do it this way:

static string concString = @"Data Source=BJ-PC\\SQLEXPRESS;Initial Catalog=new;Integrated Security=SSPI";
        public void getLastMemID()
        {
            int ID = 0;
            using (SqlConnection CN = new SqlConnection(concString))
            {
                CN.Open();
                string sqlQuery = @"SELECT MAX(ImageId) FROM ImagesStore";
                using (SqlCommand com = new SqlCommand(sqlQuery, CN))
                {
                    using (SqlDataReader dr = com.ExecuteReader())
                    {
                        if (dr.Read())
                            ID = (int)dr[0];
                    }
                }
            }
            if (ID > 0)
                MessageBox.Show("Max id is " + ID.ToString());
            else
                MessageBox.Show("There is no id in this table (its an empty table)");
        }

This is not how we do the method in "correct" way. I put the correct word into quotation marks, becuase everything is correct as long as the code works, but there wre have some rules we have to follow, so the code is more appropriate and looks better and works better (better performance - which is one of the goals of every programmer).
So try to do it this way:

static string concString = @"Data Source=BJ-PC\\SQLEXPRESS;Initial Catalog=new;Integrated Security=SSPI";
        public void getLastMemID()
        {
            int ID = 0;
            using (SqlConnection CN = new SqlConnection(concString))
            {
                CN.Open();
                string sqlQuery = @"SELECT MAX(ImageId) FROM ImagesStore";
                using (SqlCommand com = new SqlCommand(sqlQuery, CN))
                {
                    using (SqlDataReader dr = com.ExecuteReader())
                    {
                        if (dr.Read())
                            ID = (int)dr[0];
                    }
                }
            }
            if (ID > 0)
                MessageBox.Show("Max id is " + ID.ToString());
            else
                MessageBox.Show("There is no id in this table (its an empty table)");
        }

Thank you very much.
i was trying for 1 week.
Once again thank you.

If you could plz explain me why "@" sign is there or give me a link
to a site.I'll read it.

Thank you.

You are welcome.
bye

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.