Link ComboBox with Database in C#

Drag and down a ComboBox from ToolBox make a function for getting database value (Name) in ComboBox.

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);

         } 

    }

}

Ebook Download
View all
Learn
View all