Click here to Skip to main content
15,898,035 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I created a stored procedure in SQL Server 2005 is supposed to insert values into a table, Customers. In Visual Studio 2008, I created a data class that represents the fields in the Customer Table. I also created a Data Access Class that accepts connects to the database to perform CRUD. I am able to fetch information from the table, perform updates and delete.

However, I have tried inserting records for days, without success. I believe the trouble is not from my VS Application! Below is the code for the Data Access Class Insert Method:
VB
public void InsertCustomer(string customerName, string address)
        {
            SqlConnection conn = new SqlConnection(connectionString);
            SqlCommand cmd = new SqlCommand("CustomerInsert", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@CustomerName", customerName);
            cmd.Parameters.AddWithValue("@Address", address);
            try
            {
                conn.Open();
                cmd.ExecuteNonQuery();
                conn.Close();
                return;
           }
            catch
            {
                conn.Close();
                return;
            }
        }


I placed a break point in this method, and all the variables are holding exactly what i want inserted into the database. However nothing makes it into the database.

The Table definition is as follows:
SQL
CustomerID int, PK
CustomerName varchar(50)
Address varchar(50)




Below is the Stored Procedure Code:

SQL
CREATE PROCEDURE [dbo].[CustomerInsert]
(
    @CustomerID   int,
    @CustomerName varchar(50),
    @Address    varchar(50)
	
)
AS
INSERT INTO dbo.Customer
(
    CustomerID,
    CustomerName,
    Address
)
VALUES
(
    @CustomerID,
    @CustomerName,
    @Address
)


Please what am i doing wrong? Someone PLEASE HELP ME!!!! Waiting.....
Posted

You're not setting the @CustomerID parameter.

If you run your code you will get an exception that tells you exactly that.

Nick
 
Share this answer
 
remove CustomerId from de store procedure and check that is auto increment.
 
Share this answer
 
remove CustomerId from de store procedure and check that is auto increment.

And add a mesage for you like


C#
try
{
....
}
catch(Exeption ex)
{
Messagebox.show(ex.toString(), "Error");
// or 
//Console.Writeln("Error: " + ex.ToString());
....
}
 
Share this answer
 
CustomerID is a primarykey and i dont see anywhere u are setting the value of PK or passing.

SQL
INSERT INTO dbo.Customer
(
    CustomerID,
    CustomerName,
    Address
)
select 

    isnull(max(CustomerID,0) + 1,
    @CustomerName,
    @Address from dbo.Customer
)



Try this It will work.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900