Click here to Skip to main content
15,893,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have used the following code for displaying names of my sql db tables in combobox...now i want dat when i click on any of these table names from combo box..my dgv populates with that table's contents
private void Form1_Load(object sender, EventArgs e)
        {
            String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

            SqlConnection con = new SqlConnection(strConnection);
            try
            {

                con.Open();

                SqlCommand sqlCmd = new SqlCommand();

                sqlCmd.Connection = con;
                sqlCmd.CommandType = CommandType.Text;
                sqlCmd.CommandText = "Select table_name from information_schema.tables";

                SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

                DataTable dtRecord = new DataTable();
                sqlDataAdap.Fill(dtRecord);
                comboBox1.DataSource = dtRecord;
                comboBox1.DisplayMember = "TABLE_NAME";
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Posted
Updated 12-Mar-13 3:02am
v2
Comments
Shanu2rick 11-Mar-13 6:35am    
What have you tried yet?
mayuri koul 11-Mar-13 6:39am    
private void PopulateGridView(string tablename)
{
String strConnection = "Data Source=HP\\SQLEXPRESS;database=MK;Integrated Security=true";

SqlConnection con = new SqlConnection(strConnection);
con.Open();

string tableName = comboBox1.SelectedText;

SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "Select * from " + tableName;

SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);

DataTable dtRecord = new DataTable();
sqlDataAdap.Fill(dtRecord);
dataGridView1.DataSource = dtRecord;
con.Close();
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedValue != null)
{
PopulateGridView(comboBox1.SelectedValue.ToString());
}
}
but it's not working :(

hello mayuri

i think it will help you

C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
 {

      string table_name=combobox1.selecteditem.tostring(); //or can use string table_name= combox1.text      
      con.Open();
      SqlDataAdapter da = new SqlDataAdapter("select * from " + table_name + " ",con);
        DataSet ds = new DataSet();
        da.Fill(ds);
        dgv.DataSource = ds.Tables[0];
        dgv.DataBind();
    
        con.Close();
 }

happy to help
 
Share this answer
 
Comments
mayuri koul 11-Mar-13 6:55am    
thnks but...it is showing error in con.open() and close...stating does not exist in current context :(
plz help
Shubh Agrahari 11-Mar-13 8:12am    
try your own connection string that successfully runs in previous problem...
vishal.shimpi 11-Mar-13 8:22am    
define
SqlConnection Con = new SqlConnection("Your Connectionstring");
before
con.open();
Make sure that your gridview property "AutoGeneratedColumns=true", becouse you dynamically bind records to the gridview...
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.SelectedValue != null)
            {
                PopulateGridView(comboBox1.SelectedValue.ToString());
            }
        }

and make sure that in above code "comboBox1.SelectedValue" is your table name..
 
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