0

I have SQL table with three columns ID, CUSTOMERNAME and ADDRESS.

I want to show list of all customers ID in combobox1 and when I will select any one ID then Customer name and address should show in datagridview1 belongs to that ID.

Please advice me code as I am learning programming in C#

Below is my app.config for your reference

<connectionStrings>
    <add name="dbx" 
         connectionString="Data Source=USER\SQLEXPRESS;Initial Catalog=dbInventory;Integrated Security=True"  
         providerName="System.Data.SqlClient" />
</connectionStrings>
3
  • Please share the code you tried with. Commented Nov 16, 2016 at 6:15
  • how i can share code please guide me. I am unable to write here Commented Nov 16, 2016 at 20:58
  • You can edit your post and add the related aspx and cs code for the problem you mentioned. You can also format the text using proper editor functionalities. See the screen cap.. prntscr.com/d8b0c1 Commented Nov 17, 2016 at 5:59

1 Answer 1

1

This is the simple one which you can give a try.

private void BindData(string selectCommand)
{
    try
    {
        string selectCommand = "SELECT * FROM tblCustomers";
        String connectionString = System.Configuration.ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;;

        // Create a new data adapter based on the specified query.
        var dataAdapter = new SqlDataAdapter(selectCommand, connectionString);

        // Create a command builder to generate SQL update, insert, and
        // delete commands based on selectCommand. These are used to
        // update the database.
        SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);

        // Populate a new data table and bind it to the BindingSource.
        DataTable table = new DataTable();
        dataAdapter.Fill(table);
      dataGridView1.DataSource = table;
    }
    catch (SqlException)
    {
        MessageBox.Show("To run this example, replace the value of the " +
            "connectionString variable with a connection string that is " +
            "valid for your system.");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.