Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
OK
Making some progress. I can now get the database to update, but it will either just pass the 1st select value for each selected line if I use:

addSubCats.InsertParameters["LOOKUP_TYPE_ID"].DefaultValue = SUB_CATEGORY.SelectedValue.ToString();


and will put all entries on 1 line (but inserts multiple rows) if I use

addSubCats.InsertParameters["LOOKUP_TYPE_ID"].DefaultValue = Request.Form["SUB_CATEGORY"].ToString();


Any ideas on how I can get it to put the correct values in?

________________________________________________________________________

Hi.

I'm trying to insert values (sometimes single, sometimes multiple) from a ListBox on a webform to a SQL database. I need to insert a new line for each entry and have the following code, set in a code behind file that fires when the user clicks on Insert button:

C#
protected void AddCatButton_Click(object sender, EventArgs e)
{
    foreach (ListItem item in SUB_CATEGORY.Items)
        if (item.Selected)
        {
            addSubCats.InsertParameters["COMMUNICATION_ID"].DefaultValue = Request.QueryString["COMMUNICATION_ID"].ToString();
            addSubCats.InsertParameters["LOOKUP_TYPE_ID"].DefaultValue = SUB_CATEGORY.SelectedValue.ToString();
            addSubCats.Insert();
        }
    Response.Redirect("viewcase.aspx?SOURCE_REFERENCE=" + Request.QueryString["SOURCE_REFERENCE"] + "&SUBJECT_REFERENCE=" + Request.QueryString["SUBJECT_REFERENCE"] + "&COMMUNICATION_ID=" + Request.QueryString["COMMUNICATION_ID"]);
}


However, as ever with the most simple things, it's not working.

If I remove the foreach state and the if statements, it will insert the records to the database, but lists the multiple values in the field and in 1 row, not multiple rows as I need it too.

Any ideas?

Cheers

Codemagpie
Posted
Updated 3-Sep-10 2:28am
v3
Comments
senguptaamlan 3-Sep-10 7:20am    
check the DB insert query and ADO.NET implementation code.

1 solution

Try:

foreach (ListItem item in SUB_CATEGORY.Items)
{
/*Insert in db */
     addSubCats.InsertParameters["COMMUNICATION_ID"].DefaultValue = Request.QueryString["COMMUNICATION_ID"].ToString();
addSubCats.InsertParameters["LOOKUP_TYPE_ID"].DefaultValue = item.text
addSubCats.Insert();
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900