0

I am trying to insert data into my table. I have created a stored procedure that will do this. I also created a method that calls the stored procedure. When I click on the submit button, the click event is fired and I get the input data and call the method to insert to the table.

I get no errors but when I check in my database, the data has not been inserted.

I have tried executing the stored procedure manually and it does work.

Here's my stored procedure:

CREATE PROCEDURE [dbo].AddRestaurant
    @theName VARCHAR(250),
    @theAddr VARCHAR(350), 
    @theCity VARCHAR(50),
    @theState VARCHAR(2),
    @theZip VARCHAR(5),
    @theCategory INT
AS
BEGIN
    INSERT INTO Restaurants (RestName, RestAddr, RestCity, RestState, RestZip, CategoryID)
    VALUES (@theName, @theAddr, @theCity, @theState, @theZip, @theCategory)
END

Here's my table structure:

CREATE TABLE [dbo].[Restaurants] 
(
    [RestaurantID] INT IDENTITY (1, 1) NOT NULL,
    [RestName]     VARCHAR(250) NULL,
    [RestAddr]     VARCHAR(350) NULL,
    [RestCity]     VARCHAR(50)  NULL,
    [RestState]    VARCHAR(2)   NULL,
    [RestZip]      VARCHAR(5)   NULL,
    [CategoryID]   INT          NULL,

    PRIMARY KEY CLUSTERED ([RestaurantID] ASC),

    CONSTRAINT [FK_Restaurants_CatergoryID] 
        FOREIGN KEY ([CategoryID]) 
        REFERENCES [dbo].[Categories] ([CategoryID])
);

Here's my method:

public void AddRestaurant(string name, string addr, string city, string state, string zip, int category)
{
    DBConnect objDB = new DBConnect();

    objCmd.Parameters.Clear();
    objCmd.CommandType = CommandType.StoredProcedure;
    objCmd.CommandText = "AddRestaurant";

    objCmd.Parameters.AddWithValue("@theName", name);
    objCmd.Parameters.AddWithValue("@theAddr", addr);
    objCmd.Parameters.AddWithValue("@theCity", city);
    objCmd.Parameters.AddWithValue("@theState", state);
    objCmd.Parameters.AddWithValue("@theZip", zip);
    objCmd.Parameters.AddWithValue("@theCategory", category);

    objDB.GetConnection().Open();
    objDB.DoUpdateUsingCmdObj(objCmd);
    int affectedRows = objDB.DoUpdateUsingCmdObj(objCmd);
    objDB.CloseConnection();
}

Here's the click event in the aspx.cs file:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {
        rest.RestName = txtRestName.Text;
        rest.RestAddr = txtRestAddr.Text;
        rest.RestCity = txtRestCity.Text;
        rest.RestState = ddStates.SelectedValue;
        rest.RestZip = txtRestZip.Text;
        rest.CategoryID = int.Parse(ddCategories.SelectedValue);

        p.AddRestaurant(txtRestName.Text, txtRestAddr.Text, txtRestCity.Text, ddStates.SelectedItem.Value, txtRestZip.Text, int.Parse(ddCategories.SelectedValue));

        string error = txtRestName.Text + " has been added!";
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + error + "');", true);
    }
    catch (SqlException ex)
    {
        string error = txtRestName.Text + " has NOT been added!" + ex;
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + error + "');", true);
    }
}

Here's my aspx page :

