Db Connection and DataGrids

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adz1809
    New Member
    • Dec 2006
    • 27

    Db Connection and DataGrids

    Hi all,

    I'm have issues with my DB connection and the use of DataGrids.

    My DB connection is through my web.config file and the sub looks as follows:

    [HTML]sub OpenDatabase()

    sysConn = ConfigurationSe ttings.AppSetti ngs("SystemConn ection")

    sysDatabase = server.createob ject("ADODB.con nection")
    sysDatabase.ope n(sysConn)

    end sub[/HTML]

    This me sub for my DataGrid:

    [HTML]Sub BindData()

    sysConn = ConfigurationSe ttings.AppSetti ngs("SystemConn ection")

    objCmd = New OleDbCommand("S ELECT top 10 * FROM tbl_ACCO_EU_Con tacts", sysDatabase)

    sysDatabase = server.createob ject("ADODB.con nection")

    sysDatabase.ope n(sysConn)

    objRdr = objCmd.ExecuteR eader()

    dbcontacts.Data Source = objRdr

    dbcontacts.Data Bind()

    objRdr.Close()

    End Sub[/HTML]

    Where am I going wrong?

    Cheers
    adz
  • dip_developer
    Recognized Expert Contributor
    • Aug 2006
    • 648

    #2
    Originally posted by adz1809
    Hi all,

    I'm have issues with my DB connection and the use of DataGrids.

    My DB connection is through my web.config file and the sub looks as follows:

    [HTML]sub OpenDatabase()

    sysConn = ConfigurationSe ttings.AppSetti ngs("SystemConn ection")

    sysDatabase = server.createob ject("ADODB.con nection")
    sysDatabase.ope n(sysConn)

    end sub[/HTML]

    This me sub for my DataGrid:

    [HTML]Sub BindData()

    sysConn = ConfigurationSe ttings.AppSetti ngs("SystemConn ection")

    objCmd = New OleDbCommand("S ELECT top 10 * FROM tbl_ACCO_EU_Con tacts", sysDatabase)

    sysDatabase = server.createob ject("ADODB.con nection")

    sysDatabase.ope n(sysConn)

    objRdr = objCmd.ExecuteR eader()

    dbcontacts.Data Source = objRdr

    dbcontacts.Data Bind()

    objRdr.Close()

    End Sub[/HTML]

    Where am I going wrong?

    Cheers
    adz
    are you creating the command object before opening the connection..?? as your code suggests....... ........

    I think no problem in the code otherwise if connection and other stuffs are ok.

    Comment

    • adz1809
      New Member
      • Dec 2006
      • 27

      #3
      Originally posted by dip_developer
      are you creating the command object before opening the connection..?? as your code suggests....... ........

      I think no problem in the code otherwise if connection and other stuffs are ok.
      This is the code at the moment:

      [HTML]Sub BindData()

      opendatabase()

      objCmd = New OleDbCommand("S ELECT top 10 * FROM tbl_ACCO_EU_Con tacts", sysConn )

      objRdr = objCmd.ExecuteR eader()

      dbcontacts.Data Source = objRdr

      dbcontacts.Data Bind()

      objRdr.Close()

      End Sub[/HTML]

      And I'm getting this error:

      Server Error in '/AccoEndUser' Application.
      --------------------------------------------------------------------------------

      Specified cast is not valid.
      Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

      [HTML]Exception Details: System.InvalidC astException: Specified cast is not valid.

      Source Error:


      Line 55: opendatabase()
      Line 56:
      Line 57: objCmd = New OleDbCommand("S ELECT top 10 * FROM tbl_ACCO_EU_Con tacts", sysConn )
      Line 58:
      Line 59: objRdr = objCmd.ExecuteR eader()


      Source File: C:\Websites\Acc o_EndUser\recor d_srch.aspx Line: 57[/HTML]

      Any help geratly needed.

      Cheers

      Adz

      Comment

      • adz1809
        New Member
        • Dec 2006
        • 27

        #4
        Originally posted by adz1809
        This is the code at the moment:

        [HTML]Sub BindData()

        opendatabase()

        objCmd = New OleDbCommand("S ELECT top 10 * FROM tbl_ACCO_EU_Con tacts", sysConn )

        objRdr = objCmd.ExecuteR eader()

        dbcontacts.Data Source = objRdr

        dbcontacts.Data Bind()

        objRdr.Close()

        End Sub[/HTML]

        And I'm getting this error:

        Server Error in '/AccoEndUser' Application.
        --------------------------------------------------------------------------------

        Specified cast is not valid.
        Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

        [HTML]Exception Details: System.InvalidC astException: Specified cast is not valid.

        Source Error:


        Line 55: opendatabase()
        Line 56:
        Line 57: objCmd = New OleDbCommand("S ELECT top 10 * FROM tbl_ACCO_EU_Con tacts", sysConn )
        Line 58:
        Line 59: objRdr = objCmd.ExecuteR eader()


        Source File: C:\Websites\Acc o_EndUser\recor d_srch.aspx Line: 57[/HTML]

        Any help geratly needed.

        Cheers

        Adz
        All sorted, here is the finished code:

        [HTML]Sub BindData()

        dim dsn As String = ConfigurationSe ttings.AppSetti ngs("SystemConn ection")

        dim objConn as New OleDbConnection (dsn)

        objCmd = New OleDbCommand("S ELECT top 10 * FROM tbl_ACCO_EU_Con tacts", objConn )

        objConn.open()

        objRdr = objCmd.ExecuteR eader()

        dbcontacts.Data Source = objRdr

        dbcontacts.Data Bind()

        objRdr.Close()

        End Sub[/HTML]

        Comment

        Working...