dennisdominic
asked on
Populating DataTable from SQL
I've been looking around for a tutorial on how to use DataTable.
Below is the code I use to retrieve CustomerName, using DataReader.
How can I convert it to read the data into a DataTable instead?
Below is the code I use to retrieve CustomerName, using DataReader.
How can I convert it to read the data into a DataTable instead?
Dim strConnection As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("myDB").ConnectionString
Dim objConnection = New OleDbConnection(strConnection)
objConnection.Open()
Dim objCommand as OleDbCommand
Dim objDataReader As OleDbDataReader
Dim strSQL as String = "SELECT * FROM Customer"
objCommand = New OleDbCommand(strSQL, objConnection)
objDataReader = objCommand.ExecuteReader(CommandBehavior.Default)
While objDataReader.Read()
Response.Write(objDataReader("CustomerName") & "</ br>")
End While
objDataReader.Close()
objConnection.Close()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
jinal,
I get this Error on line 13, the Load command. I've checked that the DataReader works. What did I do wrong? I'm more interested in this method as it is much shorter, even though Dhaest's method works.
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
I get this Error on line 13, the Load command. I've checked that the DataReader works. What did I do wrong? I'm more interested in this method as it is much shorter, even though Dhaest's method works.
System.NullReferenceExcept
I tried this at my end it work fine.
Just try to select one column from in Select statement.
Like Select name from Customer from your side and test.
Just try to select one column from in Select statement.
Like Select name from Customer from your side and test.
Dim strConnection As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("myDB").ConnectionString
Dim objConnection = New OleDbConnection(strConnection)
objConnection.Open()
Dim objCommand As OleDbCommand
Dim objDataReader As OleDbDataReader
Dim strSQL As String = "SELECT * FROM Customers"
objCommand = New OleDbCommand(strSQL, objConnection)
objDataReader = objCommand.ExecuteReader(CommandBehavior.Default)
Dim dt As New DataTable("Customers")
dt.Load(objDataReader)
objDataReader.Close()
objConnection.Close()
Dim strConnection As String = System.Web.Configuration.WebConfigurationManager.ConnectionStrings("myDB").ConnectionString
Dim objConnection = New OleDbConnection(strConnection)
objConnection.Open()
Dim objCommand as OleDbCommand
Dim objDataReader As OleDbDataReader
Dim strSQL as String = "SELECT * FROM Customer"
objCommand = New OleDbCommand(strSQL, objConnection)
objDataReader = objCommand.ExecuteReader()
Dim dt as new DataTable("Customers")
dt.Load(objDataReader)
objDataReader.Close()
objConnection.Close()
Try with previous two comments of mine.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Hello Dhaest,
I already declared datatable this way.
Dim dt as new DataTable("Customers")
I already declared datatable this way.
Dim dt as new DataTable("Customers")
ASKER
Oops.. I gave points to the wrong guy.
I'm supposed to give 400 to jinal instead.. how can I rectify this?
I'm supposed to give 400 to jinal instead.. how can I rectify this?
Hello Dennisdemonc ,
Assisted solution is same as mine. My previous comment is same as Dhaest. Can you please verify it ?
Assisted solution is same as mine. My previous comment is same as Dhaest. Can you please verify it ?
Can you please assign some point to me ?
ASKER
I'm checking with the moderators..
I'm so sorry.
I'm so sorry.
I will look back at it evening . Try to contact moderator or admin . They will help you out in this case.
ASKER
The original code by jinal works, but Dhaest was correct to point out that I declared the datatable wrongly.
I used
Dim dt as DataTable
instead of
Dim dt as new DataTable("Customers")
I used
Dim dt as DataTable
instead of
Dim dt as new DataTable("Customers")
Dim strSQL as String = "SELECT * FROM Customer"
objCommand = New OleDbCommand(strSQL, objConnection)
Dim dtCustomer = GetDataTable(objCommand)
Open in new window