I have a simple web application in vb.net (on .net 2.0) that takes data from an excel sheet and stores it into a sql server database using typed datasets.
My problem is in the follwing code. This is an event handler for the "Import into SQL" button, after the excel file has been uploaded.
Protected
Sub ButtonImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles ButtonImport.ClickPanelUpload.Visible =
FalsePanelView.Visible =
FalsePanelImport.Visible =
True ' retrieve the Select Command for the worksheet data Dim objCommand As New OleDbCommand()objCommand = ExcelConnection()
' create a DataReader Dim reader As OleDbDataReaderreader = objCommand.ExecuteReader()
Dim counter As Integer = 0 While reader.Read() Dim ORIGINATOR_ID As String = Convert.ToString(reader("LBF_PREFIX")) 'Dim SMPL_DTE As Integer = Convert.ToInt64reader("LBF_DATE") 'Dim LAB_NUM As Double = Convert.ToDouble(reader("LBF_LAB_NUM")) Dim LAB_NUM As Double = "1"
'''I get a IndexOutOfRangeException right here, or where I try to read any row into its respective variable to be passed on to an import function. In the line above, if I would try to read the column value into LAB_NUM for instance,
Dim LAB_NUM as double = Convert.ToDouble(reader("LBF_LAB_NUM")) as I did in the commented code above it, I get the same error. What am I doing wrong here??? I have read over the suggestions and tips and they keep hinting at how this happens when the index is larger than the max index in the list, or if its a negative index, or when the column names are not defined right, but I'm still stumped:( Any help would be most appreciated.
Thanks,
'REST OF THE METHOD
--->>>>Dim TURN As String = Convert.ToString(reader("LBF_TURN"))
Dim SMPL_ANALYS_DTE As Integer = Convert.ToInt64(reader("ANALYS_HOUR"))
Dim SMPL_ANALYS_MIN As Integer = Convert.ToInt64(reader("ANALYS_MIN"))
'ADD THE NEXT COLUMN "ANALYS_MIN" AS "ANALYS_MIN TO SQL TABLE'
'Or append hour and min before inputting it into SMPL_ANALYS_DTE 'Dim SMPL_ANALYS_DTE As String = Convert.ToString(reader("ANALYS_MIN")) ......
...
...
Dim STABILITY As Double = Convert.ToDouble(reader("STABILITY"))
Dim HARDNESS As Double = Convert.ToDouble(reader("HARDNESS"))
Dim QRT_TUMBLE As Double = Convert.ToDouble(reader("QRT_TUMBLE"))
Dim TUMBLE_30M As Double = Convert.ToDouble(reader("30M_TUMBLE"))
Dim APTSPC_GRAV As Double = Convert.ToDouble(reader("APTSPC_GRAV"))
Dim FREE_SWL_I As Double = Convert.ToDouble(reader("FREE_SWL_I"))
Dim PULV8TH As Double = Convert.ToDouble(reader("8TH_PULV"))
Dim QRT_PULV As Double = Convert.ToDouble(reader("QRT_PULV"))
Dim SCR_4 As Double = Convert.ToDouble(reader("SCR_4"))
...
...
...
...
'Ensure the import data includes an ORIGINATOR_ID for each member
'If ORIGINATOR_ID.Length = 0 Then 'LabelImport.Text &= "You must provide an ORIGINATOR_ID!" 'End IfImportIntoMembers(ORIGINATOR_ID, SMPL_DTE, LAB_NUM, TURN, SMPL_ANALYS_DTE, _
MOIS_GRAV, MOISTURE, S, VM, ASH, STABILITY, HARDNESS, QRT_TUMBLE, TUMBLE_30M, _
APTSPC_GRAV, FREE_SWL_I, PULV8TH, QRT_PULV, SCR_4, SCR_3, SCR_2, SCR_1NHALF, _
SCR_1, SCR_3QRT, SCR_HALF, SCR_38THS, SCR_QRT, SCR_8TH, SCR_20M, SCR_30M, SCR_50M, _
SCR_100M, SCR_PAN, SCR_QRT_PLUS, SAMPL_LOC)
End Whilereader.Close()
End Sub
ghazanfar khanPosted May 20, 2008, 10:56 AM
Hi,
No. I'm getting that error at every row after the very first one, which is "ORIGINATER_ID".
I tried another variation of the Read method where I accessed the columns using their index values rather than the string column name. This did not throw the error for many columns but then again, it did for some of them.
Below is the code. I added breakpoints to all the lines in red and added the following items to Watch. What the Watch told me is as follows:
reader.read() = cannot evaluate expression because we are at a place where garbage collection is impossible, since the code of the current method may be optimized.
reader.getString(0) = cannot evaluate expression because we are at a place where garbage collection is impossible, since the code of the current method may be optimized.
ORIGINATER_ID = name not declared
reader(1) = In Order to evaluate an indexed property, the property must be qualified and the arguments must be explicitly supplied by the user.
'Modified code below
Protected
Sub ButtonImport_Click(ByVal sender As Object, ByVal e As System.EventArgs) _ Handles ButtonImport.ClickPanelUpload.Visible =
FalsePanelView.Visible =
FalsePanelImport.Visible =
True Dim objCommand As New OleDbCommand()objCommand = ExcelConnection()
Dim reader As OleDbDataReaderreader = objCommand.ExecuteReader()
Dim counter As Integer = 0 While reader.Read()counter = counter + 1
' counter to exit early for testing... Dim ORIGINATOR_ID As String = reader.GetString(0) Dim SMPL_DTE As String = Convert.ToString(reader(1)) Dim LAB_NUM As Double = reader.GetDouble(2) Dim TURN As String = Convert.ToString(reader(3)) Dim SMPL_ANALYS_DTE As String = Convert.ToString(reader(4)) Dim SMPL_ANALYS_MIN As String = Convert.ToString(reader(5)) Dim MOIS_GRAV As String = Convert.ToString(reader(6)) Dim MOISTURE As String = Convert.ToString(reader(7))Dipal ChoksiPosted May 20, 2008, 2:41 AM
Are you getting the error only at the code line for the LAB_NUM field?