|
-
Apr 29th, 2004, 04:21 AM
#1
Thread Starter
Frenzied Member
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
Last edited by steve_rm; Apr 30th, 2004 at 07:13 AM.
steve
-
Apr 29th, 2004, 04:36 PM
#2
Thread Starter
Frenzied Member
Can no one not help me with this problem.
Steve
-
Apr 29th, 2004, 05:16 PM
#3
What errors do you get exactly?
Also you say there's an error with the line
Code:
Dim dtCustomers As New DataTable("Customers") 'error here
I can't see how there's an error there any more details?
-
Apr 29th, 2004, 05:27 PM
#4
Thread Starter
Frenzied Member
Thanks for your reply fishcake.
I have done everything, but still getting the same error. The error says "Value of type chapter8.dataset cannot be converted to system.data.sqlclient.sqlconnection". Well Chapter8 is the name of my project. Could there be a problem here
Code:
imports system.data
imports system.data.sqlclient
Dim cnnstr As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=southwind;Data Source=steves-pc\netsdk"
Dim sqlconn As New SqlConnection(cnnstr)
Dim cmd As New SqlCommand("Select * From Customers", sqlconn)
Dim daCustomers As New SqlDataAdapter(cmd)
Dim dsCustomers As New DataSet()
daCustomers.Fill(dsCustomers, "Customers") 'Error - Value of type chapter8.DataSet cannot be converted to System.data.dataset
Thanks for you help.
Steve
-
Apr 29th, 2004, 07:46 PM
#5
I can't see whats wrong with that last snippet, i even just cut and pasted it (changing database connection of course) and it worked fine from here, as follows..
VB Code:
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Dim cnnstr As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=southwind;Data Source=steves-pc\netsdk"
Dim sqlconn As New SqlConnection("********")
Dim cmd As New SqlCommand("Select * From Players", sqlconn)
Dim daCustomers As New SqlDataAdapter(cmd)
Dim dsCustomers As New DataSet
daCustomers.Fill(dsCustomers, "Customers") 'No Error
DataGrid1.DataSource = dsCustomers.Tables("Customers")
DataGrid1.DataBind()
'Clean up
dsCustomers.Dispose()
daCustomers.Dispose()
End Sub
Sorry couldn't be of more use.
-
Apr 30th, 2004, 04:00 AM
#6
Thread Starter
Frenzied Member
Hello,
Could the problem be something to do with the way the project is set up.
If l remove the imports system.data.sqlClient
l don't get the error.
This is very strange, hope someone help me.
Thanks for your time.
Steve
-
Apr 30th, 2004, 07:05 AM
#7
Thread Starter
Frenzied Member
Hello,
I started a new vb ASP project, and did the same for the dataset and datatable. Both are working now. But l don't know what the problem was before, with my original project.
code for dataset
Code:
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", cnn)
Dim dsCustomers As New DataSet("Customers")
'Execute
daCustomers.Fill(dsCustomers, "Customers")
DataGrid1.DataSource = dsCustomers
DataGrid1.DataBind()
'Clean-up
daCustomers.Dispose()
dsCustomers.Dispose()
code for the datatable
Code:
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("My Table")
'Execute
daCustomers.Fill(dtCustomers)
DataGrid1.DataSource = dtCustomers
DataBind()
'Clean-up
dtCustomers.Dispose()
daCustomers.Dispose()
I still think it has something to do with how the original project was setup. But not sure. If you have any ideas can you tell me.
Thanks,
Steve
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|