?
Solved

How do I fix "Public member 'Length' on type 'DBNull' not found" Excel Import Error in .NET?

Posted on 2008-10-21
1
Medium Priority
?
1,330 Views
Last Modified: 2011-10-19
How do I fix "Public member 'Length' on type 'DBNull' not found" Excel Import Error in .NET?
I have attached the file I am attempting to import and the code behind.  The code works, I have successfully used it on another file, but why not this one?

Any help is greatly appreciated.

What am I missing?
Partial Class crc_importcertificates
    Inherits System.Web.UI.Page
 
    Protected Sub btImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btImport.Click
        If FileUpload.HasFile Then
            Dim strFileName As String = ""
            Dim oleDbCommand As New Data.OleDb.OleDbCommand
            Dim oleDbReader As Data.OleDb.OleDbDataReader
            Dim sqlCommand As New Data.SqlClient.SqlCommand("crc_ImportCertificates", New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("cnDWS").ConnectionString))
 
            sqlCommand.CommandType = Data.CommandType.StoredProcedure
 
            'Save File to random filename
            Randomize()
            strFileName = ConfigurationManager.AppSettings("FileUploadPath").ToString & CStr(Rnd()) & ".xls"
            If Not System.IO.File.Exists(strFileName) Then
                FileUpload.SaveAs(strFileName)
            End If
 
            oleDbCommand.Connection = New Data.OleDb.OleDbConnection(Replace(ConfigurationManager.AppSettings("cnACT").ToString, "FILENAME", strFileName))
            oleDbCommand.CommandText = "SELECT * FROM [ExportCertificateData$];"
 
            oleDbCommand.Connection.Open()
            sqlCommand.Connection.Open()
 
            oleDbReader = oleDbCommand.ExecuteReader
            sqlCommand.Parameters.Add("@first_name", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@last_name", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@address", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@city", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@state", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@zip", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@username", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@password", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@certificate_level", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@mathematics_level", Data.SqlDbType.TinyInt)
            sqlCommand.Parameters.Add("@mathematics_tested", Data.SqlDbType.DateTime)
            sqlCommand.Parameters.Add("@reading_level", Data.SqlDbType.TinyInt)
            sqlCommand.Parameters.Add("@reading_tested", Data.SqlDbType.DateTime)
            sqlCommand.Parameters.Add("@locating_level", Data.SqlDbType.TinyInt)
            sqlCommand.Parameters.Add("@locating_tested", Data.SqlDbType.DateTime)
            sqlCommand.Parameters.Add("@certificate_issued", Data.SqlDbType.DateTime)
            sqlCommand.Parameters.Add("@expiration", Data.SqlDbType.DateTime)
            sqlCommand.Parameters.Add("@testing_location", Data.SqlDbType.VarChar)
            sqlCommand.Parameters.Add("@act_certificate_number", Data.SqlDbType.VarChar)
 
            'loop through records and update crc_certificates based on results
            While oleDbReader.Read()
                sqlCommand.Parameters("@first_name").Value = IIf(oleDbReader("First Name").Length = 0, System.DBNull.Value, oleDbReader("First Name"))
                sqlCommand.Parameters("@last_name").Value = IIf(oleDbReader("Last Name").Length = 0, System.DBNull.Value, oleDbReader("Last Name"))
                sqlCommand.Parameters("@address").Value = IIf(oleDbReader("Primary Address1").Length = 0, System.DBNull.Value, oleDbReader("Primary Address1"))
                sqlCommand.Parameters("@address").Value &= IIf(oleDbReader("Primary Address2").Length = 0, System.DBNull.Value, oleDbReader("Primary Address2"))
                sqlCommand.Parameters("@address").Value &= IIf(oleDbReader("Primary Address3").Length = 0, System.DBNull.Value, oleDbReader("Primary Address3"))
                sqlCommand.Parameters("@city").Value = IIf(oleDbReader("Primary City").Length = 0, System.DBNull.Value, oleDbReader("Primary City"))
                sqlCommand.Parameters("@state").Value = IIf(oleDbReader("Primary State").Length = 0, System.DBNull.Value, oleDbReader("Primary State"))
                sqlCommand.Parameters("@zip").Value = IIf(oleDbReader("Primary PostalCode/ZipCode").Length = 0, System.DBNull.Value, oleDbReader("Primary PostalCode/ZipCode"))
                sqlCommand.Parameters("@username").Value = oleDbReader("MyWorkKeys UserName")
                sqlCommand.Parameters("@password").Value = oleDbReader("Password")
                sqlCommand.Parameters("@certificate_level").Value = oleDbReader("Certificate Level")
                sqlCommand.Parameters("@mathematics_level").Value = oleDbReader("Applied Mathematics Score")
                sqlCommand.Parameters("@mathematics_tested").Value = oleDbReader("Applied Mathematics Test Date")
                sqlCommand.Parameters("@reading_level").Value = oleDbReader("Reading for Information Score")
                sqlCommand.Parameters("@reading_tested").Value = oleDbReader("Reading for Information Test Date")
                sqlCommand.Parameters("@locating_level").Value = oleDbReader("Locating Information Score")
                sqlCommand.Parameters("@locating_tested").Value = oleDbReader("Locating Information Test Date")
                sqlCommand.Parameters("@certificate_issued").Value = oleDbReader("Certificate Issue Date")
                sqlCommand.Parameters("@expiration").Value = oleDbReader("Certificate Expiration Date")
                sqlCommand.Parameters("@testing_location").Value = oleDbReader("Last Known Testing Location")
                sqlCommand.Parameters("@act_certificate_number").Value = oleDbReader("Certificate Identifier")
                sqlCommand.ExecuteNonQuery()
            End While
 
            sqlCommand.Connection.Close()
            oleDbCommand.Connection.Close()
 
            'clean up
            System.IO.File.Delete(strFileName)
            lblStatus.Visible = True
        End If
    End Sub
 
    Protected Sub btReturn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btReturn.Click
        Response.Redirect("default.aspx", True)
    End Sub
End Class

Open in new window

test.xls
0
Comment
Question by:devnewbee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 18

Accepted Solution

by:
UnifiedIS earned 2000 total points
ID: 22769483
You can also usually just bypass the null checks with empty strings
sqlCommand.Parameters("@address") = string.empty & oleDbReader("Primary Address1")

Another way would be to reverse your iiif logic
if isdbnull(oledbreader(fieldname)) then dbnull.value else oledbreader(fieldname)

0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question