Solved

I do not view what I need in my Datagrid

Posted on 2003-12-08
16
625 Views
Last Modified: 2011-09-20
Hello,
here is my code, It is very simple, only one table, I want to show the table content in the Datagrid.
It currently works, but I need to click on a "+" sign, then, the word "table" and then I got my "item table" and the content.

How come?  My       dataGrid1.DataMember is Setted.

How to I get only the table content to be displayed?

Thanks!




                  string _myConnStr = "Data Source=eb.mdb;Provider=Microsoft.Jet.OLEDB.4.0;";
                  string _mysqlstr = "SELECT * FROM item ORDER BY ordernum";
                        try
                        {
                              myOledbConn = new OleDbConnection(_myConnStr);
                              myOledbConn.Open();
                              Console.WriteLine("\nmyOledbConn Open...");
                        }
                        catch(Exception ex)
                        {
                              MessageBox.Show("Error: Failed to create a database connection. \n{0}" + ex.Message);
                              Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
                              return;
                        }

                  selectCMD = new OleDbCommand(_mysqlstr,myOledbConn);
                  myitemDA = new OleDbDataAdapter(selectCMD);
                  myitemDS = new DataSet();
                  myOledbConn.Close();
                  Console.WriteLine("Dataset errors?: " + myitemDS.HasErrors);
                  dataGrid1.DataMember = "item";
                  dataGrid1.DataSource = myitemDS;
                  myitemDS.Clear();
                  myitemDA.Fill(myitemDS);                  
            }
0
Comment
  • 7
  • 6
  • 3
16 Comments
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9898282
I normally do it like this:

DataSet GridDS = BuildGridDataSet(); //This method fills the table with data.
dataGrid1.SetDataBinding(GridDS, "TableName");

I normally build my dataset before binding, as you can see.                  

If that doesn't work, look at your dataset.  Maybe something strange is going on.  From what you have said, it looks like you might have a parent/child relationship going in there.

I use a method to print all table contents to the console, which helps in debugging (unless your table has too many rows).

Of course, you can always set your tablestyle and columstyles to get exactly what you want in your datagrid.

Good luck!
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9898636
Do you mean you put your OleDbDataAdapter and Fill in your method and return the dataset?

               selectCMD = new OleDbCommand(_mysqlstr,myOledbConn);
               myitemDA = new OleDbDataAdapter(selectCMD);
               myitemDS = new DataSet();
When I change the code I many times get:
Additional information: Cannot create a child list for field item.

My code id now:

                  string _myConnStr = "Data Source=eb.mdb;Provider=Microsoft.Jet.OLEDB.4.0;";
                  string _mysqlstr = "SELECT caption, url FROM item";
                        try
                        {
                              myOledbConn = new OleDbConnection(_myConnStr);
                              myOledbConn.Open();
                              Console.WriteLine("\nmyOledbConn Open...");
                        }
                        catch(Exception ex)
                        {
                              MessageBox.Show("Error: Failed to create a database connection. \n{0}" + ex.Message);
                              Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
                              return;
                        }

                  myitemDA = new OleDbDataAdapter(_mysqlstr,myOledbConn);
                  myitemDS = new DataSet();
                  myitemDA.Fill(myitemDS);
                  dataGrid1.SetDataBinding(myitemDS, "item");
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9899994
You should set the DataMember to the name of the table, namely "ORDER", not "item"
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 4

Assisted Solution

by:Tonylmiller
Tonylmiller earned 100 total points
ID: 9904309
Good point.  I had assumed that "item" was the name of the table.  

It should look just like my example:

dataGrid1.SetDataBinding(GridDS, "TableName");

with the name of the dataset and then the name of the table in double quotes.
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9904408
item is the table name.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9904442
Based on your code it's ORDER, not item. Change it and see
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9904476
I mean your original code
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9904797
The only place where ORDER is written is the ORDER BY of the select statement.

Her is a new version of my code without any order by:

string _myConnStr = "Data Source=eb.mdb;Provider=Microsoft.Jet.OLEDB.4.0;";
                  string _mysqlstr = "SELECT caption, url FROM item";
                        try
                        {
                              myOledbConn = new OleDbConnection(_myConnStr);
                              myOledbConn.Open();
                              Console.WriteLine("\nmyOledbConn Open...");
                        }
                        catch(Exception ex)
                        {
                              MessageBox.Show("Error: Failed to create a database connection. \n{0}" + ex.Message);
                              Console.WriteLine("Error: Failed to create a database connection. \n{0}", ex.Message);
                              return;
                        }
                  myitemDA = new OleDbDataAdapter(_mysqlstr,myOledbConn);
                  myitemDS = new DataSet();
                  myitemDA.Fill(myitemDS);
                  dataGrid1.SetDataBinding(myitemDS, "item");// to change the Datasource and Dataqmember at run time.
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 150 total points
ID: 9904857
OK, sorry, my mistake :-((

By the way: you don't need to open or close the connection. The data adapter will do this alone. Try this code:

string _myConnStr = "Data Source=eb.mdb;Provider=Microsoft.Jet.OLEDB.4.0;";
string _mysqlstr = "SELECT caption, url FROM item";
myOledbConn = new OleDbConnection(_myConnStr);
myitemDA = new OleDbDataAdapter(_mysqlstr,myOledbConn);
myitemDS = new DataSet();
myitemDA.Fill(myitemDS);
dataGrid1.DataMember = myitemDS.Tables[0].TableName;
dataGrid1.DataSource = myitemDS;
0
 
LVL 4

Expert Comment

by:Tonylmiller
ID: 9904879
Not sure if it will create the "item" table for you in your dataset automatically.

Try this:

Create a dataset that contains the item table.  Let's say its called ItemDataSet.xsd

Then do myitemDS = new ItemDataSet(); //in place of your code.

Let me know if it works.  :-)
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9904941
TheAvenger your code works perfectly.

But, I don't undersand why... since it looks same a my code.

The only diff. is myitemDS.Tables[0].TableName;

Does that mean, the code might have a problem with "item"?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9904960
No, maybe because of the table name. Try to print the table name:

MessageBox.Show (myitemDS.Tables[0].TableName);

and have a look what it is
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9905027
Great idea...
With a very unpredictable result for me!

The table name is... "Table"!!!

I guest this is because the dataset is filled automatically?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9905041
Probably, yes :-(( I didn't know that
0
 

Author Comment

by:Yann de Champlain, PMP
ID: 9905137
I would like to give points to both of you.
I am not sure how to achieve that.
Do you know how?
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9905643
You should have a link called Split Points, below the messages I believe. Then you can decide to whom to give how many points and who is the accepted answer and who is assisted answer.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Angular - break the dataset in chunks? 7 51
Error in JQuery 5 54
use of unassigned local variable 'id' 6 40
Getting error in connectionstring with Excel. 30 35
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question