Click here to Skip to main content
16,004,761 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Trying to insert selected items in a list box to SQL server database.

aspx.net code:
text boxes are:

company name
title
contact
address
city
state
zip code
telephone
fax phone
cell phone
email address



My list box:

category (list box)

Store procedure:

SQL
-- Creates a new record in the [dbo].[CustomerContact] table.
ALTER PROCEDURE [dbo].[CustomerContactAdd]
    @p_CompanyName nvarchar(50),
    @p_Title nvarchar(50),
    @p_Contact nvarchar(50),
    @p_Address nvarchar(50),
    @p_City nvarchar(50),
    @p_State nvarchar(50),
    @p_ZipCode numeric(18,0),
    @p_Telephone nvarchar(50),
    @p_FaxPhone nvarchar(50),
    @p_CellPhone nvarchar(50),
    @p_EmailAddress nvarchar(50),
    @p_Category nvarchar(50),
    @p_CustomerID_out int output
AS

BEGIN TRANSACTION
IF @p_CustomerID_out = 0

BEGIN
    INSERT
    INTO [dbo].[CustomerContact]
        (
            [CompanyName],
            [Title],
            [Contact],
            [Address],
            [City],
            [State],
            [ZipCode],
            [Telephone],
            [FaxPhone],
            [CellPhone],
            [EmailAddress],
            [Category]
        )
    VALUES
        (
             @p_CompanyName,
             @p_Title,
             @p_Contact,
             @p_Address,
             @p_City,
             @p_State,
             @p_ZipCode,
             @p_Telephone,
             @p_FaxPhone,
             @p_CellPhone,
             @p_EmailAddress,
             @p_Category
        )

    SET @p_CustomerID_out = SCOPE_IDENTITY()

END


C# code:

C#
public void Save_Click_Base(object sender, EventArgs args)
        {
            string query = "CustomerContactAdd";
            int recMode = 0;
            int recID = 0;
            SqlConnection con = new SqlConnection(GetConnectionString());
            con.Open();
           SqlCommand cmd = new SqlCommand(query, con);
            cmd.CommandType = CommandType.StoredProcedure;

             cmd.Parameters.Add(new SqlParameter("@p_CustomerID", SqlDbType.Int));
                if(recMode == 0)
                {
                   // New record
                    cmd.Parameters["@p_CustomerID"].Value = recID + 1;
                }
                else
               {
                    //Editing a record
                    cmd.Parameters["@p_CustomerID"].Value = recID;
                }
                    
            cmd.Parameters.Add("@p_CompanyName", SqlDbType.NVarChar).Value = CompanyName.Text;
            cmd.Parameters.Add("@p_CustomerID", SqlDbType.Int);
            cmd.Parameters["@p_CustomerID"].Value = 0;
            cmd.Parameters["@p_CustomerID"].Direction = ParameterDirection.Output;             
            cmd.Parameters.Add("@p_Title", SqlDbType.NVarChar).Value = Title.Text;
            cmd.Parameters.Add("@p_Contact", SqlDbType.NVarChar).Value = ContactName.Text;
            cmd.Parameters.Add("@p_Address", SqlDbType.NVarChar).Value = Address.Text;
            cmd.Parameters.Add("@p_City", SqlDbType.NVarChar).Value = City.Text;
            cmd.Parameters.Add("@p_State", SqlDbType.NVarChar).Value = State.Text;
            cmd.Parameters.Add("@p_ZipCode", SqlDbType.Decimal).Value = ZipCode.Text;
            cmd.Parameters.Add("@p_Telephone", SqlDbType.NVarChar).Value = Telephone.Text;
            cmd.Parameters.Add("@p_FaxPhone", SqlDbType.NVarChar).Value = FaxPhone.Text;
            cmd.Parameters.Add("@p_CellPhone", SqlDbType.NVarChar).Value = CellPhone.Text;
            cmd.Parameters.Add("@p_EmailAddress", SqlDbType.NVarChar).Value = EmailAddress.Text;
            foreach (ListItem item in Category.Items)
            {
                if (item.Selected && item.Value != "0")
                {
                    cmd.Parameters.Add("@p_Category", SqlDbType.NVarChar).Value = Category.Text;
                    cmd.ExecuteNonQuery();
                }
            }


        }

        private string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

        }



Getting error message

