Click here to Skip to main content
15,915,328 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I've been trying to set as the source of data of a combobox( in this case combobox2 ) the data in the column of an SQL Database table called "BulkInput" with little success. When I run the code, the combobox is still empty.
Please see code below:

What I have tried:

private void button2_Click(object sender, EventArgs e) 
{ 
   try { 
      SqlConnection bscn = new SqlConnection("Data Source=PV10\\LOCALSERVER;Initial Catalog=SmallSoftwareDB;Integrated Security=True;Pooling=False"); 
      bscn.Open(); 
      string ins = "insert into BulkSale(ProductSource, Date, Quantity, Type, UnitPrice, Total) values('" + textBox2.Text + "', '" + dateTimePicker1.Value + "', '" + textBox3.Text + "', '" + comboBox1.Text + "', '" + textBox4.Text + "', '" + textBox5.Text + "' )";
      string sPN = "select ProductName from BulkInput"; 
      SqlCommand P = new SqlCommand(ins, bscn); 
      SqlCommand Q = new SqlCommand(sPN, bscn); 
      SqlDataReader mRead; 
      mRead = Q.ExecuteReader(); 
      while(mRead.Read())
      { 
         comboBox2.Items.Add(mRead["ProductName"].ToString()); 
      } 
      MessageBox.Show("Successfully Completed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); 
      bscn.Close(); 
   } 
   catch (Exception exc) 
   { 
      MessageBox.Show(exc.Message, " ", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning); 
   } 
}
Posted
Updated 18-Sep-16 7:01am
v4
Comments
[no name] 18-Sep-16 8:20am    
Maybe someone performed an SQL injection attack on your database using your code and there is nothing in your database to return.

You just forget to Execute insert command 'P' ! :)
 
Share this answer
 
Comments
Nganku Junior 18-Sep-16 12:01pm    
Insert command? Where exactly
Taken this is Windows forms, one easy way to fill the combo would be to fetch the data for example to a data table and set the combo box's DataSource property to the data table's default view. You also need to set DisplayMember and ValueMember properties to control what is shown in the combo box and what is the key value. Of course you can just add the values from a reader but things tend to get more complicated if you use separate key column.

For more information, have a look at
- ComboBox.DataSource Property (System.Windows.Forms)[^]
- ListControl.DisplayMember Property (System.Windows.Forms)[^]
- and an example Binding to a ComboBox using a DataTable and Linq -Deborah's Developer MindScape[^]

Another thing is the way you use data from the user interface to insert it into the database. Currently you concatenate values directly to the SQL statement. This introduces several risks and problems like SQL injection. The proper way would be to use parameters. Have a look at Properly executing database operations[^]
 
Share this answer
 
v3
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

The debugger is here to show you what your code is doing and your task is to compare with what it should do.
When the code don't do what is expected, you are close to a bug.

Quote:
C#
string ins = "insert into BulkSale(ProductSource, Date, Quantity, Type, UnitPrice, Total) values('" + textBox2.Text + "', '" + dateTimePicker1.Value + "', '" + textBox3.Text + "', '" + comboBox1.Text + "', '" + textBox4.Text + "', '" + textBox5.Text + "' )";

This code is subject to SQL Injection
SQL injection - Wikipedia, the free encyclopedia[^]
SQL Injection[^]
 
Share this answer
 
I had to initialize the copying at form initialization. Thanks for your comments :)



C#
public B()
        {
            InitializeComponent();

            SqlConnection con = new SqlConnection("Data Source=PV10\\LOCALSERVER;Initial Catalog=SmallSoftwareDB;Integrated Security=True;Pooling=False");
            con.Open();
            string sPn = "select ProductName from BulkInput";
            SqlCommand CsPn = new SqlCommand(sPn, con);

            SqlDataReader mDr;
            mDr = CsPn.ExecuteReader();
            while (mDr.Read())
            {
                comboBox2.Items.Add(mDr["ProductName"].ToString());
            }

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900