Advertisement

09.20.2007 at 01:50PM PDT, ID: 22842689
[x]
Attachment Details

Ensure excel spreadsheet has data befor insert into SQL

Asked by gogetsome in .Net Editors & IDEs

Tags: insert, excel, sql, spreadsheet

Hello, Im inserting some rows from an excel spreadsheet into a SQL database with the code below. I need to ensure that all fields contain data on the spreadsheet before importing. If there are nulls then do not perform the insert. What would be the simplest way to do this with the code given below?

 Try
            Dim QuoteNumber As String = lblQuoteNumber.Text.Trim
            Dim sSQLTable As String = "BB_ImportQuoteProducts"
            Dim Prclevel As String = ""
            Dim sExcelFileName As String = ("~/ImportProducts/" & lblImportProductsFileName.Text)
            Dim sWorkbook As String = "[Sheet1$]"

            Dim sExcelConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath(sExcelFileName) & ";Extended Properties=""Excel 8.0;HDR=YES;IMEX=1"""
            Dim sSqlConnectionString As String = (System.Configuration.ConfigurationManager.ConnectionStrings("quoteConnectionString").ToString)

            Dim OleDbConn As OleDbConnection = New OleDbConnection(sExcelConnectionString)
            Dim OleDbCmd As OleDbCommand = New OleDbCommand(("SELECT '', '" & QuoteNumber & "','" & Prclevel & "', [PartNumber], [Description], [UOFM], [Quantity], [UOMPrice], [ExtPrice] FROM " & sWorkbook), OleDbConn)

            OleDbConn.Open()

            Dim dr As OleDbDataReader = OleDbCmd.ExecuteReader()
            Dim bulkCopy As SqlBulkCopy = New SqlBulkCopy(sSqlConnectionString)

            bulkCopy.DestinationTableName = sSQLTable
            bulkCopy.WriteToServer(dr)

            OleDbConn.Close()

            lblImported.Text = "Yes"
        Catch ex As Exception
            lblImportProductError.Text = (ex.Message)
        Finally
        End TryStart Free Trial
[+][-]09.20.2007 at 02:03PM PDT, ID: 19931780

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.20.2007 at 02:04PM PDT, ID: 19931786

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]09.20.2007 at 02:15PM PDT, ID: 19931858

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]09.24.2007 at 01:50AM PDT, ID: 19947080

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: .Net Editors & IDEs
Tags: insert, excel, sql, spreadsheet
Sign Up Now!
Solution Provided By: srinivasbn
Participating Experts: 2
Solution Grade: A
 
 
 
Loading Advertisement...
20080716-EE-VQP-32 / EE_QW_1_20070628