last_insert_id returning a 0 on Windows 2003 server

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hi,

I am running the following code succesffuly on Windows XP, but it is giving
me problems on Windows 2003 server. On the latter, last_insert_id always
returns a 0!



cmdStr = "INSERT INTO users VALUES (NULL, '" + uemailTextbox.Text + "')";

cmd = new OdbcCommand(cmdStr, conn);

cmd.Connection.Open();

cmd.ExecuteNonQuery();

conn.Close();



string uid = "";

cmdStr = "SELECT LAST_INSERT_ID()";

cmd = new OdbcCommand(cmdStr, conn);

cmd.Connection.Open();

if (cmd.ExecuteScalar() !=null)

uid = cmd.ExecuteScalar().ToString();

conn.Close();



if (uid.Length > 0)

{

cmdStr = "INSERT INTO cats VALUES (NULL, '" + uid + "')";

cmd = new OdbcCommand(cmdStr, conn);

cmd.Connection.Open();

cmd.ExecuteNonQuery();

conn.Close();

}




Thanks in advance.
-Shefali
 
I believe last_insert_id is for MySQL, and is on a per connection basis.
Try executing the scalar on the same connection that executed the insert.
 
Back
Top