Connecting Access DB to VB .net DataGrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Sebastian

    Connecting Access DB to VB .net DataGrid

    I'm trying to connect a query I made in the Access
    database to populate a grid.I'm using the following code
    and I get a error:

    Specified cast is not valid.

    on the first line below. Any Ideas?


    Dim DataAdp As OleDbDataAdapte r = New OleDbDataAdapte r
    ("GetInvoice s", DBvar)
    DataAdp.SelectC ommand.CommandT ype =
    CommandType.Sto redProcedure
    If Not IsDBNull(DataAd p) Then

    Try
    DataAdp.Fill(ds , "Invoices")
    Catch ex As Exception
    MsgBox(ex.Messa ge,
    MsgBoxStyle.Cri tical, "Error:")
    End Try
    GrdInvoices.Dat aSource = dataset.Tables
    ("Invoices")
    End If

    ts.MappingName = dataset.Tables
    ("Invoices").Ta bleName
  • Armin Zingler

    #2
    Re: Connecting Access DB to VB .net DataGrid

    "Sebastian" <Sebastian@ilog ic.com> schrieb[color=blue]
    > I'm trying to connect a query I made in the Access
    > database to populate a grid.I'm using the following code
    > and I get a error:
    >
    > Specified cast is not valid.
    >
    > on the first line below. Any Ideas?
    >
    >
    > Dim DataAdp As OleDbDataAdapte r = New OleDbDataAdapte r
    > ("GetInvoice s", DBvar)[/color]

    I've no clue, but.. what is the type of DBVar? It must be a string or an
    System.Data.Ole Db.OleDbConnect ion.
    [color=blue]
    > DataAdp.SelectC ommand.CommandT ype =
    > CommandType.Sto redProcedure
    > If Not IsDBNull(DataAd p) Then[/color]

    Using IsDBNull on a DataAdapter doesn't make sense at all. A DataAdapter is
    an object reading and storing records from and into a database.

    [color=blue]
    > Try
    > DataAdp.Fill(ds , "Invoices")
    > Catch ex As Exception
    > MsgBox(ex.Messa ge,
    > MsgBoxStyle.Cri tical, "Error:")
    > End Try
    > GrdInvoices.Dat aSource = dataset.Tables
    > ("Invoices")[/color]

    You are reading into "ds", but attaching "dataset"(.tabl es)?
    [color=blue]
    > End If
    >
    > ts.MappingName = dataset.Tables
    > ("Invoices").Ta bleName[/color]


    --
    Armin

    How to quote and why:



    Comment

    • Sebastian

      #3
      Re: Connecting Access DB to VB .net DataGrid

      Actually I'm using ds everywere but I tryed to change it
      here to better describe what I was doing (I missed that
      last one). However I am useing a variable ds everywhere
      which is a type dataset.

      DBVar is global connection to the access database.Its used
      as follows.
      Public DBvar As New ADODB.Connectio n()
      DBvar.Open("Pro vider=Microsoft .Jet.OLEDB.4.0; Data
      Source=PalData. mdb")

      Is the connection the problem? How do I need to change it
      to get it to work with datagrids? Do I need any special
      references?

      Thanks,
      [color=blue]
      >-----Original Message-----
      >"Sebastian" <Sebastian@ilog ic.com> schrieb[color=green]
      >> I'm trying to connect a query I made in the Access
      >> database to populate a grid.I'm using the following code
      >> and I get a error:
      >>
      >> Specified cast is not valid.
      >>
      >> on the first line below. Any Ideas?
      >>
      >>
      >> Dim DataAdp As OleDbDataAdapte r = New OleDbDataAdapte r
      >> ("GetInvoice s", DBvar)[/color]
      >
      >I've no clue, but.. what is the type of DBVar? It must[/color]
      be a string or an[color=blue]
      >System.Data.Ol eDb.OleDbConnec tion.
      >[color=green]
      >> DataAdp.SelectC ommand.CommandT ype =
      >> CommandType.Sto redProcedure
      >> If Not IsDBNull(DataAd p) Then[/color]
      >
      >Using IsDBNull on a DataAdapter doesn't make sense at[/color]
      all. A DataAdapter is[color=blue]
      >an object reading and storing records from and into a[/color]
      database.[color=blue]
      >
      >[color=green]
      >> Try
      >> DataAdp.Fill(ds , "Invoices")
      >> Catch ex As Exception
      >> MsgBox(ex.Messa ge,
      >> MsgBoxStyle.Cri tical, "Error:")
      >> End Try
      >> GrdInvoices.Dat aSource = dataset.Tables
      >> ("Invoices")[/color]
      >
      >You are reading into "ds", but[/color]
      attaching "dataset"(.tabl es)?[color=blue]
      >[color=green]
      >> End If
      >>
      >> ts.MappingName = dataset.Tables
      >> ("Invoices").Ta bleName[/color]
      >
      >
      >--
      >Armin
      >
      >How to quote and why:
      >http://www.plig.net/nnq/nquote.html
      >http://www.netmeister.org/news/learn2quote.html
      >
      >.
      >[/color]

      Comment

      • Cor Ligthert

        #4
        Re: Connecting Access DB to VB .net DataGrid

        Hi Sebastian,

        The simplest way to test this is first remove that SP.
        Test it with a normal SQL txt string, when it than it working it is easy to
        change that again for an SP.

        The same as Armin wrote do I think that that DBnul dataadapter is the error

        What has a DBnull.value to do with a dataadapter?

        The commandpart can have no reference, however that is not a DBnull.value
        but a Nothing (while the dataadapter is Something).

        Cor


        Comment

        • Armin Zingler

          #5
          Re: Connecting Access DB to VB .net DataGrid

          "Sebastian" <Sebastian@ilog ic.com> schrieb[color=blue]
          > Actually I'm using ds everywere but I tryed to change it
          > here to better describe what I was doing (I missed that
          > last one). However I am useing a variable ds everywhere
          > which is a type dataset.
          >
          > DBVar is global connection to the access database.Its used
          > as follows.
          > Public DBvar As New ADODB.Connectio n()
          > DBvar.Open("Pro vider=Microsoft .Jet.OLEDB.4.0; Data
          > Source=PalData. mdb")
          >
          > Is the connection the problem? How do I need to change it
          > to get it to work with datagrids? Do I need any special
          > references?[/color]

          You are mixing two (totally) different technologies: ADO and ADO.Net. You
          are establishing an ADO connection, but you need an ADO.Net connection
          (System.Data.Ol eDb.OleDbConnec tion).


          --
          Armin

          How to quote and why:



          Comment

          • Cor Ligthert

            #6
            Re: Connecting Access DB to VB .net DataGrid

            Hi Sebastian,

            Armin saw that better than I however to fullfil your question

            dim DBVar as new
            OleDb.OleDbConn ection("Provide r=Microsoft.Jet .OLEDB.4.0;Data
            Source=\somepat h\mydb.mdb;User Id=admin;Passwo rd=;" )

            Cor


            Comment

            Working...