Click here to Skip to main content
15,912,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a column "ITEMS" in sql server and i want to save all listbox items in server.
using ado.net
Posted

1 solution

Hi,

Have a look at the below code:
It iterates for the number of items in your listbox and inserts them into the table in sql.
Just a sample code..you can enhance it further, and don't forget to use try-catch-finally block..

C#
protected void Button1_Click(object sender, EventArgs e)
    {

        foreach (ListItem item in ListBox1.Items)
        {
            
            SqlCommand cmd =new SqlCommand( "Insert into TableName(ColumnName) Values ("+item.Text+")",xconn);

            xconn.Open();
            cmd.ExcuteNonQuery();
            xconn.Close();

        }

    } 


Hope it helps..

-Anurag
 
Share this answer
 
Comments
arvindnitin7 14-Mar-13 4:41am    
foreach (ListItem item in ListBox1.Items)

in this line 'ListItem' have an error
Anurag Sinha V 14-Mar-13 5:55am    
hmm...debug and check what is the error there and try to remove it..
i dont have access to your computer, hence i cant debug from here...
i have provided you with a sample code buddy...try to remove the errors and excel..:)

-anurag

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