Hi,
im new to c#.When i execute the following code i get "vshost.exe has stopped working"
message. Firstly i insert some data to a table in sql server 2008.then i want to retrieve the MAX(Mem_ID). My insertion is working properly. But when im trying to retrive MAX(Mem_ID). It gives above error.
Plzz.. help me to make it work?

public String getLastMemID()
        {
            String ID = null;
            try
            {


                CN = new SqlConnection(concString);
                CN.Open();
            
                SqlCommand com = new SqlCommand("SELECT MAX(Mem_ID) FROM MemberMaster", CN);
                SqlDataReader dr = com.ExecuteReader();



                while (dr.Read())
                {


                    ID = dr.GetInt32(0).ToString();
                }


            }
            catch (SqlException ex)
            {
                MessageBox.Show("A SqlException. You must check the Querry parameteres");
                MessageBox.Show(ex.ToString());

            }
            finally
            {
                CN.Close();


            }
         
            return ID;
        }

Dani AI

Generated

Nice find by . For , that vshost.exe dialog usually means your app threw an unhandled exception under the VS hosting process. In your case, calling GetInt32(0) on SELECT MAX(Mem_ID) will crash when the result is a database NULL (for example, when the table is empty or Mem_ID is nullable). Use ExecuteScalar for single values and guard for DBNull before converting. (learn.microsoft.com)

int? lastId = null;
using (var cn = new SqlConnection(connString))
using (var cmd = new SqlCommand("SELECT MAX(Mem_ID) FROM MemberMaster;", cn))
{
    cn.Open();
    var result = cmd.ExecuteScalar();
    if (result != null && result != DBNull.Value)
        lastId = Convert.ToInt32(result); // or ToInt64 if Mem_ID is bigint
}

If you actually want the ID you just inserted, avoid SELECT MAX(...) because it can race with other inserts. Return the new key in the same statement instead:

  • Append SELECT CAST(SCOPE_IDENTITY() AS int); immediately after the INSERT, which returns the last identity generated in the same scope. (learn.microsoft.com)
  • Or better, use the OUTPUT clause to fetch the key directly from the insert. (learn.microsoft.com)
using (var cn = new SqlConnection(connString))
using (var cmd = new SqlCommand(
    "INSERT INTO MemberMaster(ColA, ColB) OUTPUT INSERTED.Mem_ID VALUES (@a, @b);", cn))
{
    cmd.Parameters.AddWithValue("@a", a);
    cmd.Parameters.AddWithValue("@b", b);
    cn.Open();
    var newId = (int)cmd.ExecuteScalar();
}

A couple of extra tips:

  • Prefer using around connections/commands as above so resources are disposed even on exceptions.
  • If the crash dialog still appears, it is just the host showing your app’s exception; fixing the null/cast issue should make the vshost message disappear.

Recommended Answers

All 2 Replies

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.