10
Answers

Can Datareader be used instead of Data Adapter to populate the Datagrid

In Windows Forms application using vb.net / access - can datareader be used to populate the datagrid? The datagrid in my form is just for display purposes. Modifications to the data will be handled by double clicking the grid which would take to another form which handles insert /update / delete. I tried to populate the datagrid with a datareader: Dim sql As String = "SELECT * FROM Pcardlog where RDM='R'" Cmd.Connection = Conn Cmd.CommandText = sql Conn.Open() DataGrid1.DataSource = Cmd.ExecuteReader I get the following error: An unhandled exception of type 'System.Exception' occurred in system.windows.forms.dll Additional information: Complex DataBinding accepts as a data source either an IList or an IListSource Could someone throw some light on how to get this to work?

Answers (10)

0
Photo of rlehnherr
NA 2 0 20y
had troubles with the GetSchemaTable() here's how i got it to work: System.Data.OleDb.OleDbDataReader dr = comm.ExecuteReader(System.Data.CommandBehavior.CloseConnection); System.Data.DataTable dt = new DataTable(); for (int i=0; i
0
Photo of Carlos Sanchez
NA 264 0 20y
Tath's right, but we want to know how to populate a DataGrid from a DataReader. is it possible? thank you. regards.
0
Photo of nagenderk
NA 2 0 20y
Hi It is not DataBind(). It is somthig like this: dataGrid1.SetDataBinding(dsPersonel,"Personel"); dsPersonnel is Dataset and Personel is the table in the dataset
0
Photo of Carlos Sanchez
NA 264 0 20y
I don't know the two fifferent datagrids. I'm using the datagrid from the toolbar and it doesn't have the method DataBind(). can somebody gives a solution please
0
Photo of Adam Erstelle
NA 343 0 20y
can you please fully qualify the objects you are using, as there are two different datagrid objects. This will make it easier, as I also have this problem. Thanks!
0
Photo of p.jones
NA 124 0 20y
Instead of OleDbCommand etc try using SqlCommand etc.
0
Photo of Carlos Sanchez
NA 264 0 20y
CrendKing, the code that you wrote it doesn't work, may be because I'm usig a SQL Server connection. If you have the solution for SQL, I'd thank you.
0
Photo of crend_king
NA 18 0 21y
Try this code, it works in my computer, and it's simple :) (C# Code) conn is OleDbConnection, cmd is OleDbCommand, dr is OleDbDataReader, dg is DataGrid. conn.Open(); cmd = new OleDbCommand("SELECT * FROM vote", conn); // Use Constructor dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); dg.DataSource = dr; dg.DataBind();
0
Photo of spgilmore
NA 591 0 21y
This is really stupid. I say that there are two major flaws in .NET. First, there is no one-stop way to convert a char[] to a byte[], even though you can cast a char to a byte, you can't cast a char[] to a byte[]. The second is that you cannot assign a datareader to a dataset. Who cares if it's read only forward only while it's connected? Data is data and once it's disconnected, let the dataset move back and forth. It's supposed to be disconnected dataset-oriented, but it's still bound to this stupid connected cursor crap. GARBAGE! I have done this anyway. For the reasons I've described, this should be possible, and it is. You just have to do it manually. Create a dataset ds. Declare a datatable dt. Open your datareader dr. Execute this: DataTable dt = dr.GetSchemaTable; } Create an object array named rowdata. For each row in dr, get each column as an object and assign it to the corresponding element in rowdata. Then, add the rowdata to the table with dt.Rows.Add(rowdata); Finally, add the datatable to the dataset like this: ds.Tables.Add(dt); With the data in a dataset, you can bind the dataset to the grid.
0
Photo of dip_2004_05
NA 82 0 21y
Check out the MS article at : http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q313/4/82.asp&NoWebContent=1&NoWebContent=1 Quote: "Unlike ASP.NET Web Forms, the Windows Forms object model does not support binding to the DataReader object or the IEnumerable interface because backward scrolling support is required when you bind to anything other than a simple object." - dip