0
votes

Hello all I am trying to populate some textboxes automatically when I select an item from a combobox dropdown list using Microsoft sql server with a usercontrol form. I have written the codes below, however I am getting the items in the combox dropdown list but when I select an item in the combobox dropdown list, nothing happens in the text boxes. the values are in a table called competitors. the columns are Institution, Region, FirstName, LastName and I want the values to display in the respective textboxes when I select the combox dropdown. I seek your assistance in solving this problem. thanks in advance

    private void RabbitCare_Load(object sender, EventArgs e)
    {
        CBoxParishDdlist.Items.Clear();
        SqlConnection connect = new SqlConnection(@"Data Source=ITSPECIALIST\SQLPROJECTS;Initial Catalog=EXPRORESULTS;Integrated Security=True ");
        connect.Open();

        SqlCommand cmd = connect.CreateCommand();
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "Select ParishName FROM Competitors WHERE CompetitiveEventName = 'Care and Management of Bees'";
        cmd.ExecuteNonQuery();
        DataTable dat = new DataTable();
        SqlDataAdapter SDA = new SqlDataAdapter(cmd);
        SDA.Fill(dat);
        foreach (DataRow DAR in dat.Rows)
        {
            CBoxParishDdlist.Items.Add(DAR["ParishName"].ToString());
        }
        connect.Close();
    }

    private void CBoxParishDdlist_SelectedIndexChanged(object sender, EventArgs e)
        {

            SqlConnection connect = new SqlConnection(@"Data Source=ITSPECIALIST\SQLPROJECTS;Initial Catalog=EXPRORESULTS;Integrated Security=True ");
            connect.Open();
            cmd = new SqlCommand("SELECT * FROM  Competitors WHERE ParishName = '" + CBoxParishDdlist.Text + "'", connect);



            cmd.ExecuteNonQuery();
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            while (dr.Read())
            {

                string Institution = (string)dr["Institution"].ToString();
                TxtBoxInstitution.Text = Institution;

                string Region = (string)dr["Region"].ToString();
                TxtBoxRegion.Text = Region;

                string FirstName = (string)dr["FirstName"].ToString();
                TxtBoxFname.Text = FirstName;

                string LastName = (string)dr["LastName"].ToString();
                TxtBoxLname.Text = LastName;

            }
            connect.Close();
1
Did you debug your code? Did you check if while loop is executing or not?Chetan
I did debug and no error was shown. not sure how to check if while looping is executingDean
The code written inside while loop gets executed?Chetan
it didn't has the values were not displayed in the textboxesDean
That means the data is not returned from the db. Try running the same query against the database directly from sql management studio and see if you get any resultsChetan

1 Answers

0
votes

I think this will solve your problem.

TxtBoxInstitution.Text = comboBoxName.SelectedItem.ToString();