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:
Datasource code:
The button:
The code behind:
The code from the listbox:
Code:
<asp:ListBox runat="server" ID="AuthorList" DataTextField="AuthorName" DataValueField="AuthorName" DataSourceID="Authors" SelectionMode="Multiple"/>
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>
Code:
protected void btnAddAuthors(object sender, EventArgs e) { dsAddAuthors.Insert(); lbAuthorList.ClearSelection(); Response.Redirect(Request.Url.AbsoluteUri); }
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(); } } }
Comment