Results 1 to 8 of 8

Thread: [RESOLVED] Getting errors how to resolve it?

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    274

    Resolved [RESOLVED] Getting errors how to resolve it?

    I am creating a database project in VS2013 by following a video tutorial. But I don't know why I am getting these errors while the same code was working fine on that video tutorial.

    The following are the errors:

    Error 1 Value of type 'System.Data.SqlClient.SqlDataReader' cannot be converted to 'System.Data.SqlClient.SqlDataAdapter'.
    Error 2 'HasRows' is not a member of 'System.Data.SqlClient.SqlDataAdapter'.
    Error 3 'Read' is not a member of 'System.Data.SqlClient.SqlDataAdapter'.
    Error 4 Class 'System.Data.SqlClient.SqlDataAdapter' cannot be indexed because it has no default property.
    Error 5 Class 'System.Data.SqlClient.SqlDataAdapter' cannot be indexed because it has no default property.

    The tutorial link: http://www.youtube.com/watch?v=EwWzl7L9n-0

    Please help.

  2. #2
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,248

    Re: Getting errors how to resolve it?

    You need to show us the code that you have and let us know which lines in the code are generating which errors.
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  3. #3
    Frenzied Member dolot's Avatar
    Join Date
    Nov 2007
    Location
    Music city, U.S.A.
    Posts
    1,248

    Re: Getting errors how to resolve it?

    The first error is essentially a type mismatch error - you're trying to assign SqlDataReader to a SqlDataAdapter - and there's no conversion for that.

    The next two errors tell you that you're trying to invoke a method or access a property on an object, but that object doesn't have that method/property.

    The last two are caused by trying to access an indexed property of an object, but that object has no default property that is of an indexed type.
    I always add to the reputation of those whose post are helpful, and even occasionally to those whose posts aren't helpful but who obviously put forth a valiant effort. That is, when the system will allow it.
    My war with a browser-redirect trojan

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    274

    Re: Getting errors how to resolve it?

    Thanks for the response. The following is the code:
    Code:
    Imports System.Data.SqlClient
    Public Class Employees
        Dim cn As New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\VS2013\Documents\visual studio 2013\Projects\AMCON\AMCON\AMCON.mdf;Integrated Security=True")
        Dim cmd As New SqlCommand
        Dim dr As New SqlDataAdapter
        Private Sub Employees_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            cmd.Connection = cn
        End Sub
        Private Sub loadlistbox()
            ListBox1.Items.Clear()
            ListBox2.Items.Clear()
            cn.Open()
            cmd.CommandText = "Select EID, ENM from EMPLOYEES"
            dr = cmd.ExecuteReader()
            If dr.HasRows Then
                While (dr.Read())
                    ListBox1.Items.Add(dr("EID"))
                    ListBox2.Items.Add(dr("1"))
                End While
            End If
            cn.Close()
        End Sub
        Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
            Dim lb As New ListBox
            lb = sender
            If lb.SelectedIndex <> -1 Then
                ListBox1.SelectedIndex = lb.SelectedIndex
                ListBox2.SelectedIndex = lb.SelectedIndex
            End If
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            If txtEID.Text <> "" And txtENM.Text <> "" Then
                cn.Open()
                cmd.CommandText = "Insert into EMPLOYEES (EID,ENM) VALUES ('" & txtEID.Text & "','" & txtENM.Text & "')"
                cmd.ExecuteNonQuery()
                cn.Close()
                txtEID.Text = ""
                txtENM.Text = ""
                loadlistbox()
            End If
        End Sub
    End Class
    Last edited by VS2013; Dec 18th, 2014 at 03:53 PM.

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,232

    Re: Getting errors how to resolve it?

    Just change this:

    Dim dr As New SqlDataAdapter

    to this:

    Dim dr as SqlDataReader

    You don't even need the new, since you will get a new one when this line executes:

    dr = cmd.ExecuteReader()
    My usual boring signature: Nothing

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    274

    Re: Getting errors how to resolve it?

    I wrongly typed "
    Code:
    Dim dr As New SqlDataAdapter
    " instead of "
    Code:
    Dim dr As New SqlDataReader
    ". But Still I am getting the following error:

    Error 1 'System.Data.SqlClient.SqlDataReader.Friend Sub New(command As System.Data.SqlClient.SqlCommand, behavior As System.Data.CommandBehavior)' is not accessible in this context because it is 'Friend'. C:\Users\VS2013\documents\visual studio 2013\Projects\AMCON\AMCON\Employees.vb

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2014
    Posts
    274

    Re: Getting errors how to resolve it?

    As I removed "New" from "Dim dr As New SqlDataReader", it is working fine.

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    35,232

    Re: [RESOLVED] Getting errors how to resolve it?

    Yeah. You get a new datareader when you call ExecuteReader, so there is no need to ever create the datareader ahead of time, as you would just throw it out in favor of the new one when you eventually call ExecuteReader. The same cannot be said for DataAdapter, though.
    My usual boring signature: Nothing

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