0

This may have been asked before but I could not find exactly what I am searching for.

I have a form which has a dropdown for job titles. Based on the selection in the dropdown, 2 listboxes are populated with items that those roles receive for hardware and software resources. The listboxes are populated by a table called dbo.RoleResources.

In the code behind of my asp form I am needing to save the details of the form to another SQL Server table. I am using a stored procedure that inserts the details into a table called dbo.NewHireReources. For most I can use

SqlCmd.Parameters.Add(@EmployeeName, SqlDbType.NVarChar, 255).Value = txtEmployeeName.Text;

However, I need to get all items in the listboxes to go into a column named Hardware and a column named Software.

I have tried the code shown below, but it is not working effectively as it says too many arguments defined in stored procedure even though all other parameters are commented out at the moment.

foreach (ListItem item in hardwareList.Items)
{
    SqlCmd.Parameters.Clear();
    SqlCmd.Parameters.AddWithValue("@Hardware", item.Value);
}

What is the best way to get all items into the table? Can I just call a select statement and pull in the list from dbo.RoleResources? Any help is appreciated.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Do you want row for each Item from Hardware/Software list or concatenated list? Can you also post all the parameters defined in the SP? – rs. Sep 25 '18 at 18:49
  • Sorry to leave out important information. Yes, I would like concatenated list of all items in the listbox. My sp parameters are both nvarchar(max) for Hardware and Software. The sp just does a simple insert statement for all items in my form. – Philip Herman Sep 25 '18 at 18:59
  • Before you pass listitems to SQL parameters try to create a concatenated string. Try https://stackoverflow.com/questions/559415/concat-all-strings-inside-a-liststring-using-linq and then after you have final concatenated string pass that to SQL parameters. Also using clear in loop will keep clearing parameters and you will end up with one parameter and value that was executed at last. Try to run clear only once before trying to set all parameters. – rs. Sep 25 '18 at 19:12
  • I'm not sure if this is correct. string items = String.Join(",", hardwareList.Text); SqlCmd.Parameters.AddWithValue("@Hardware", items); Can you advise? – Philip Herman Sep 25 '18 at 21:25

0 Answers0