Click here to Skip to main content
15,889,861 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello
I created a DataBase named charityah containing 5 tables
their names are listed in a combobox
when i choose one of them i want to display its content in a datagridview

What i tried is
first i linked the datagridview to this database
and tried this code that i found

C#
SqlConnection connection = new SqlConnection();
 
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string s = comboBox1.Text;
     
    connection.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True";
         
    using (connection)
    {
    connection.Open();
    SqlDataAdapter adapter = new SqlDataAdapter("select * from "+s, connection);
    DataSet ds = new DataSet();
    adapter.Fill(ds, s);
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = s;
    dataGridView1.Refresh();   
}



this method doesn't give me any error and it finds the tables but nothing is seenin the datagridview
Posted
v3
Comments
kumar2413 21-May-13 2:36am    
u missed dataGridView1.dataBind()
Member 8403770 21-May-13 2:48am    
no such method is there any using system i should add?
kumar2413 21-May-13 2:53am    
If u didn't bind the data how u going to see in the grid view then
Member 8403770 21-May-13 3:00am    
how can i bind it?
kumar2413 21-May-13 3:02am    
Just put this code "dataGridView1.dataBind()" after the refresh code and check out.

C#
SqlConnection con = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True");
DataSet ds = new DataSet();
SqlDataAdapter DataAdapter1 = new SqlDataAdapter();
SqlCommand command = new SqlCommand();


command.CommandText = ("select * from"+s, connection);
command.CommandType = CommandType.Text;
command.Connection = con;

DataAdapter1.SelectCommand = command;
DataAdapter1.Fill(ds, s);
DataGridView1.DataSource = ds.Tables[0];



Try this code and see.It might work.
 
Share this answer
 
Comments
Member 8403770 21-May-13 3:48am    
Could not find server 'System' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.

what is this error?
Try -

SqlConnection connection = new SqlConnection();
 
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string s = comboBox1.Text;
     
    connection.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True";
         
    using (connection)
    {
    connection.Open();
    SqlDataAdapter adapter = new SqlDataAdapter("select * from "+s, connection);
    DataSet ds = new DataSet();
    adapter.Fill(ds, s);
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = s;
    adapter.Fill(ds);
    dataGridView1.DataSource = ds;
    dataGridView1.DataMember = ds.Tables[0].TableName;
    dataGridView1.Refresh();   
}
 
Share this answer
 
Comments
Member 8403770 21-May-13 4:14am    
it's working but the same problem as the method i posted
it's not giving me any error but no table is shown in the datagrid view
but i noticed that ds.Tables[0].TableName doesn't give me the tablename i chose
I would change a small part of your code from

C#
dataGridview1.DataSource = ds;


to
C#
dataGridView1.DataSource = ds.Tables[0]


UPDATE:

C#
string TableName = comboBox1.Text;
string Query = "Select * from " + TableName;
DataSet ds = new DataSet();

using(SqlConnection connection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True"))
{
  SqlDataAdapter da = new SqlDataAdapater();
  da.SelectCommand = new SqlCommand(Query, connection);
  da.fill(ds, TableName);
}

DataGridview1.DataSource = ds.Tables[0];
 
Share this answer
 
v2
Comments
Member 8403770 21-May-13 5:46am    
there's something else missing :S
can't find what
everything is in the datasource but no changes in the datagridview
Simon_Whale 21-May-13 5:51am    
declare the dataset outside of the using(connection) statement and try it again.
Member 8403770 21-May-13 5:54am    
nothing changed
Simon_Whale 21-May-13 5:57am    
you need to check that there is data in the dataset now.
Member 8403770 21-May-13 6:06am    
i already checked
C#
dataGridView1.VirtualMode = false;
dataGridView1.Columns.Clear();
dataGridView1.AutoGenerateColumns = true;


Problem solved :D
ty all for ur help
 
Share this answer
 
v2

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