TheInsider
Programmer
Hi,
I'm coding my first ASP.Net/C# web application. I knew ASP 3 and ADO like the back of my hand, but I'm having problems with ADO.Net. When I execute the code below, I get an error stating "Procedure 'spAddGuest' expects parameter '@UserName', which was not supplied.". I've looked at several examples on the Web and cannot figure out what I'm doing wrong.
The stored procedure is in MSDE/SQL Server and returns a BigInt value.
Thanks,
Rob
I'm coding my first ASP.Net/C# web application. I knew ASP 3 and ADO like the back of my hand, but I'm having problems with ADO.Net. When I execute the code below, I get an error stating "Procedure 'spAddGuest' expects parameter '@UserName', which was not supplied.". I've looked at several examples on the Web and cannot figure out what I'm doing wrong.
Code:
private void saveData()
{
SqlConnection connection = new SqlConnection(Application["ConnectionString"].ToString());
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "spAddGuest";
SqlParameter parameter;
parameter = command.Parameters.Add("@GuestID", SqlDbType.BigInt);
parameter.Direction = ParameterDirection.ReturnValue;
parameter = command.Parameters.Add("@UserName", SqlDbType.VarChar, 25, txtUserName.ToString().Trim());
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@Password", SqlDbType.VarChar, 12, txtPassword2.ToString().Trim());
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@FirstName", SqlDbType.VarChar, 15, txtFirstName.ToString().Trim());
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@Initials", SqlDbType.Char, 3, txtInitials.ToString().Trim().Length > 0 ? txtInitials.ToString().Trim() : null);
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@LastName", SqlDbType.VarChar, 20, txtLastName.ToString().Trim());
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@Address", SqlDbType.VarChar, 255, txtAddress.ToString().Trim());
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@ZipCode", SqlDbType.Char, 5, txtZipCode.ToString().Trim());
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@Telephone", SqlDbType.Char, 10, txtTelephone.ToString().Trim());
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@Cellular", SqlDbType.Char, 10, txtCellular.ToString().Trim().Length > 0 ? txtCellular.ToString().Trim() : null);
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@Fax", SqlDbType.Char, 10, txtFax.ToString().Trim().Length > 0 ? txtFax.ToString().Trim() : null);
parameter.Direction = ParameterDirection.Input;
parameter = command.Parameters.Add("@EMail", SqlDbType.VarChar, 255, txtEMail.ToString().Trim());
parameter.Direction = ParameterDirection.Input;
try
{
connection.Open();
command.ExecuteNonQuery();
if ((long)command.Parameters["@GuestID"].Value != 0)
{
Session["GuestID"] = (long)command.Parameters["GuestID"].Value;
}
}
catch(Exception e)
{
Response.Write(e.Message);
}
command.Dispose();
connection.Close();
connection.Dispose();
}
The stored procedure is in MSDE/SQL Server and returns a BigInt value.
Thanks,
Rob