Click here to Skip to main content
15,913,854 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have one text box and 3 list box, i want add this data in database in sql server 2005.so, how i add data to database from list box.
Posted

Dear Friend,

Following links will help you out:-

Inserting data from listbox into database in C#[^]

http://stackoverflow.com/questions/8612821/insert-listbox-items-into-multiple-field-in-database-c-sharp[^]

Don't forget to mark this as your answer if it helps you out.

Thanks
 
Share this answer
 
Use this


C#
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
 
namespace test
{
public partial class MainForm : Form
{
  public MainForm()
  {
      InitializeComponent();
  }
 
  void Button1Click(object sender, EventArgs e)
  {
     SqlConnection conn = new SqlConnection(@"User ID=username; Password=pass; 
                    Initial Catalog=databasename; data source=localhost");
     SqlCommand cmd = conn.CreateCommand();
     foreach(ListItem i in listBox1.SelectedItem)
           {
               cmd.CommandText = "INSERT INTO TableName (columnName) VALUES('"+i.Text)+"')";
               cmd.ExecuteNonQuery();
           }
     conn.Close();
  }
}


Plz accept and vote otherwise revert back with your answer
--Rahul D.
 
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