<fieldset class="center, content2">
            <legend>Restaurant Info</legend>
            <asp:Label ID="lblRestName" runat="server" CssClass="left">Name: </asp:Label>
            <asp:TextBox ID="txtRestName" runat="server" CssClass="right"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblRestAddr" runat="server" CssClass="left">Address: </asp:Label>
            <asp:TextBox ID="txtRestAddr" runat="server" CssClass="right"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblRestCity" runat="server" CssClass="left">City: </asp:Label>
            <asp:TextBox ID="txtRestCity" runat="server" CssClass="right"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblRestState" runat="server" CssClass="left">State: </asp:Label>
            <asp:DropDownList ID="ddStates" runat="server" CssClass="right" Width="140px">
                <asp:ListItem Value="AL">Alabama</asp:ListItem>
                <asp:ListItem Value="AK">Alaska</asp:ListItem>
                <asp:ListItem Value="AZ">Arizona</asp:ListItem>
                <asp:ListItem Value="AR">Arkansas</asp:ListItem>
                <asp:ListItem Value="CA">California</asp:ListItem>
                <asp:ListItem Value="CO">Colorado</asp:ListItem>
                <asp:ListItem Value="CT">Connecticut</asp:ListItem>
                <asp:ListItem Value="DC">District of Columbia</asp:ListItem>
                <asp:ListItem Value="DE">Delaware</asp:ListItem>
                <asp:ListItem Value="FL">Florida</asp:ListItem>
                <asp:ListItem Value="GA">Georgia</asp:ListItem>
                <asp:ListItem Value="HI">Hawaii</asp:ListItem>
                <asp:ListItem Value="ID">Idaho</asp:ListItem>
                <asp:ListItem Value="IL">Illinois</asp:ListItem>
                <asp:ListItem Value="IN">Indiana</asp:ListItem>
                <asp:ListItem Value="IA">Iowa</asp:ListItem>
                <asp:ListItem Value="KS">Kansas</asp:ListItem>
                <asp:ListItem Value="KY">Kentucky</asp:ListItem>
                <asp:ListItem Value="LA">Louisiana</asp:ListItem>
                <asp:ListItem Value="ME">Maine</asp:ListItem>
                <asp:ListItem Value="MD">Maryland</asp:ListItem>
                <asp:ListItem Value="MA">Massachusetts</asp:ListItem>
                <asp:ListItem Value="MI">Michigan</asp:ListItem>
                <asp:ListItem Value="MN">Minnesota</asp:ListItem>
                <asp:ListItem Value="MS">Mississippi</asp:ListItem>
                <asp:ListItem Value="MO">Missouri</asp:ListItem>
                <asp:ListItem Value="MT">Montana</asp:ListItem>
                <asp:ListItem Value="NE">Nebraska</asp:ListItem>
                <asp:ListItem Value="NV">Nevada</asp:ListItem>
                <asp:ListItem Value="NH">New Hampshire</asp:ListItem>
                <asp:ListItem Value="NJ">New Jersey</asp:ListItem>
                <asp:ListItem Value="NM">New Mexico</asp:ListItem>
                <asp:ListItem Value="NY">New York</asp:ListItem>
                <asp:ListItem Value="NC">North Carolina</asp:ListItem>
                <asp:ListItem Value="ND">North Dakota</asp:ListItem>
                <asp:ListItem Value="OH">Ohio</asp:ListItem>
                <asp:ListItem Value="OK">Oklahoma</asp:ListItem>
                <asp:ListItem Value="OR">Oregon</asp:ListItem>
                <asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
                <asp:ListItem Value="RI">Rhode Island</asp:ListItem>
                <asp:ListItem Value="SC">South Carolina</asp:ListItem>
                <asp:ListItem Value="SD">South Dakota</asp:ListItem>
                <asp:ListItem Value="TN">Tennessee</asp:ListItem>
                <asp:ListItem Value="TX">Texas</asp:ListItem>
                <asp:ListItem Value="UT">Utah</asp:ListItem>
                <asp:ListItem Value="VT">Vermont</asp:ListItem>
                <asp:ListItem Value="VA">Virginia</asp:ListItem>
                <asp:ListItem Value="WA">Washington</asp:ListItem>
                <asp:ListItem Value="WV">West Virginia</asp:ListItem>
                <asp:ListItem Value="WI">Wisconsin</asp:ListItem>
                <asp:ListItem Value="WY">Wyoming</asp:ListItem>
            </asp:DropDownList>
            <br /><br />
            <asp:Label ID="lblRestZip" runat="server" CssClass="left">Zip Code: </asp:Label>
            <asp:TextBox ID="txtRestZip" runat="server" CssClass="right"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblCategory" runat="server" CssClass="left">Category: </asp:Label>
            <asp:DropDownList ID="ddCategories" runat="server" CssClass="right" Width="140px">
                <asp:ListItem Value="1">American</asp:ListItem>
                <asp:ListItem Value="2">Mexican</asp:ListItem>
                <asp:ListItem Value="3">Korean</asp:ListItem>
                <asp:ListItem Value="4">Chinese</asp:ListItem>
                <asp:ListItem Value="5">Japanese</asp:ListItem>
            </asp:DropDownList>
            <br /><br />
            <asp:Button ID="btnSubmit" Text="Submit" runat="server" CssClass="button2" OnClick="btnSubmit_Click" />
            <asp:Button ID="btnReview" Text="Add Review" runat="server" OnClick="btnReview_Click" CssClass="button2" />
        </fieldset>
10
  • Are you sure about what database are you checking? It is the same database defined in your connectionstring? Could you post this connectionstring? Commented Mar 7, 2019 at 22:13
  • 1
    You are inserting your data twice. Commented Mar 7, 2019 at 22:14
  • 2
    I don't see the code that's actually executing the procedure. It looks like some sort of wrapper. I see it referenced in a few places on websites. But there's no way of knowing whether your procedure is actually getting executed. My guess is it's not. You might want to try this the old fashioned way, creating a SqlConnection, SqlCommand, and executing it. That wrapper probably isn't adding anything of value except making it harder for you to tell what's actually happening. Commented Mar 7, 2019 at 22:18
  • @Steve it is quite long but I am sure it is used correctly. gist.github.com/76342ck/6333cc69ac82bd9c81ab4e7567c0fc05 Commented Mar 7, 2019 at 22:21
  • 6
    It's an odd phenomenon that developers create little wrapper classes for SQL commands. 95% of the time it doesn't add any value, and you end up having to read the wrapper code to make sure it's doing what you expect, and meanwhile it's just as easy use the System.Data.SqlClient classes directly. Commented Mar 7, 2019 at 23:00

1 Answer 1

1

I found my error. I was opening my connection twice.

objDB.GetConnection().Open();

Change it to:

objDB.GetConnection();
Sign up to request clarification or add additional context in comments.

1 Comment

And you also appear to execute your stored procedure twice: objDB.DoUpdateUsingCmdObj(objCmd); int affectedRows = objDB.DoUpdateUsingCmdObj(objCmd); that might lead to duplicate rows being inserted

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.