kookaburra
08-02-2004, 12:30 AM
This code works if I select only the first one or two items, but most of the time throws and exception "Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index". Please tell me what I've done wrong....
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim ex() As String = New String() {"ADE", "BRI", "MEL", "CAN", "CEN", "DAR", "GDC", "HOB", "LAU", "N01", "N02", "N03", "N04", "N05", "N06", "NCL", "PER", "Q01", "Q02", "Q03", "Q04", "Q05", "Q06", "Q07", "Q08", "S02", "S03", "S04", "Kang Is", "SUN", "SYD", "T02", "TSV", "V01", "V02", "V03", "W01", "W02", "W03", "W04", "W05", "W06", "W07", "W08", "W09", "Broome", "WOL"}
Dim dest() As String = New String() {"ADE", "BRI", "MEL", "CAN", "CEN", "DAR", "GDC", "HOB", "LAU", "N01", "N02", "N03", "N04", "N05", "N06", "NCL", "PER", "Q01", "Q02", "Q03", "Q04", "Q05", "Q06", "Q07", "Q08", "S02", "S03", "S04", "Kang Is", "SUN", "SYD", "T02", "TSV", "V01", "V02", "V03", "W01", "W02", "W03", "W04", "W05", "W06", "W07", "W08", "W09", "Broome", "WOL"}
Dim sht() As String = New String() {"KG Dead Weight Cost", "KG Dead + 20%", "Kg Dead + 30%", "Kg Dead + 40%", "Kg Cost 150 cube", "Kg 150 + 20%", "Kg 150 + 30%", "Kg 150 + 40%", "Kg Cost 180 cube", "Kg 180 + 20%", "Kg 180 + 30%", "Kg 180 + 40%", "Kg Cost 200 cube", "Kg 200 + 20%", "Kg 200 + 30%", "Kg 200 + 40%", "Kg Cost 250 cube", "Kg 250 + 20%", "Kg 250 + 30%", "Kg 250 + 40%"}
Dim a As Integer
Dim b As Integer
Dim c As Integer
For a = 0 To UBound(ex)
Me.DropDownList1.Items.Add(New ListItem(ex(a)))
Next
For b = 0 To UBound(dest)
Me.DropDownList2.Items.Add(New ListItem(dest(b)))
Next
For c = 0 To UBound(sht)
Me.DropDownList3.Items.Add(New ListItem(sht(c)))
Next
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Range As String
Range = DropDownList3.SelectedItem.Text
' Create connection string variable. Modify the "Data Source" parameter as
' appropriate for your environment.
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath("../CostMatrix.xls") _
& ";" & "Extended Properties=Excel 8.0;"
' Create the connection object by using the preceding connection string.
Dim objConn As New OleDbConnection(sConnectionString)
' Open connection with the database.
objConn.Open()
' Create new OleDbCommand to return data from worksheet.
Dim objCmdSelect1 As New OleDbCommand("SELECT [" & DropDownList2.SelectedItem.Text & "]" & " FROM [Basic Charges$A14:co61] WHERE F1 = '" & DropDownList1.SelectedItem.Text & "'", objConn)
' Create new OleDbDataAdapter that is used to build a DataSet
' based on the preceding SQL SELECT statement.
Dim objAdapter1 As New OleDbDataAdapter
' Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect1
' Create new DataSet to hold information from the worksheet.
Dim objDataset1 As New DataSet
Dim objDataVal As String
' Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData")
' Build a table from the original data.
DataGrid1.DataSource = objDataset1.Tables(0).DefaultView
DataGrid1.DataBind()
Dim objCmdSelect2 As New OleDbCommand("SELECT [" & DropDownList2.SelectedItem.Text & "]" & " FROM [" & Range & "$A3:AV5] WHERE Ex = '" & DropDownList1.SelectedItem.Text & "'", objConn)
' Create new OleDbDataAdapter that is used to build a DataSet
' based on the preceding SQL SELECT statement.
Dim objAdapter2 As New OleDbDataAdapter
' Pass the Select command to the adapter.
objAdapter2.SelectCommand = objCmdSelect2
' Create new DataSet to hold information from the worksheet.
Dim objDataset2 As New DataSet
' Fill the DataSet with the information from the worksheet.
objAdapter2.Fill(objDataset2, "XLData")
' Build a table from the original data.
DataGrid2.DataSource = objDataset2.Tables(0).DefaultView
DataGrid2.DataBind()
' Clean up objects.
objConn.Close()
Dim Basic As Decimal
Dim Additional As Decimal
Basic = DataGrid1.Items(0).Cells(0).Text
Additional = DataGrid2.Items(0).Cells(0).Text
LabelText.Text = (Additional + Basic)
LabelText2.Text = (Additional + Basic + 0.03)
End Sub
End Class
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not Page.IsPostBack Then
Dim ex() As String = New String() {"ADE", "BRI", "MEL", "CAN", "CEN", "DAR", "GDC", "HOB", "LAU", "N01", "N02", "N03", "N04", "N05", "N06", "NCL", "PER", "Q01", "Q02", "Q03", "Q04", "Q05", "Q06", "Q07", "Q08", "S02", "S03", "S04", "Kang Is", "SUN", "SYD", "T02", "TSV", "V01", "V02", "V03", "W01", "W02", "W03", "W04", "W05", "W06", "W07", "W08", "W09", "Broome", "WOL"}
Dim dest() As String = New String() {"ADE", "BRI", "MEL", "CAN", "CEN", "DAR", "GDC", "HOB", "LAU", "N01", "N02", "N03", "N04", "N05", "N06", "NCL", "PER", "Q01", "Q02", "Q03", "Q04", "Q05", "Q06", "Q07", "Q08", "S02", "S03", "S04", "Kang Is", "SUN", "SYD", "T02", "TSV", "V01", "V02", "V03", "W01", "W02", "W03", "W04", "W05", "W06", "W07", "W08", "W09", "Broome", "WOL"}
Dim sht() As String = New String() {"KG Dead Weight Cost", "KG Dead + 20%", "Kg Dead + 30%", "Kg Dead + 40%", "Kg Cost 150 cube", "Kg 150 + 20%", "Kg 150 + 30%", "Kg 150 + 40%", "Kg Cost 180 cube", "Kg 180 + 20%", "Kg 180 + 30%", "Kg 180 + 40%", "Kg Cost 200 cube", "Kg 200 + 20%", "Kg 200 + 30%", "Kg 200 + 40%", "Kg Cost 250 cube", "Kg 250 + 20%", "Kg 250 + 30%", "Kg 250 + 40%"}
Dim a As Integer
Dim b As Integer
Dim c As Integer
For a = 0 To UBound(ex)
Me.DropDownList1.Items.Add(New ListItem(ex(a)))
Next
For b = 0 To UBound(dest)
Me.DropDownList2.Items.Add(New ListItem(dest(b)))
Next
For c = 0 To UBound(sht)
Me.DropDownList3.Items.Add(New ListItem(sht(c)))
Next
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim Range As String
Range = DropDownList3.SelectedItem.Text
' Create connection string variable. Modify the "Data Source" parameter as
' appropriate for your environment.
Dim sConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0;" _
& "Data Source=" & Server.MapPath("../CostMatrix.xls") _
& ";" & "Extended Properties=Excel 8.0;"
' Create the connection object by using the preceding connection string.
Dim objConn As New OleDbConnection(sConnectionString)
' Open connection with the database.
objConn.Open()
' Create new OleDbCommand to return data from worksheet.
Dim objCmdSelect1 As New OleDbCommand("SELECT [" & DropDownList2.SelectedItem.Text & "]" & " FROM [Basic Charges$A14:co61] WHERE F1 = '" & DropDownList1.SelectedItem.Text & "'", objConn)
' Create new OleDbDataAdapter that is used to build a DataSet
' based on the preceding SQL SELECT statement.
Dim objAdapter1 As New OleDbDataAdapter
' Pass the Select command to the adapter.
objAdapter1.SelectCommand = objCmdSelect1
' Create new DataSet to hold information from the worksheet.
Dim objDataset1 As New DataSet
Dim objDataVal As String
' Fill the DataSet with the information from the worksheet.
objAdapter1.Fill(objDataset1, "XLData")
' Build a table from the original data.
DataGrid1.DataSource = objDataset1.Tables(0).DefaultView
DataGrid1.DataBind()
Dim objCmdSelect2 As New OleDbCommand("SELECT [" & DropDownList2.SelectedItem.Text & "]" & " FROM [" & Range & "$A3:AV5] WHERE Ex = '" & DropDownList1.SelectedItem.Text & "'", objConn)
' Create new OleDbDataAdapter that is used to build a DataSet
' based on the preceding SQL SELECT statement.
Dim objAdapter2 As New OleDbDataAdapter
' Pass the Select command to the adapter.
objAdapter2.SelectCommand = objCmdSelect2
' Create new DataSet to hold information from the worksheet.
Dim objDataset2 As New DataSet
' Fill the DataSet with the information from the worksheet.
objAdapter2.Fill(objDataset2, "XLData")
' Build a table from the original data.
DataGrid2.DataSource = objDataset2.Tables(0).DefaultView
DataGrid2.DataBind()
' Clean up objects.
objConn.Close()
Dim Basic As Decimal
Dim Additional As Decimal
Basic = DataGrid1.Items(0).Cells(0).Text
Additional = DataGrid2.Items(0).Cells(0).Text
LabelText.Text = (Additional + Basic)
LabelText2.Text = (Additional + Basic + 0.03)
End Sub
End Class