More Specific Error Messages???

Discussion in 'ASP .Net' started by David Lozzi, Dec 16, 2004.

  1. David Lozzi

    David Lozzi Guest

    Howdy,

    I'm doing some simple database reading and loading severl values into labels for readonly use. Here is my script:

    Dim selStoreID As String

    If StoreID.SelectedIndex > -1 Then
    selStoreID = StoreID.SelectedItem.Value
    'lblStoreID.Text = StoreID.SelectedItem.Text

    Dim sqlConn As String = Session("SQLConn")
    Dim dbConnection As IDbConnection = New SqlConnection(sqlConn)

    Dim queryString As String = "SELECT * FROM tblStores WHERE ID = " & selStoreID
    Dim dbCommand As IDbCommand = New SqlCommand
    dbCommand.CommandText = queryString
    dbCommand.Connection = dbConnection

    dbConnection.Open()
    Dim rec As IDataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection)
    If Not rec.Read() Then
    'error info
    Else
    btnSelect.Visible = True
    Dim strStoreID As String = rec("StoreNo")
    lblStoreID.Text = rec("StoreNo")
    If Not rec("address1") Is System.DBNull.Value Then
    lblAddress1.Text = rec("Address1")
    End If
    If Not rec("address2") Is System.DBNull.Value Then
    lblAddress2.Text = rec("Address2")
    End If
    If Not rec("city") Is System.DBNull.Value Then
    lblCity.Text = rec("City")
    End If
    If Not rec("state") Is System.DBNull.Value Then
    lblState.Text = rec("state")
    End If
    If Not rec("zip") Is System.DBNull.Value Then
    lblZip.Text = rec("zip")
    End If
    If Not rec("phone") Is System.DBNull.Value Then
    If Len(rec("phone")) = 10 Then
    Dim fon As String = rec("phone")
    lblPhone.Text = "(" & Left(fon, 3) & ") " & Mid(fon, 4, 3) & "-" & Right(fon, 4)
    End If
    End If
    End If
    Else
    lblStoreID.Text = "None"
    btnSelect.Visible = False
    End If

    Now, the error I get is "Cast from type 'DBNull' to type 'String' is not valid", but it doesn't tell me what line! How can I figure that out? I was getting this error before until I added the check for the DBNull.Value for each field, but there is on record where I still receive this error. Here's the data in the record. Other records work fine!!

    ID StoreNo Address1 Address2 City State Zip Phone CompanyID
    43294 7068 4732 DEVINE ST COLUMBIA SC 0 4

    So, address2 and zip are <NULL> in SQL and Phone is 0, but I believe I am catching that too??

    Thanks!!!!

    --
    David Lozzi
    Web Applications/Network Specialist
    Delphi Technology Solutions, Inc.
    dlozzi(remove-this)@delphi-ts.com
     
    David Lozzi, Dec 16, 2004
    #1
    1. Advertising

  2. Put a breakpoint and see what the debugger says. Or put a print statement in the code outputting the IDs for each item, then when the error occurs, you know it is the most recent to be outputted.
    "David Lozzi" <dlozzi(remove-this)@delphi-ts.com> wrote in message news:...
    Howdy,

    I'm doing some simple database reading and loading severl values into labels for readonly use. Here is my script:

    Dim selStoreID As String

    If StoreID.SelectedIndex > -1 Then
    selStoreID = StoreID.SelectedItem.Value
    'lblStoreID.Text = StoreID.SelectedItem.Text

    Dim sqlConn As String = Session("SQLConn")
    Dim dbConnection As IDbConnection = New SqlConnection(sqlConn)

    Dim queryString As String = "SELECT * FROM tblStores WHERE ID = " & selStoreID
    Dim dbCommand As IDbCommand = New SqlCommand
    dbCommand.CommandText = queryString
    dbCommand.Connection = dbConnection

    dbConnection.Open()
    Dim rec As IDataReader = dbCommand.ExecuteReader(CommandBehavior.CloseConnection)
    If Not rec.Read() Then
    'error info
    Else
    btnSelect.Visible = True
    Dim strStoreID As String = rec("StoreNo")
    lblStoreID.Text = rec("StoreNo")
    If Not rec("address1") Is System.DBNull.Value Then
    lblAddress1.Text = rec("Address1")
    End If
    If Not rec("address2") Is System.DBNull.Value Then
    lblAddress2.Text = rec("Address2")
    End If
    If Not rec("city") Is System.DBNull.Value Then
    lblCity.Text = rec("City")
    End If
    If Not rec("state") Is System.DBNull.Value Then
    lblState.Text = rec("state")
    End If
    If Not rec("zip") Is System.DBNull.Value Then
    lblZip.Text = rec("zip")
    End If
    If Not rec("phone") Is System.DBNull.Value Then
    If Len(rec("phone")) = 10 Then
    Dim fon As String = rec("phone")
    lblPhone.Text = "(" & Left(fon, 3) & ") " & Mid(fon, 4, 3) & "-" & Right(fon, 4)
    End If
    End If
    End If
    Else
    lblStoreID.Text = "None"
    btnSelect.Visible = False
    End If

    Now, the error I get is "Cast from type 'DBNull' to type 'String' is not valid", but it doesn't tell me what line! How can I figure that out? I was getting this error before until I added the check for the DBNull.Value for each field, but there is on record where I still receive this error. Here's the data in the record. Other records work fine!!

    ID StoreNo Address1 Address2 City State Zip Phone CompanyID
    43294 7068 4732 DEVINE ST COLUMBIA SC 0 4

    So, address2 and zip are <NULL> in SQL and Phone is 0, but I believe I am catching that too??

    Thanks!!!!

    --
    David Lozzi
    Web Applications/Network Specialist
    Delphi Technology Solutions, Inc.
    dlozzi(remove-this)@delphi-ts.com
     
    Peter Rilling, Dec 16, 2004
    #2
    1. Advertising

  3. David Lozzi

    CMA Guest

    hi,

    my suggestion is the error in all checking lines with the DBNull.. so try to
    correct like this.

    wrong: If Not rec("zip") Is System.DBNull.Value Then
    correct: If Not rec("zip") Is System.DBNull Then


    the problem is when you take value.. it is a string type. but rec("zip")
    returns a DBNull.

    hope this helps,
    CMA


    "David Lozzi" <dlozzi(remove-this)@delphi-ts.com> wrote in message
    news:...
    Howdy,

    I'm doing some simple database reading and loading severl values into labels
    for readonly use. Here is my script:

    Dim selStoreID As String

    If StoreID.SelectedIndex > -1 Then
    selStoreID = StoreID.SelectedItem.Value
    'lblStoreID.Text = StoreID.SelectedItem.Text

    Dim sqlConn As String = Session("SQLConn")
    Dim dbConnection As IDbConnection = New SqlConnection(sqlConn)

    Dim queryString As String = "SELECT * FROM tblStores WHERE ID =
    " & selStoreID
    Dim dbCommand As IDbCommand = New SqlCommand
    dbCommand.CommandText = queryString
    dbCommand.Connection = dbConnection

    dbConnection.Open()
    Dim rec As IDataReader =
    dbCommand.ExecuteReader(CommandBehavior.CloseConnection)
    If Not rec.Read() Then
    'error info
    Else
    btnSelect.Visible = True
    Dim strStoreID As String = rec("StoreNo")
    lblStoreID.Text = rec("StoreNo")
    If Not rec("address1") Is System.DBNull.Value Then
    lblAddress1.Text = rec("Address1")
    End If
    If Not rec("address2") Is System.DBNull.Value Then
    lblAddress2.Text = rec("Address2")
    End If
    If Not rec("city") Is System.DBNull.Value Then
    lblCity.Text = rec("City")
    End If
    If Not rec("state") Is System.DBNull.Value Then
    lblState.Text = rec("state")
    End If
    If Not rec("zip") Is System.DBNull.Value Then
    lblZip.Text = rec("zip")
    End If
    If Not rec("phone") Is System.DBNull.Value Then
    If Len(rec("phone")) = 10 Then
    Dim fon As String = rec("phone")
    lblPhone.Text = "(" & Left(fon, 3) & ") " & Mid(fon,
    4, 3) & "-" & Right(fon, 4)
    End If
    End If
    End If
    Else
    lblStoreID.Text = "None"
    btnSelect.Visible = False
    End If

    Now, the error I get is "Cast from type 'DBNull' to type 'String' is not
    valid", but it doesn't tell me what line! How can I figure that out? I was
    getting this error before until I added the check for the DBNull.Value for
    each field, but there is on record where I still receive this error. Here's
    the data in the record. Other records work fine!!

    ID StoreNo Address1 Address2 City
    State Zip Phone CompanyID
    43294 7068 4732 DEVINE ST COLUMBIA SC
    0 4

    So, address2 and zip are <NULL> in SQL and Phone is 0, but I believe I am
    catching that too??

    Thanks!!!!

    --
    David Lozzi
    Web Applications/Network Specialist
    Delphi Technology Solutions, Inc.
    dlozzi(remove-this)@delphi-ts.com
     
    CMA, Dec 16, 2004
    #3
  4. Did that work for you David!!
    Patrick



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Patrick Olurotimi Ige, Dec 16, 2004
    #4
  5. David Lozzi

    David Lozzi Guest

    No such luck. VS.NET throws a blue line under it stating 'DBNull' is a type
    in 'System' and cannot be used as an expression.

    Here's another thing that is weird, is that this script is working fine this
    morning. I didn't change a thing from last night! Lets see if it remains!

    Thanks

    --
    David Lozzi
    Web Applications/Network Specialist
    Delphi Technology Solutions, Inc.
    dlozzi(remove-this)@delphi-ts.com


    "CMA" <> wrote in message
    news:%...
    > hi,
    >
    > my suggestion is the error in all checking lines with the DBNull.. so try
    > to
    > correct like this.
    >
    > wrong: If Not rec("zip") Is System.DBNull.Value Then
    > correct: If Not rec("zip") Is System.DBNull Then
    >
    >
    > the problem is when you take value.. it is a string type. but rec("zip")
    > returns a DBNull.
    >
    > hope this helps,
    > CMA
    >
    >
    > "David Lozzi" <dlozzi(remove-this)@delphi-ts.com> wrote in message
    > news:...
    > Howdy,
    >
    > I'm doing some simple database reading and loading severl values into
    > labels
    > for readonly use. Here is my script:
    >
    > Dim selStoreID As String
    >
    > If StoreID.SelectedIndex > -1 Then
    > selStoreID = StoreID.SelectedItem.Value
    > 'lblStoreID.Text = StoreID.SelectedItem.Text
    >
    > Dim sqlConn As String = Session("SQLConn")
    > Dim dbConnection As IDbConnection = New SqlConnection(sqlConn)
    >
    > Dim queryString As String = "SELECT * FROM tblStores WHERE ID =
    > " & selStoreID
    > Dim dbCommand As IDbCommand = New SqlCommand
    > dbCommand.CommandText = queryString
    > dbCommand.Connection = dbConnection
    >
    > dbConnection.Open()
    > Dim rec As IDataReader =
    > dbCommand.ExecuteReader(CommandBehavior.CloseConnection)
    > If Not rec.Read() Then
    > 'error info
    > Else
    > btnSelect.Visible = True
    > Dim strStoreID As String = rec("StoreNo")
    > lblStoreID.Text = rec("StoreNo")
    > If Not rec("address1") Is System.DBNull.Value Then
    > lblAddress1.Text = rec("Address1")
    > End If
    > If Not rec("address2") Is System.DBNull.Value Then
    > lblAddress2.Text = rec("Address2")
    > End If
    > If Not rec("city") Is System.DBNull.Value Then
    > lblCity.Text = rec("City")
    > End If
    > If Not rec("state") Is System.DBNull.Value Then
    > lblState.Text = rec("state")
    > End If
    > If Not rec("zip") Is System.DBNull.Value Then
    > lblZip.Text = rec("zip")
    > End If
    > If Not rec("phone") Is System.DBNull.Value Then
    > If Len(rec("phone")) = 10 Then
    > Dim fon As String = rec("phone")
    > lblPhone.Text = "(" & Left(fon, 3) & ") " &
    > Mid(fon,
    > 4, 3) & "-" & Right(fon, 4)
    > End If
    > End If
    > End If
    > Else
    > lblStoreID.Text = "None"
    > btnSelect.Visible = False
    > End If
    >
    > Now, the error I get is "Cast from type 'DBNull' to type 'String' is not
    > valid", but it doesn't tell me what line! How can I figure that out? I was
    > getting this error before until I added the check for the DBNull.Value for
    > each field, but there is on record where I still receive this error.
    > Here's
    > the data in the record. Other records work fine!!
    >
    > ID StoreNo Address1 Address2 City
    > State Zip Phone CompanyID
    > 43294 7068 4732 DEVINE ST COLUMBIA SC
    > 0 4
    >
    > So, address2 and zip are <NULL> in SQL and Phone is 0, but I believe I am
    > catching that too??
    >
    > Thanks!!!!
    >
    > --
    > David Lozzi
    > Web Applications/Network Specialist
    > Delphi Technology Solutions, Inc.
    > dlozzi(remove-this)@delphi-ts.com
    >
    >
    >
     
    David Lozzi, Dec 16, 2004
    #5
  6. Well David some magic do happen sometimes OVERNIGHT:)
    Eazy..
    Patrick



    *** Sent via Developersdex http://www.developersdex.com ***
    Don't just participate in USENET...get rewarded for it!
     
    Patrick Olurotimi Ige, Dec 16, 2004
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. zorhel
    Replies:
    5
    Views:
    597
    Marina
    Aug 24, 2004
  2. Kenneth McDonald
    Replies:
    1
    Views:
    377
    Peter Hansen
    Jun 16, 2004
  3. Michael
    Replies:
    4
    Views:
    418
    Matt Hammond
    Jun 26, 2006
  4. dfighter
    Replies:
    0
    Views:
    640
    dfighter
    Apr 19, 2009
  5. Robert Klemme

    With a Ruby Yell: more, more more!

    Robert Klemme, Sep 28, 2005, in forum: Ruby
    Replies:
    5
    Views:
    218
    Jeff Wood
    Sep 29, 2005
Loading...

Share This Page