Unable to successfully submit a list of authors from listbox

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • programmher
    New Member
    • Jan 2014
    • 19

    Unable to successfully submit a list of authors from listbox

    I need to add either one author or a list or array of authors to my sql table. Here is the code that is used to insert the authors into the table:

    The code from the listbox:

    Code:
    <asp:ListBox runat="server" ID="AuthorList" DataTextField="AuthorName" 
                DataValueField="AuthorName" DataSourceID="Authors"  SelectionMode="Multiple"/>
    Datasource code:
    Code:
    <asp:ObjectDataSource runat="server" ID="ds_InventoriedAuthors" SelectMethod="GetAuthors" TypeName="CntyLibrary.Inventory.Authors" InsertMethod="AddAuthors">
        <InsertParameters>
            <asp:QueryStringParameter Name="AuthorID" Type="Int32" QueryStringField="id" />
            <asp:ControlParameter Name="ReqUserID" Type="Int32" ControlID="AuthorList" PropertyName="SelectedValue" />
            <asp:ProfileParameter Name="ReqBy" Type="Int32" PropertyName="ReqUserID" />
        </InsertParameters>
        <SelectParameters>
            <asp:QueryStringParameter Name="AuthorID" QueryStringField="id" Type="Int32" />
        </SelectParameters>
    </asp:ObjectDataSource>
    The button:
    Code:
    protected void btnAddAuthors(object sender, EventArgs e)
        {
            dsAddAuthors.Insert();
            lbAuthorList.ClearSelection();
            Response.Redirect(Request.Url.AbsoluteUri);
        }
    The code behind:
    Code:
    public static void AddAuthors(Int32 AuthorID, Int32 ReqUserID, Int32 AddedByUser)
            {
                using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["AUTHORS"]].ConnectionString))
                {
                    using (SqlCommand comm = new SqlCommand())
                    {
                        comm.CommandText = "add_Authors";
                        comm.CommandType = CommandType.StoredProcedure;
                        comm.Connection = conn;
                        comm.Parameters.AddWithValue("authorid", AuthorID);
                        comm.Parameters.AddWithValue("Requserid", ReqUserID);
                        comm.Parameters.AddWithValue("addedbyUser", AddedByUser);
                        conn.Open();
                        comm.ExecuteNonQuery();
                        conn.Close();
                    }
                }
    }
    Last edited by Rabbit; Jan 17 '14, 09:36 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • programmher
    New Member
    • Jan 2014
    • 19

    #2
    I tried modifying my code to the below but am still unable to insert multiple values:
    Code:
     protected void addAuthors_button(object sender, EventArgs e)
        {
            AddAuthors_ds.Insert();
            StringCollection sc = new StringCollection();
            foreach (ListItem item in AuthorsList.Items)
            {
                if (item.Selected)
                {
    
                     AddAuthors_ds.InsertParameters.Clear();
                     AddAuthors_ds.InsertParameters.Add("AuthorID", item.Text);
                    AddAuthors_ds.Insert();
                }
            }
             AddAuthors_ds.Insert();
            AuthorsList.ClearSelection();
            Response.Redirect(Request.Url.AbsoluteUri);
        }

    Comment

    • Luk3r
      Contributor
      • Jan 2014
      • 300

      #3
      Your SQL CommandText needs to be an actual SQL command to add data to a table. Here's an example of a valid SQL command CommandText (from VB.NET)

      Code:
      comm.CommandText = "INSERT INTO [Table1] ([Col1], [Col2], [Col3]) VALUES ("'Col1Value', 'Col2Value', 'Col3Value')"

      Comment

      • programmher
        New Member
        • Jan 2014
        • 19

        #4
        I am updating code another programmer (who left the company) wrote. The existing code uses a stored procedure. The project manager said she wants to keep it as a stored procedure instead of a SQL statement.

        Comment

        Working...