.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Ava Aiden
Imran Ghani
Thomas Shaw

Home >> Code Snippets >> Visual Studio >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Add and reload new items in the listbox

Posted By:satyapriyanayak       Posted Date: September 01, 2014    Points: 40    Category: Visual Studio    URL: http://www.dotnetspark.com  

In this Code Snippets we will know how to add and reload new items in the listbox.

We will know how to insert records to database and show one column of inserted items into a listbox.Here we use two textboxes, button and a listbox.And after adding items it is automatically loaded to the listbox.

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Windows.Forms;

using System.Data.SqlClient;

namespace After_add_new_item_reload_listbox


    public partial class Form1 : Form


        SqlConnection con = new SqlConnection(@"server = HOME-SAT/SQLEXPRESS;data source = .; initial catalog = test; user id=sa;pwd =pintu ;integrated security = SSPI");

        SqlCommand com;

        SqlDataAdapter sqlda;

        DataSet ds;

        string str;

        DataTable dt;

        public Form1()




        private void Form1_Load(object sender, EventArgs e)




        private void listboxbind()



            str = "select * from employee";

            com = new SqlCommand(str, con);

            sqlda = new SqlDataAdapter(com);

            ds = new DataSet();

            sqlda.Fill(ds, "employee");

            dt = ds.Tables["employee"];

            listBox1.DisplayMember = "empid";

            listBox1.ValueMember = "empname";

            listBox1.DataSource = ds.Tables["employee"];



        private void btn_add_Click(object sender, EventArgs e)


            com = new SqlCommand();

            com.Connection = con;

            com.CommandType = CommandType.Text;

            com.CommandText = "insert into employee values(@empid,@empname)";


            com.Parameters.AddWithValue("@empid", txt_id.Text);

            com.Parameters.AddWithValue("@empname", txt_name.Text);

            if (con.State == ConnectionState.Closed)




            MessageBox.Show("Data entered successfully!!!");




        private void clear()


            txt_id.Text = "";






Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend