473,378 Members | 1,078 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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:

Expand|Select|Wrap|Line Numbers
  1. <asp:ListBox runat="server" ID="AuthorList" DataTextField="AuthorName" 
  2.             DataValueField="AuthorName" DataSourceID="Authors"  SelectionMode="Multiple"/>  
  3.  
Datasource code:
Expand|Select|Wrap|Line Numbers
  1. <asp:ObjectDataSource runat="server" ID="ds_InventoriedAuthors" SelectMethod="GetAuthors" TypeName="CntyLibrary.Inventory.Authors" InsertMethod="AddAuthors">
  2.     <InsertParameters>
  3.         <asp:QueryStringParameter Name="AuthorID" Type="Int32" QueryStringField="id" />
  4.         <asp:ControlParameter Name="ReqUserID" Type="Int32" ControlID="AuthorList" PropertyName="SelectedValue" />
  5.         <asp:ProfileParameter Name="ReqBy" Type="Int32" PropertyName="ReqUserID" />
  6.     </InsertParameters>
  7.     <SelectParameters>
  8.         <asp:QueryStringParameter Name="AuthorID" QueryStringField="id" Type="Int32" />
  9.     </SelectParameters>
  10. </asp:ObjectDataSource>
  11.  
The button:
Expand|Select|Wrap|Line Numbers
  1. protected void btnAddAuthors(object sender, EventArgs e)
  2.     {
  3.         dsAddAuthors.Insert();
  4.         lbAuthorList.ClearSelection();
  5.         Response.Redirect(Request.Url.AbsoluteUri);
  6.     } 
  7.  
The code behind:
Expand|Select|Wrap|Line Numbers
  1. public static void AddAuthors(Int32 AuthorID, Int32 ReqUserID, Int32 AddedByUser)
  2.         {
  3.             using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings[ConfigurationManager.AppSettings["AUTHORS"]].ConnectionString))
  4.             {
  5.                 using (SqlCommand comm = new SqlCommand())
  6.                 {
  7.                     comm.CommandText = "add_Authors";
  8.                     comm.CommandType = CommandType.StoredProcedure;
  9.                     comm.Connection = conn;
  10.                     comm.Parameters.AddWithValue("authorid", AuthorID);
  11.                     comm.Parameters.AddWithValue("Requserid", ReqUserID);
  12.                     comm.Parameters.AddWithValue("addedbyUser", AddedByUser);
  13.                     conn.Open();
  14.                     comm.ExecuteNonQuery();
  15.                     conn.Close();
  16.                 }
  17.             }
  18. }
Jan 17 '14 #1
3 1536
I tried modifying my code to the below but am still unable to insert multiple values:
Expand|Select|Wrap|Line Numbers
  1.  protected void addAuthors_button(object sender, EventArgs e)
  2.     {
  3.         AddAuthors_ds.Insert();
  4.         StringCollection sc = new StringCollection();
  5.         foreach (ListItem item in AuthorsList.Items)
  6.         {
  7.             if (item.Selected)
  8.             {
  9.  
  10.                  AddAuthors_ds.InsertParameters.Clear();
  11.                  AddAuthors_ds.InsertParameters.Add("AuthorID", item.Text);
  12.                 AddAuthors_ds.Insert();
  13.             }
  14.         }
  15.          AddAuthors_ds.Insert();
  16.         AuthorsList.ClearSelection();
  17.         Response.Redirect(Request.Url.AbsoluteUri);
  18.     }
Jan 20 '14 #2
Luk3r
300 256MB
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)

Expand|Select|Wrap|Line Numbers
  1. comm.CommandText = "INSERT INTO [Table1] ([Col1], [Col2], [Col3]) VALUES ("'Col1Value', 'Col2Value', 'Col3Value')" 
Jan 20 '14 #3
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.
Jan 20 '14 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: carlos.pacheco | last post by:
Hi all, I have a internet page written in asp to submit into authors curriculum vitae publications (title, author, year, etc.). If the author submit less than 8000 characters it functions OK,...
2
by: David | last post by:
I have a problem with the Listbox Web control in C#. It works fine in VB but in C#, the .Selected property always returns false for some reason. I have the Listbox Selectionmode set to Multiple. ...
2
by: Richard Wilde | last post by:
I am trying to change a colour of a listbox item depending on a value in a data view My fields in the dataview are ID Name idCat ListBox1.ValueMember = "ID" ListBox1.DataSource = dv1...
2
by: deepaks85 | last post by:
Dear Friends, I have a small problem....but hope you guys can help me out...I am unable to submit data into my database. Here is the code...... filesubmit.htm <html> <head>
7
by: mikeh3275 | last post by:
I'm creating a .net program that uploads images to the FTP server. A blank listbox is populated dynamically on the client side from the value of the html input file widget. There is also a...
1
by: John | last post by:
Hi When I try to get a list of roles using RolesArray = Roles.GetAllRoles() the array returned is Nothing. However if I assign a user to a role as below Roles.AddUserToRole(User.UserName,...
1
by: sbandalli | last post by:
Hi All, I have one more question in my project, Last time the reply I got really helped me a lot . This is the project in WinForm(C#). I want to copy the text in the text box to a dropdownlist or...
0
by: Srikanth A | last post by:
how to create muliple listBox in GWT with following requirements... ListBox list = new ListBox(true);//true because i don't need listbox to show like dropdown... now on the screen i will have...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.