Click here to Skip to main content
15,886,801 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi how to insert multiple records into database from listbox?can u provide insert procedure for storedprocedure??
Posted
Comments
zyck 8-Feb-12 23:45pm    
can you provide sample Output
ythisbug 8-Feb-12 23:55pm    
hi like

name[dropdowns]
testcase[dropdowns]
listitems[tiger,elephent,cat,dog]
[btnSave]this records i want to save in sql..how to give procedure for storedprocedure?

Hi,

You may try this link http://www.codeincsharp.blogspot.com/2008/06/how-to-insert-with-progress-bar-c-sharp.html

I hope this one can help.

Thank You.
 
Share this answer
 
v3
Hi,
suppose your list box is Employee ListBox then to insert all emp records into DB call the following method on Save Button click:

C#
public void SavValues()
    {
        SqlConnection connectionSql = new SqlConnection("Data Source=(local);Integrated Security=sspi");
        SqlCommand commandSql = new SqlCommand();
        commandSql.Connection = connectionSql;
        commandSql.CommandText = "INSERT INTO dbo.Employee (EmployeeName) VALUES (@employeeName)";
        commandSql.Parameters.Add("@employeeName");
        try
        {
            connectionSql.Open();
            for (int indexCounter = 0; indexCounter < listBoxEmployeeName.Items.Count; indexCounter++)
            {
                commandSql.Parameters["employeeName"].Value = listBoxEmployeeName.Items[indexCounter].Text;
                commandSql.ExecuteNonQuery();
            }
            connectionSql.Close();
        }
        catch (Exception exceptionMessage)
        {
            //show error message
        }
        finally
        {
            commandSql.Dispose();
            connectionSql.Close();
            connectionSql.Dispose();
        }
    }
 
Share this answer
 
v2
Comments
ythisbug 9-Feb-12 0:24am    
hi in my sqltable contains two dropdowns and one listbox..i saw ur coding for list box and wat about drop down.how to save together when button click for list box and dropdowns??
tanweer 9-Feb-12 0:29am    
are you sure that there are same no of data in both the ListBox and DropDownList??
ythisbug 9-Feb-12 0:34am    
no drop down contains..like names and another dropdowns contains animal names,,and listbox contains state names
ythisbug 9-Feb-12 0:45am    
no .in one table..in one table m i want to insert unlimited records of list items
tanweer 9-Feb-12 0:49am    
I can see that you are going to insert in more than one tables. if so then its easy just modify the above method Like after the first for loop start another for loop that will be like:


commandSql.CommandText = "INSERT INTO 2nd Table (column Name) VALUES (@perameter1)";
commandSql.Parameters.Add("@perameter1");
for (int i= 0; i< istDropDownName.Items.Count; i++)
{
commandSql.Parameters["perameter1"].Value = istDropDownName.Items[i].SelectedItem.Text;
commandSql.ExecuteNonQuery();
}

and then start another loop like

commandSql.CommandText = "INSERT INTO 3rd Table (column Name) VALUES (@perameter2)";
commandSql.Parameters.Add("@perameter2");
for (int j= 0; j< SecDropDownName.Items.Count; j++)
{
commandSql.Parameters["perameter2"].Value = SecDropDownName.Items[j].SelectedItem.Text;
commandSql.ExecuteNonQuery();
}

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