Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
SqlDataAdapter da = new SqlDataAdapter("select * from sysobjects where xtype='u'", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            comboBox1.DataSource = ds.Tables[0].DefaultView;
            comboBox1.DisplayMember = "name";


this lines of code gives all table names to combobox after that i need to display all tables in grid view one at a time for this i write bellow code

SqlDataAdapter da = new SqlDataAdapter("select * from "     +comboBox1.SelectedItem.ToString() + "", con);
DataSet ds = new DataSet();
da.Fill(ds);
dataGridView1.DataSource = ds.Tables[0];


but when compiling got a error like Invalid object name 'System.Data.DataRowView' HOW CAN I RECTIFY MY PROBLEM PLEASE HELP ME
I AM SERIOUSLY FACING THIS PROB


THANK YOU IN ADVANCE
Posted
Updated 19-Aug-10 18:48pm
v2
Comments
Toli Cuturicu 20-Aug-10 6:59am    
Reason for my vote of 2
Simply awful.
By the way, DON'T SHOUT!!!

Try using
("select * from " +comboBox1.SelectedValue.ToString()) + "", con);

Before this bind value property of combo..

da.Fill(ds);
comboBox1.DataSource = ds.Tables[0].DefaultView;
comboBox1.DisplayMember = "name";
comboBox1.ValueMember = "name";
 
Share this answer
 
Comments
T.Saravanann 20-Aug-10 0:51am    
Reason for my vote of 5
Tulasi is working fine.
Good Answer.
Are you using comboBox1_SelectedIndexChanged event...
if yes
You need use try -catch
because for first time when form loaded, this event fire ...ant throw exception

This code work properly...I Have checked
C#
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                
                SqlDataAdapter dt = new SqlDataAdapter("Select * from " + comboBox1.SelectedValue.ToString(), con.Connection);
                DataSet ds = new DataSet();
                dt.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0];
            }
            catch (Exception ex) { MessageBox.Show(ex.Message); }
        }
 
Share this answer
 
Comments
T.Saravanann 20-Aug-10 0:55am    
Reason for my vote of 5
Correct Answer..
Christian Graus 20-Aug-10 0:59am    
Your code fixes his basic problem, but is still an absolute disaster, in terms of being written to a professional standard. Using a try/catch instead of an 'if' statement is also wasteful of resources.
tulasi7777777 20-Aug-10 1:01am    
thank you sir its my mistake i am sorry
very essential data given by you sir
Both your code and the code someone gave you are a disaster. You should be writing a proper data layer, giving your controls meaningful names, and generally writing readable code.

The fact that you have no idea what your error is, proves you know nothing about C#, do a C# course, then learn ASP.NET after that. You should also learn to use the debugger, so you can see the SQL being generated. The issue is that ToString returns the class name of the object, not the SelectedValue, as someone rightly said. His answer will fix your immediate problem, but you have far deeper issues that need addressing.
 
Share this answer
 
Comments
tulasi7777777 20-Aug-10 1:03am    
Reason for my vote of 2
useful message given to me
jayantbramhankar 20-Aug-10 1:06am    
Reason for my vote of 5
Agreed sir..
tulasi7777777 20-Aug-10 1:32am    
but doing this way wont give exception
under form load
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
comboBox1.Items.Add(ds.Tables[0].Rows[i][0]);
selected index of combo box at this time no error sir
but doing like Combobox1.DisplyMember("name") gives error y sir
tulasiram.v 22-Jan-15 4:20am    
you need to learn about display member and display value then you will find all the answer
you need to prepare well first and go the above answer is absolutly right
think before asking a answer
 
Share this answer
 
Comments
tulasiram3975 20-Aug-10 2:14am    
thank you ok fine

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