Results 1 to 7 of 7

Thread: DataTable and Dataset error [* Resolved *] but why???

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331

    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

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331
    Can no one not help me with this problem.

    Steve
    steve

  3. #3
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    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?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331
    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
    steve

  5. #5
    Frenzied Member Fishcake's Avatar
    Join Date
    Feb 2001
    Location
    Derby, UK
    Posts
    1,092
    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:
    1. Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
    2.         'Dim cnnstr As String = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=southwind;Data Source=steves-pc\netsdk"
    3.         Dim sqlconn As New SqlConnection("********")
    4.         Dim cmd As New SqlCommand("Select * From Players", sqlconn)
    5.         Dim daCustomers As New SqlDataAdapter(cmd)
    6.         Dim dsCustomers As New DataSet
    7.  
    8.         daCustomers.Fill(dsCustomers, "Customers") 'No Error
    9.         DataGrid1.DataSource = dsCustomers.Tables("Customers")
    10.         DataGrid1.DataBind()
    11.  
    12.         'Clean up
    13.         dsCustomers.Dispose()
    14.         daCustomers.Dispose()
    15.     End Sub
    Sorry couldn't be of more use.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331
    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
    steve

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Dec 2001
    Posts
    1,331
    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
    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
  •  



Click Here to Expand Forum to Full Width