-1

I'm trying to insert data from my database in a combo box specifically showing all my tables in the combobox here's my connection

private void Form1_Load(object sender, EventArgs e)
{
     string connetionString = null;
     SqlConnection cnn;
     connetionString = "Data Source=ITWORKSDEV01;Initial Catalog=ITWorksDEV";
     cnn = new SqlConnection(connetionString);

     for (int i = 0; i < 5; i++)
     {
         comboBox1.Items.Add(i.ToString());
     }
 }
6
  • 3
    where is the remaining code? Commented Jun 27, 2014 at 5:48
  • why vb.net is added in tags? Commented Jun 27, 2014 at 5:50
  • ^ its a typo sorry it auto corrected Commented Jun 27, 2014 at 5:52
  • After update in the question. Still code is not enough. It is recommended to search about the topic Commented Jun 27, 2014 at 5:57
  • YOu're only creating a connection - you're not actually executing any SQL query against the database! Commented Jun 27, 2014 at 6:06

1 Answer 1

1

First of all, which database-type are you using ?

You can look-up the correct connection-code for each database here: http://www.connectionstrings.com/

Second, don't forget to open the connection:

private void Form1_Load(object sender, EventArgs e)
{
     string connetionString = null;
     connetionString = "Data Source=ITWORKSDEV01;Initial Catalog=ITWorksDEV";
     using (SqlConnection cnn = new SqlConnection(connetionString))
     {
         cnn.Open();

After you opened the connection, create a DataReader and a SQL-Statement:

SqlDataReader and SqlCommand

     }
}

Closing/Disposing isn't needed here, using makes that job for you.

But stackoverflow is not a place where we write you the code, you have to write it by yourself and if you get errors we can help you.

Good luck!

Sign up to request clarification or add additional context in comments.

4 Comments

Note: closing connections is also very important
@SergeyBerezovskiy Yes you are right. Closing/Disposing(which includes closing) is important, will add it to the answer.
Second note :) It's better to use using statement to close connection even if exception happens
@SergeyBerezovskiy True the second :P Actually I normally use using, I think I just wanted to fix the OP's-code and didn't improve it. My bad :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.