DataTable and Dataset error [* Resolved *] but why???
Hello,
I am practicing writing code using the dataset and datatable. But l keep getting these errors in the code. Can't understand what is going wrong. the code is listed below. I can sort of understand what the errors are, but not sure how to solve them. When l check the text book for the corrrect syntax, everything is corrrect
Code for using the datatable
Code:
Imports System.Data
Imports System.Data.SqlClient
Public Class MyCustomers
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Create objects
Dim cnnstr As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=southwind;Data Source=steves-pc\netsdk"
Dim daCustomers As New SqlDataAdapter("Select * From Customers", cnnstr)
Dim dtCustomers As New DataTable("Customers") 'error here
daCustomers.Fill(dtCustomers) 'error here
'Execute
DataGrid1.DataSource = dtCustomers
DataBind()
'Clean up
daCustomers.Dispose()
dtCustomers.Dispose()
End Sub
End Class
Code for using the dataset
Code:
Imports System.Data
Imports System.Data.SqlClient
Public Class MyCustomers
Inherits System.Web.UI.Page
Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
Protected WithEvents Button1 As System.Web.UI.WebControls.Button
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'Create objects
Dim cnnstr As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=southwind;Data Source=steves-pc\netsdk"
Dim cnn As New SqlConnection(cnnstr)
Dim daCustomers As New SqlDataAdapter("Select * From Customers", cnnstr)
Dim dsCustomers As New DataSet("NorthWind")'error here
'Execute
daCustomers.Fill(dsCustomers,"Customers")'Error here
DataGrid1.DataSource = dsCustomers.tables("Customers")
DataBind()
'Clean up
dsCustomers.Dispose()
daCustomers.Dispose()
End Sub
End Class
I hope someone can tell me what is wrong with this code.
Thanks for your time in helping me
Steve