Friday 18 April 2014

Link ComboBox with database in window form


                                                                                                                                           Previous...
                                                                                                                                               Next...

Here i will explain how to Link combobox with database. Before this o explained that How to use combobox.

Step(1) :- Form with ComboBox
Drag and down a combobox from tool Box.
Step(2) :- Coding
Now at coding section make a function which is connected with database. And call this method in constructor.

Void fillcombo()
{
    string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=123;";
    SqlConnection con = new SqlConnection(str);
    string query = "select * from tablename";
    SqlCommand cmd = new SqlCommand( query,con); 
    sqldatareader dbr;
    try
    {
        con.open();
        dbr = cmd.executereader();
        while(dbr.read())
        {
           string sname = (string) myreader["name"];; //name is coming from database
           combobox1.items.Add(sname);
         }
         catch(execption es)
         {
              messagebox.show(es.message);
         } 
    }
}

Step(3) :- Output
When you run your application the data(we are fatching name from database) will be store in combobox.
                                                                                                                                           Previous...
                                                                                                                                               Next...


No comments:

Post a Comment

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...