Friday 18 April 2014

Database values in textbox if select Combobox in window form



                                                                                                                                            previous....
                                                                                                                                                  Next..

Here i will explain that when i select any name from combobox then is all detail show in respective textboxes.

Step(1):-Form with combobox
drag and down a combobx from toolbox.
Step(2):-Coding
Click on combobox and write this coding.


Private Void comboBox1.SelectedIndexChanged(object sender , EventArgs e)
{
    string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=123;";
    SqlConnection con = new SqlConnection(str);
    string query = "select * from tablename where name = '"+combobox1.text+"' ";
    SqlCommand cmd = new SqlCommand( query,con);
    sqldatareader dbr;
    try
    {
        con.open();
        dbr = cmd.executereader();
        while(dbr.read())
        {
            string sID = (string) dbr["ID"].tostring;
            string sname = (string) dbr["name"// name is string value
             string ssurname = (string) dbr["surname"] 
            string sage = (string) dbr["age"].tostring;
            textbox1.text = sid;
            textbox2.text = sname;
            textbox3.text = ssurname ;
            textbox4.text = sage;

        }
        catch(execption es)
        {
           messagebox.show(es.message);
        }
    }
}
Step(3):-Output
Run your application and click on name which is in combobox.

                                                                                                                                            previous....
                                                                                                                                                  Next..

5 comments:

  1. can i retrieve data from sql database to textboxes in windows form?

    ReplyDelete
  2. Yes you can do this also..

    SqlConnection con = new SqlConnection("server=Munesh-PC; database=Temp; uid= sa;pwd=***");
    SqlDataReader dr;
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
    con.Open();

    string GetData = "Select * from TempTable where ID= 1";
    cmd = new SqlCommand(GetData, con);
    dr = cmd.ExecuteReader();

    if (dr.Read())
    {

    TextBox1.Text = dr[0].ToString();
    TextBox2.Text = dr[1].ToString();
    }

    ReplyDelete
  3. Are you in Australia? if so are you in Sydney? i am looking for a tutor for visual C#

    ReplyDelete
  4. are you in Australia? if so are you in Sydney? i am looking for a visual studio c# tutor.

    ReplyDelete
  5. how can i get data in textboxes from database if my combobox selected values are from table1 and the values i want to get in textboxes are from table2?

    ReplyDelete

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...