SELECT LAST_INSERT_ID() not working
Posted by: Michael Caruso
Date: March 06, 2005 02:57PM

Using VS.Net 2003 and MySQL 4.1.8 on MS Server 2003.

I have the following (look closely at cmdText) C# code:

int userKey = 0;
			
string cmdText = "INSERT INTO accounts_users (BillingFname, BillingLname, BillingAddress1, "
+ "BillingAddress2, BillingCity, BillingState, BillingZip, BillingCountry, ShipFname, "
+ "ShipLname, ShipAddress1, ShipAddress2, ShipCity, ShipState, ShipZip, ShipCountry, "
+ "Telephone, Email, Password, UserRole) Values "
+ "(?BillingFname, ?BillingLname, ?BillingAddress1, ?BillingAddress2, ?BillingCity, "
+ "?BillingState, ?BillingZip, ?BillingCountry, ?ShipFname, ?ShipLname, ?ShipAddress1, "
+ "?ShipAddress2, ?ShipCity, ?ShipState, ?ShipZip, ?ShipCountry, ?Telephone, ?Email, "
+ "?Password, ?UserRole);SELECT LAST_INSERT_ID();";

MySqlCommand command = new MySqlCommand();
command.CommandText = cmdText;
command.Parameters.Add("?BillingFname", MySqlDbType.VarChar, 30);
command.Parameters.Add("?BillingLname", MySqlDbType.VarChar, 50);
command.Parameters.Add("?BillingAddress1", MySqlDbType.VarChar, 80);
command.Parameters.Add("?BillingAddress2", MySqlDbType.VarChar, 80);
command.Parameters.Add("?BillingCity", MySqlDbType.VarChar, 40);
command.Parameters.Add("?BillingState", MySqlDbType.VarChar, 20);
command.Parameters.Add("?BillingZip", MySqlDbType.VarChar, 15);
command.Parameters.Add("?BillingCountry", MySqlDbType.VarChar, 50);
command.Parameters.Add("?ShipFname", MySqlDbType.VarChar, 30);
command.Parameters.Add("?ShipLname", MySqlDbType.VarChar, 50);
command.Parameters.Add("?ShipAddress1", MySqlDbType.VarChar, 80);
command.Parameters.Add("?ShipAddress2", MySqlDbType.VarChar, 80);
command.Parameters.Add("?ShipCity", MySqlDbType.VarChar, 40);
command.Parameters.Add("?ShipState", MySqlDbType.VarChar, 20);
command.Parameters.Add("?ShipZip", MySqlDbType.VarChar, 15);
command.Parameters.Add("?ShipCountry", MySqlDbType.VarChar, 50);
command.Parameters.Add("?Telephone", MySqlDbType.VarChar, 14);
command.Parameters.Add("?Email", MySqlDbType.VarChar, 255);
command.Parameters.Add("?Password", MySqlDbType.Blob, 20);
command.Parameters.Add("?UserRole", MySqlDbType.VarChar, 15);
command.Parameters[0].Value = webUser.Fname;
command.Parameters[1].Value = webUser.Lname;
command.Parameters[2].Value = webUser.BillingAddress1;
command.Parameters[3].Value = webUser.BillingAddress2;
command.Parameters[4].Value = webUser.BillingCity;
command.Parameters[5].Value = webUser.BillingState;
command.Parameters[6].Value = webUser.BillingZip;
command.Parameters[7].Value = webUser.BillingCountry;
command.Parameters[8].Value = webUser.ShipFname;
command.Parameters[9].Value = webUser.ShipLname;
command.Parameters[10].Value = webUser.ShipAddress1;
command.Parameters[11].Value = webUser.ShipAddress2;
command.Parameters[12].Value = webUser.ShipCity;
command.Parameters[13].Value = webUser.ShipState;
command.Parameters[14].Value = webUser.ShipZip;
command.Parameters[15].Value = webUser.ShipCountry;
command.Parameters[16].Value = webUser.Telephone;
command.Parameters[17].Value = webUser.Email;
command.Parameters[18].Value = webUser.Password;
command.Parameters[19].Value = webUser.UserRole;
userKey = RunNonQuery(command);

RunNonQuery code:

protected int RunNonQuery(MySqlCommand command)
{
    int result = 0;
    try
    {
	Connection.Open();
	command.Connection = Connection;
	result = command.ExecuteNonQuery();
	Connection.Close();
    }
    catch (MySqlException)
    {
	ConnectionException connExc = new ConnectionException("An error has occured reading the database.", 99);
	throw connExc;
    }
    finally
    {
	if (Connection.State == System.Data.ConnectionState.Open)
		Connection.Close();
    }
    return result;
}

RunNonQuery always return a value of 2 as long as the Email address is unique. It acts like its returning a row count (the inserted row and the results of the select last_insert_id()).

Any ideas what's wrong?

TIA,

Michael

Options: ReplyQuote


Subject
Written By
Posted
SELECT LAST_INSERT_ID() not working
March 06, 2005 02:57PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.