3
Reply

Unable to Insert Data Using StoredProc from C#

mark doxtater

mark doxtater

Mar 18 2006 8:31 PM
2.3k
Hi, this is my first time here and I was wondering if someone could give me a hand with an issue that I've spent a couple days on now.

I am writing an application that keeps inventory of engine parts.  One of my forms allows the user to add a new parts manufacturer record to a SQL Server Express database named "Inventory.mdb" by gathering information from 3 textboxes on the form and calculating the next available "ManufacturerID".  These 4 pieces of information are then passed as parameters to a stored procedure called "InsertManufacturer" which contains the following code:

ALTER PROCEDURE dbo.InsertManufacturer

@ManID int,
@ManName
varchar (50),
@ContactName
varchar (50),
@ContactNumber
varchar (20)
AS
INSERT INTO Manufacturer
VALUES (@ManID, @ManName, @ContactName, @ContactNumber)

RETURN

private void btnAdd_Click(object sender, EventArgs e)

{

string myManName = txtManName.Text;

string myContact = txtContactName.Text;

string myNumber = txtPhoneNumber.Text;

SqlConnection myConnection = new SqlConnection();

myConnection.ConnectionString = StationInventorySystem.Properties.Settings.Default.InventoryConnectionString;

SqlCommand myInsert = new SqlCommand("dbo.InsertManufacturer", myConnection);

myInsert.CommandType = CommandType.StoredProcedure;
//I wasn't sure if the commandtext was "sticking" above so I added the following line to make sure.
myInsert.CommandText =
"dbo.InsertManufacturer";

SqlParameter pManID = myInsert.Parameters.Add("@ManID", SqlDbType.Int, 10);

SqlParameter pManName = myInsert.Parameters.Add("@ManName", SqlDbType.VarChar, 50);

SqlParameter pContactName = myInsert.Parameters.Add("@ContactName", SqlDbType.VarChar, 50);

SqlParameter pContactNumber = myInsert.Parameters.Add("@ContactNumber", SqlDbType.VarChar, 50);

pManID.Value = myNewManID;

pManName.Value = myManName;

pContactName.Value = myContact;

pContactNumber.Value = myNumber;

try

{

myConnection.Open();

myInsert.ExecuteNonQuery();

myConnection.Close();

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
this.Close();
}
-----------------------end code--------------------
For some reason when I execute the InsertManufacturer stored procedure from within the designer window in visual studio and supply the parameters, the record is inserted.  However, when I build my project and run the code above, the record will not be inserted.  No exception is being thrown, but for some reason the values simply are not inserted and the application continues as if nothing is wrong.  This problem is driving me nuts, so any advice you can give (even another / different way that might work) will be extremely appreciated.  I'm using Visual C# Express and SQL Server Express. 

S.O.S


Answers (3)