Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8602397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T02:04:34+00:00 2026-06-12T02:04:34+00:00

Hi I have a requirement to import spreadsheets – validate data and save to

  • 0

Hi I have a requirement to import spreadsheets – validate data and save to DB.

The spreadsheet data comes from two sources. One can be imported directly (I have no problems with this). The data in the spreadsheet from the second source has to be manipulated before the data is saved to the DB.

I’ve created a web page which uploads the xls files and then imports to datatable. The datatable is then the data source for a gridview. I then do validation and save to DB.

This code worked great for the standard spreadsheet. I now need to amend the code so that it can cope with either type of source spreadsheet.

Example Data
Standard Source

Unique ID   Ref       PartName           New_SH Year  ProductType   Location  Qty  TopSeller
39564       SD9PP/SS  Micra Water Pump   N      2012  Cooling       CD19       12  Y

Source that needs manipulating

Unique ID   Ref                   PartName          New_SH  Year  ProductType   Location    Qty  TopSeller
IS-4564     XM3M_ZZ_001_001_1982  180 wiper motor   S             Wiper Motor   CD19          3  N

In the above example the Ref needs to be manipulated so the the last 4 digits are transfered to the YEAR column and the first part of the Ref up to the second underscore is trimed and the underscore is replaced with / e.g. XM3M/ZZ.
The reference can be anything from 4+2suffix to 6+2suffix but will always be followed by _XXX_XXX_YYYY where X and Y are digits X are not required and YYYY is the year.

My plan was to import the spreadsheet to a temp datatable and the go through that table row by row cell by cell and copy to another datatable after making any alterations neccesary. This second table is then uses as the datasource for the gridview.

Here is my code…

Protected Sub ReadExcelFile(filename As String, worksheet As String)

Try
' Fetch Data from Excel data is based on the following
'Unique ID  Ref         PartName        New_SH  Year    ProductType Location    Qty TopSeller
'IS-4564    XM3M_ZZ_001_001_1982    180 wiper motor     S       Wiper Motor CD19            3   N
'35667      SD9PP/SS        Micra water pump    N   2012    Cooling     CA4     14  Y

Dim MyConnection As New OleDbConnection("provider=Microsoft.Jet.OLEDB.4.0; data source='" & filename & "'; Extended Properties='Excel 8.0; HDR=Yes; IMEX=1'")
Dim MyCommand As New OleDbDataAdapter("select * from [" & worksheet & "]", MyConnection)
Dim dtPreview As New DataTable()
Dim dtTemp As New DataTable()


    'Place data in temp datatable
    MyCommand.Fill(dtTemp)

    'Go through tem[ datatable and transfer cell values to dtPreview
    If dtTemp.Columns.Count < 9 Then
    'We do not have enough columns
    lb_Error.Text = "Spreadsheet does not contain enough columns
    Exit Sub

    Else
    'We have at least 9 columns - we are only interested in the first 9
    If dtTemp.Rows.Count > 0 Then
        'we have at least one row

        'Add Column Headers
        For c = 0 To dtTemp.Columns.Count - 1
        dtPreview.Columns.Add(dtTemp.Columns(c).ColumnName, dtTemp.Columns(c).DataType)
        Next

        'Add Row Data
        Dim l As Integer = 0
        For Each drTempRow As DataRow In dtTemp.Rows


        Dim newRow As DataRow = dtPreview.NewRow()

        'Unique ID
        If Left(dtTemp.Rows(0).ToString, 3) = "IS-" Then
            newRow(0) = Mid(dtTemp.Columns(0).ToString.ToUpper.Trim, 4, 8)
        Else
            newRow(0) = dtTemp.Columns(0).ToString.ToUpper.Trim
        End If

        'Ref
        If Left(dtTemp.Rows(0).ToString, 3) = "IS-" Then
            l = Len(dtTemp.Columns(1).ToString.Trim)
            newRow(1) = Left(dtTemp.Columns(1).ToString.Replace("_", "/").ToUpper.Trim, l - 13)
        Else
            newRow(1) = dtTemp.Columns(1).ToString.ToUpper.Trim
        End If

        'Part Name
        newRow(2) = dtTemp.Columns(2).ToString.ToUpper.Trim

        'New / Secondhand
        newRow(3) = dtTemp.Columns(3).ToString.ToUpper.Trim

        'Year
        If Left(dtTemp.Columns(0).ToString, 3) = "IS-" Then
            newRow(4) = Right(dtTemp.Columns(0).ToString, 4)
        Else
            newRow(4) = dtTemp.Columns(4).ToString
        End If

        'Product Type
        newRow(5) = dtTemp.Columns(5).ToString.ToUpper.Trim

        'Location
        newRow(6) = dtTemp.Columns(6).ToString.ToUpper.Trim

        'Quantity in Stock
        newRow(7) = dtTemp.Columns(7).ToString

        'Top Seller
        newRow(8) = dtTemp.Columns(8).ToString.ToUpper.Trim

        dtPreview.Rows.Add(newRow)

        newRow = Nothing
        Next

    End If

    'Add additional columns
    dtPreview.Columns.Add("RecdBy", GetType(System.Int16), Session("UserNo").ToString())

    If ddl_FileSource.SelectedValue.ToString() = "1" Then
        dtPreview.Columns.Add("IntExt", GetType(System.Int16), "1")
    Else
        dtPreview.Columns.Add("IntExt", GetType(System.Int16), "0")
    End If

    dtTemp = Nothing

End If

'Bind DataTable to Gridview
Session("dtgv") = dtPreview
gv_StockData.DataSource = dtPreview
gv_StockData.DataBind()
MyConnection.Close()

Catch msg As Exception
lb_Error.Text = msg.Message

End Try

End Sub

The trouble is I’m getting errors…. Am I approaching this from the right angle?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T02:04:35+00:00Added an answer on June 12, 2026 at 2:04 am

    Sorted… The code for looping through each cell in datatable was flawed… will publish working version later today.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a requirement to take the test data from Excel sheet and write
In python 2.7, by using from __future__ import division, print_function I can now have
I have a Java Program that will import data from a DB2 table to
I have two tables, one an import table, the other a FK constraint on
My requirement is the following: From my ASPX page I import data from a
I have a requirement to do some imports from an Excel spread sheet and
I have requirement to get Facebook friends list with their images. How can I
In our project we have requirement that, after receiving sms message from third party
In my asp.net web application there is a requirement where i have to import
I have a Excel spreadsheet of requirements created from ReqPro that I need to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.