简体   繁体   中英

how to bind datagridview to database at page load time in vb.net

I use below code in try catch block but it gives exception" 'table' argument cannot be null. Parameter name: table " .My table name is caste and that table two columns are there srno and castename .But it say that my table has no data.Memory table is a datatable.

 Dim Dset As New DataSet()
            Dset = New DataSet()
            Dset.Tables.Add(MemoryTable)
            DataGridView1.DataSource = Dset.Tables("caste")

I tried data connect with database with using datasource but it gives service pack 1 error 'One is to use data binding on your TextBox controls and assigning the same DataSource.but its gives error

You don't show the definition of MemoryTable , but you do say it's a data table. If it's an object of type System.Data.DataTable , then it will have a property called TableName .

When you access a DataTable in a DataSet with a string index value, the value you are passing is the table's TableName property. So Dset.Tables("caste") is looking for a DataTable whose TableName property is set to "caste". If it can't find one, it will return Null . That looks like what's happening.

So set MemoryTable.TableName to "caste" and the error may go away.

I assume that MemoryTable actually has rows in it? If not, that may be a reason why you're getting the message about your table having no data.

So your code should look something like this:

Dim Dset As New DataSet()  ' You don't have to do a separate assignment to Dset
                           ' if you use New in the declaration, so we can omit that line.'
MemoryTable.TableName = "caste"
Dset.Tables.Add(MemoryTable)
DataGridView1.DataSource = Dset.Tables("caste")

And, actually, you can use MemoryTable as your data source without having to add it to a DataSet , unless you need to for some other reason.

DataGridView1.DataSource = MemoryTable

I hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM