Emmmanuel FIADUFE

Emmmanuel FIADUFE

  • 693
  • 1.1k
  • 55.5k

Window Form Textbox Auto Complete saving error

Jun 26 2024 3:38 PM

Hello Team,
 in my C# window Form project, Am using textbox autoComplete  to load data from these tables tblType,Generic,tblFormulation,tblBrand,tblClassification  and save into tblProducts table, and it doesn't allow me to save different data except the same data  is getting save multiple times and as well doesn't allow me to Update different data.
 

 private void loadBrand()
        {

            SqlConnection con = new SqlConnection(dbcon.MyConnection());
            SqlCommand com = new SqlCommand("Select Brand from tblBrand", con);
            con.Open();
            SqlDataReader rdr = com.ExecuteReader();
             AutoCompleteStringCollection autoCompleteCollection = new AutoCompleteStringCollection();
            while (rdr.Read())
            {
                autoCompleteCollection.Add(rdr.GetString(0));
            }
           CustomSource  
            txt_Brand.AutoCompleteSource = AutoCompleteSource.CustomSource;
            SuggestAppend. SuggestAppend Applies both Suggest and Append  
            txt_Brand.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            txt_Brand.AutoCompleteCustomSource = autoCompleteCollection;
            con.Close();

        }


 private void txt_Brand_TextChanged(object sender, EventArgs e)
        {
            try
            {

               cn.Open();
                cm = new SqlCommand("select * from tblBrand where Brand like @Brand", cn);
                cm.Parameters.AddWithValue("@Brand", "%" + txt_Brand.Text + "%");
                dr = cm.ExecuteReader();

                while (dr.Read())
                {
                    if (lblBrand.Text.Trim() == string.Empty)
                    {
                        lblBrand.Text = dr[0].ToString();
                    }
                   
                }

                dr.Close();
                cn.Close();
            }
            catch (Exception ex)
            {
                if (dr != null && !dr.IsClosed)
                {
                    dr.Close();
                }
                if (cn.State == ConnectionState.Open)
                {
                    cn.Close();
                }
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

        }

 


Answers (1)