Click here to Skip to main content
15,903,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi,
good morning,
i have taken one listbox and am selecting multiple values in listboxx, and i want to store those values into database and how to write the code in asp.net,,please tell me the code,,please,,

Thanks in Advance
Ganesh
Posted

Please try this:

Step 1:- In my case I use one ListBox(ListBox1),TextBox(TextBox1)(to show the data) and a Button(for save the seleced values in the database)

Step2:- create a table in the database
SQL
create table table2
(
list varchar(max)
)

Step3:- set the AutoPostBack="True" of the ListBox1 and on the OnSelectedIndexChanged(event) we write the following code
TextBox1.Text = TextBox1.Text + ListBox1.SelectedItem.Text;

Step4:- Now on the Button Click Event(for save the value in the database)
C#
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
        conn.Open();
        SqlCommand cmd1 = new SqlCommand("insert into table1(list) values('" + TextBox1.Text + "')", conn);
        cmd1.ExecuteNonQuery();
        conn.Close();

Note:-> in the Web.Config File
XML
<connectionStrings>
    <add name="con" connectionString="Data Source=my\SQLEXPRESS;Initial Catalog=try;Integrated Security=true;" />
  </connectionStrings>
 
Share this answer
 
v3
Comments
Sandeep Mewara 15-Sep-10 15:14pm    
You never listen! :doh:

You don't like PRE tags right? Using them would just make your answer much readable. Try them!
Formatted your answer for now.

Good answer :)
Write a stored procedure in Sql Server for inserting values into table and then call that procedure in your application by passing appropriate parameters.

Search Google for the code of above functionality. Its very simple. :)
 
Share this answer
 
Hi,

Try this,


int[] index = ListBox1.GetSelectedIndices();
for (int i = 0; i < index.Length; i++)
{
   //Database query to update/insert the selected value
   Response.Write(ListBox1.Items[index[i]].Text);
   //ListBox1.Items[index[i]].Text

}


Regards,
Suresh
 
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