Procedure or function 'CustomerContactAdd' has to many arguments specified.

when I go to loop and get to the cmd.ExecuteNonQuery

[Modified: just added the sql language parameter in the pre tag for your sql code to color-code it better]
Posted
Updated 22-Dec-10 7:50am
v2

Well, then you probably have specified too many parameters so you should go back to your code and look at exactly what you're adding.

You start out by adding a parameter called @p_CustomerID (which, by the way, isn't defined in your stored procedure at all. You have a parameter defined as @p_CustomerID_out, but that's not the same).

Then, you add @p_CompanyName. Then, you add a new parameter and call it the same as the first parameter. So, you now have three parameters in your object:

@p_CustomerID, @p_CompanyName, @p_CustomerID


Then, if we go to the bottom of the code, for each item in Category.Items, you add a new parameter called @p_Category. So, if there were 4 items, then you would have 4 parameters called @p_Category

I'm not sure how it checks, but you can't have more parameters than are defined in your stored procedure.

You would be better to initially create all of your parameters (once). And, then set them individually. That way, you will ensure you don't have too many parameters.
 
Share this answer
 
Comments
postonoh 22-Dec-10 14:28pm    
Made the following changes


cmd.Parameters.Add(new SqlParameter("@p_CustomerID_out", SqlDbType.Int));
if (recMode == 0)
{
// New record
cmd.Parameters["@p_CustomerID_out"].Value = recID + 1;
}
else
{
//Editing a record
cmd.Parameters["@p_CustomerID_out"].Value = recID;
}

cmd.Parameters.AddWithValue("@p_CompanyName", SqlDbType.NVarChar).Value = CompanyName.Text;
cmd.Parameters["@p_CustomerID_out"].Direction = ParameterDirection.Output;
cmd.Parameters.Add("@p_Title", SqlDbType.NVarChar).Value = Title.Text;
cmd.Parameters.Add("@p_Contact", SqlDbType.NVarChar).Value = ContactName.Text;
cmd.Parameters.Add("@p_Address", SqlDbType.NVarChar).Value = Address.Text;
cmd.Parameters.Add("@p_City", SqlDbType.NVarChar).Value = City.Text;
cmd.Parameters.Add("@p_State", SqlDbType.NVarChar).Value = State.Text;
cmd.Parameters.Add("@p_ZipCode", SqlDbType.NVarChar).Value = ZipCode.Text;
cmd.Parameters.Add("@p_Telephone", SqlDbType.NVarChar).Value = Telephone.Text;
cmd.Parameters.Add("@p_FaxPhone", SqlDbType.NVarChar).Value = FaxPhone.Text;
cmd.Parameters.Add("@p_CellPhone", SqlDbType.NVarChar).Value = CellPhone.Text;
cmd.Parameters.Add("@p_EmailAddress", SqlDbType.NVarChar).Value = EmailAddress.Text;
foreach (ListItem item in Category.Items)
{
if (item.Selected && item.Value != "0")
{
cmd.Parameters.Add("@p_Category", SqlDbType.NVarChar).Value = Category.Text;
cmd.ExecuteNonQuery();
}
}

now getting error code:

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1.

I am trying to if possible append items selected in the list box and insert them into the category column in the table. Can you help with this.
William Winner 22-Dec-10 14:35pm    
You have to commit the transaction in the stored procedure. see here: http://msdn.microsoft.com/en-us/library/ms188929.aspx
As an FYI, you also have a problem at
C#
cmd.Parameters.AddWithValue("@p_CompanyName", SqlDbType.NVarChar).Value = CompanyName.Text; 

You didn't want to use AddWithValue You won't be getting what you expect into @p_CompanyName
 
Share this answer
 
Comments
postonoh 22-Dec-10 14:48pm    
thanks
postonoh 22-Dec-10 14:56pm    
Thanks figure how to append selected. thanks get still getting the following error message.

Transaction count after EXECUTE indicates a mismatching number of BEGIN and COMMIT statements. Previous count = 0, current count = 1. Researching this now
postonoh 22-Dec-10 15:09pm    
Thanks for pointing me the right direction. I had to move this cmd.Parameters["@p_CustomerID_out"].Direction = ParameterDirection.Output;
under the cmd.Parameters["@p_CustomerID_out"].Value = recID + 1;
fix the problem.

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