gleznov
asked on
Web Apps, listbox indexes - start at 0 or 1?
Hi,
I've been programming Windows apps in .NET for awhile now, but web apps are new. I'm working on one right now that's giving me an error:
Specified argument was out of the range of valid values. Parameter name: 1
All I know is when I select any index initially other than 0 to fill in text boxes with, I have no problem. When I choose index 0, I get this error. Are you not supposed to use index 0 in a listbox or other control in a web app? And if that is the case, then my other problem is that index 1 chooses the second item, so that whatever's first in the listbox must (in my mind) be index 0, but I can't choose it in code. I'm so frustrated.
JP
I've been programming Windows apps in .NET for awhile now, but web apps are new. I'm working on one right now that's giving me an error:
Specified argument was out of the range of valid values. Parameter name: 1
All I know is when I select any index initially other than 0 to fill in text boxes with, I have no problem. When I choose index 0, I get this error. Are you not supposed to use index 0 in a listbox or other control in a web app? And if that is the case, then my other problem is that index 1 chooses the second item, so that whatever's first in the listbox must (in my mind) be index 0, but I can't choose it in code. I'm so frustrated.
JP
ASKER
LBound(listbox) doesn't seem to work - is it LBound(listbox.something)?
JP
JP
Sorry, it should be LBound(listbox.items). Also, replace 'listbox' with the name of your listbox.
ASKER
Still a problem - I'm using:
selIndex = LBound(cmbSearch.Items)
it still doesn't like it:
\\ga1cdc02\c$\Inetpub\wwwr oot\OEL2\D BItem.aspx .vb(432): Value of type 'System.Web.UI.WebControls .ListItemC ollection' cannot be converted to 'System.Array'.
cmbSearch is a dropdownlist, I just still use cmb (combo) as my prefix because that's what I'm used to using. Oh, I see above I've been saying listbox - sorry bout that, it's actually a dropdownlist.
JP
selIndex = LBound(cmbSearch.Items)
it still doesn't like it:
\\ga1cdc02\c$\Inetpub\wwwr
cmbSearch is a dropdownlist, I just still use cmb (combo) as my prefix because that's what I'm used to using. Oh, I see above I've been saying listbox - sorry bout that, it's actually a dropdownlist.
JP
This thought just popped in my head. If you want the selected item to be the first item in the list, why do you need to set the selectedIndex? When the page loads if there is no preset selectedIndex, the selected item is automatically set to be the first item in the list.
ASKER
Well that's true, but I need to manually fill some text and combo controls using the record in the database referenced by the selected element in that combo box. I was just setting it to 0 for the initial load, but that's where it didn't work. I've tried binding, but for some reason I'm getting nothing in bound boxes.
JP
JP
It should start with 0, i tried and it works fine.
Can you post your code, including the place (procedure) where you are setting the selectedIndex?
Can you post your code, including the place (procedure) where you are setting the selectedIndex?
ASKER
Here is the page_load sub - I used >> in front of the part that crashes with that error.
Session("IsAdd") = False
Dim selIndex As Integer = -1
If Session("files") = 0 Then cmdDelete.Visible = False
cmdDelete.Attributes.Add(" onclick", "javascript:return confirm('Are you sure you want to delete this element ?');")
'Put user code to initialize the page here
SqlDataAdapter1.Fill(DS_DB Item1)
SqlDataAdapter2.Fill(DS_DB Item1)
SqlDataAdapter3.Fill(DS_DB Item1)
If Not (IsPostBack) Then
cmbSearchBy.Items.Add("Nom enclature" )
cmbSearchBy.Items.Add("Ite m ID")
' Fill in the search combos
cmbSearch.Items.Clear()
If cmbSearchBy.SelectedValue = "Nomenclature" Then
lblSearch.Text = "Nomenclature"
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
cmbSearch.Items.Add(DS_DBI tem1.Table s("product s").Rows(x ).Item("Pr oductName" ))
Next
Else
lblSearch.Text = "Item ID"
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
cmbSearch.Items.Add(DS_DBI tem1.Table s("product s").Rows(x ).Item("Pr oductCode" ))
Next
End If
' Fill in the supplier combo
For x As Integer = 0 To DS_DBItem1.Tables("supplie rs").Rows. Count - 1
cmbSup.Items.Add(DS_DBItem 1.Tables(" suppliers" ).Rows(x). Item("Comp anyName"))
Next
' Fill in the Unit Issue combo
For x As Integer = 0 To DS_DBItem1.Tables("units") .Rows.Coun t - 1
cmbUIss.Items.Add(DS_DBIte m1.Tables( "units").R ows(x).Ite m("unit"))
Next
cmbUIss.SelectedValue = "EA"
' Disable controls
txtID.Enabled = False
cmbSup.Enabled = False
txtNom.Enabled = False
txtManf.Enabled = False
txtModel.Enabled = False
txtUPri.Enabled = False
cmbUIss.Enabled = False
' Setup Buttons
cmdAdd.Enabled = True
cmdEdit.Enabled = True
cmdSave.Enabled = False
cmdSave.ImageUrl = "oel2_btn_save_disabled.bm p"
cmdCancel.Enabled = False
cmdCancel.ImageUrl = "oel2_btn_cancel_disabled. bmp"
cmdDelete.Enabled = True
cmdMainMenu.Enabled = True
' Fill controls
' =============
' Fill controls
>> ' selIndex = 0
>> selIndex = LBound(cmbSearch.Items)
>> txtID.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctCode")
>> txtNom.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctName")
>> txtManf.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Manuf acturer")
>> txtModel.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Model ")
>> txtUPri.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("UnitP rice")
>> cmbUIss.SelectedValue = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Qty_I ssue")
' Determine supplier from ID code
Dim supIndex As Integer = -1
For x As Integer = 0 To DS_DBItem1.Tables("supplie rs").Rows. Count - 1
If DS_DBItem1.Tables("supplie rs").Rows( x).Item("S upplierID" ) = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Suppl ierCode") Then
supIndex = x
Exit For
End If
Next
cmbSup.SelectedValue = DS_DBItem1.Tables("Supplie rs").Rows( supIndex). Item("Comp anyName")
Else ' Is Postback
' Find selIndex
If cmbSearchBy.SelectedValue = "Nomenclature" Then
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product s").Rows(x ).Item("Pr oductName" ) Then
selIndex = x
Exit For
End If
Next
Else
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product s").Rows(x ).Item("Pr oductCode" ) Then
selIndex = x
Exit For
End If
Next
End If
' Fill controls
txtID.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctCode")
txtNom.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctName")
txtManf.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Manuf acturer")
txtModel.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Model ")
txtUPri.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("UnitP rice")
cmbUIss.SelectedValue = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Qty_I ssue")
End If
End Sub
Private Sub cmbSearchBy_SelectedIndexC hanged(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles cmbSearchBy.SelectedIndexC hanged
cmbSearch.Items.Clear()
If cmbSearchBy.SelectedValue = "Nomenclature" Then
lblSearch.Text = "Nomenclature"
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
cmbSearch.Items.Add(DS_DBI tem1.Table s("product s").Rows(x ).Item("Pr oductName" ))
Next
Else
lblSearch.Text = "Item ID"
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
cmbSearch.Items.Add(DS_DBI tem1.Table s("product s").Rows(x ).Item("Pr oductCode" ))
Next
End If
End Sub
Private Sub cmdMainMenu_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv entArgs) Handles cmdMainMenu.Click
Response.Redirect("MainMen u.aspx")
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv entArgs) Handles cmdAdd.Click
Session("IsAdd") = True
' Enable controls
txtID.Enabled = True
cmbSup.Enabled = True
txtNom.Enabled = True
txtManf.Enabled = True
txtModel.Enabled = True
txtUPri.Enabled = True
cmbUIss.Enabled = True
' Setup Buttons
cmdAdd.Enabled = False
cmdAdd.ImageUrl = "oel2_btn_add_disabled.bmp "
cmdEdit.Enabled = False
cmdEdit.ImageUrl = "oel2_btn_edit_disabled.bm p"
cmdSave.Enabled = True
cmdSave.ImageUrl = "oel2_btn_save.bmp"
cmdCancel.Enabled = True
cmdCancel.ImageUrl = "oel2_btn_cancel.bmp"
cmdDelete.Enabled = False
cmdDelete.ImageUrl = "oel2_btn_delete_disabled. bmp"
cmdMainMenu.Enabled = False
cmdMainMenu.ImageUrl = "oel2_btn_mainmenu_disable d.bmp"
' Clear controls
txtID.Text = ""
txtNom.Text = ""
txtManf.Text = ""
txtModel.Text = ""
txtUPri.Text = "0.00"
cmbSup.SelectedIndex = 0
cmbUIss.SelectedValue = "EA"
End Sub
Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv entArgs) Handles cmdEdit.Click
' Enable controls
txtID.Enabled = True
cmbSup.Enabled = True
txtNom.Enabled = True
txtManf.Enabled = True
txtModel.Enabled = True
txtUPri.Enabled = True
cmbUIss.Enabled = True
' Setup Buttons
cmdAdd.Enabled = False
cmdAdd.ImageUrl = "oel2_btn_add_disabled.bmp "
cmdEdit.Enabled = False
cmdEdit.ImageUrl = "oel2_btn_edit_disabled.bm p"
cmdSave.Enabled = True
cmdSave.ImageUrl = "oel2_btn_save.bmp"
cmdCancel.Enabled = True
cmdCancel.ImageUrl = "oel2_btn_cancel.bmp"
cmdDelete.Enabled = False
cmdDelete.ImageUrl = "oel2_btn_delete_disabled. bmp"
cmdMainMenu.Enabled = False
cmdMainMenu.ImageUrl = "oel2_btn_mainmenu_disable d.bmp"
End Sub
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv entArgs) Handles cmdCancel.Click
Dim selIndex As Integer = -1
' Enable controls
txtID.Enabled = False
cmbSup.Enabled = False
txtNom.Enabled = False
txtManf.Enabled = False
txtModel.Enabled = False
txtUPri.Enabled = False
cmbUIss.Enabled = False
' Setup Buttons
cmdAdd.Enabled = True
cmdAdd.ImageUrl = "oel2_btn_add.bmp"
cmdEdit.Enabled = True
cmdEdit.ImageUrl = "oel2_btn_edit.bmp"
cmdSave.Enabled = False
cmdSave.ImageUrl = "oel2_btn_save_disabled.bm p"
cmdCancel.Enabled = False
cmdCancel.ImageUrl = "oel2_btn_cancel_disabled. bmp"
cmdDelete.Enabled = True
cmdDelete.ImageUrl = "oel2_btn_delete.bmp"
cmdMainMenu.Enabled = True
cmdMainMenu.ImageUrl = "oel2_btn_mainmenu.bmp"
' Fill controls back up
' =====================
' Find selIndex
If cmbSearchBy.SelectedValue = "Nomenclature" Then
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product s").Rows(x ).Item("Pr oductName" ) Then
selIndex = x
Exit For
End If
Next
Else
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product s").Rows(x ).Item("Pr oductCode" ) Then
selIndex = x
Exit For
End If
Next
End If
' Fill controls
txtID.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctCode")
txtNom.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctName")
txtManf.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Manuf acturer")
txtModel.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Model ")
txtUPri.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("UnitP rice")
cmbUIss.SelectedValue = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Qty_I ssue")
' Determine supplier from ID code
Dim supIndex As Integer = -1
For x As Integer = 0 To DS_DBItem1.Tables("supplie rs").Rows. Count - 1
If DS_DBItem1.Tables("supplie rs").Rows( x).Item("S upplierID" ) = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Suppl ierCode") Then
supIndex = x
Exit For
End If
Next
cmbSup.SelectedValue = DS_DBItem1.Tables("Supplie rs").Rows( supIndex). Item("Comp anyName")
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv entArgs) Handles cmdSave.Click
Dim selIndex As Integer = -1
Dim OSupplier As String
' Find Supplier Code
For d As Integer = 0 To DS_DBItem1.Tables("Supplie rs").Rows. Count - 1
If DS_DBItem1.Tables("supplie rs").Rows( d).Item("C ompanyName ") = cmbSup.SelectedValue.ToStr ing Then
OSupplier = DS_DBItem1.Tables("supplie rs").Rows( d).Item("S upplierID" )
End If
Next
' Find selIndex
If cmbSearchBy.SelectedValue = "Nomenclature" Then
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product s").Rows(x ).Item("Pr oductName" ) Then
selIndex = x
Exit For
End If
Next
Else
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product s").Rows(x ).Item("Pr oductCode" ) Then
selIndex = x
Exit For
End If
Next
End If
If Session("IsAdd") = False Then ' Edit
Dim dr As DataRow
dr = DS_DBItem1.Tables("product s").Rows(s elIndex)
dr.Item("ProductCode") = txtID.Text
dr.Item("SupplierCode") = OSupplier.ToString
dr.Item("ProductName") = txtNom.Text
dr.Item("UnitPrice") = txtUPri.Text
dr.Item("Manufacturer") = txtManf.Text
dr.Item("Model") = txtModel.Text
dr.Item("Qty_Issue") = cmbUIss.SelectedValue.ToSt ring
dr.Item("UnitsInStock") = 0
dr.Item("ReorderLevel") = 0
Try
' When you call the update ADO.net will go through all the record that have a rowstate 'Modified' and will update that record
SqlDataAdapter3.Update(DS_ DBItem1)
Catch ex As Exception
MsgBox(ex.Message)
End Try
DS_DBItem1.AcceptChanges()
Else ' Add
Dim dr As DataRow
dr = DS_DBItem1.Tables("Product s").NewRow
dr.Item("ProductCode") = txtID.Text
dr.Item("SupplierCode") = OSupplier.ToString
dr.Item("ProductName") = txtNom.Text
dr.Item("UnitPrice") = txtUPri.Text
dr.Item("Manufacturer") = txtManf.Text
dr.Item("Model") = txtModel.Text
dr.Item("Qty_Issue") = cmbUIss.SelectedValue.ToSt ring
dr.Item("UnitsInStock") = 0
dr.Item("ReorderLevel") = 0
dr.EndEdit()
DS_DBItem1.Tables("Product s").Rows.A dd(dr)
Try
' When you call the update ADO.net will go through all the record that have a rowstate 'Modified' and will update that record
SqlDataAdapter3.Update(DS_ DBItem1)
Catch ex As Exception
MsgBox(ex.Message)
End Try
DS_DBItem1.AcceptChanges()
End If
' Enable controls
txtID.Enabled = False
cmbSup.Enabled = False
txtNom.Enabled = False
txtManf.Enabled = False
txtModel.Enabled = False
txtUPri.Enabled = False
cmbUIss.Enabled = False
' Setup Buttons
cmdAdd.Enabled = True
cmdAdd.ImageUrl = "oel2_btn_add.bmp"
cmdEdit.Enabled = True
cmdEdit.ImageUrl = "oel2_btn_edit.bmp"
cmdSave.Enabled = False
cmdSave.ImageUrl = "oel2_btn_save_disabled.bm p"
cmdCancel.Enabled = False
cmdCancel.ImageUrl = "oel2_btn_cancel_disabled. bmp"
cmdDelete.Enabled = True
cmdDelete.ImageUrl = "oel2_btn_delete.bmp"
cmdMainMenu.Enabled = True
cmdMainMenu.ImageUrl = "oel2_btn_mainmenu.bmp"
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv entArgs) Handles cmdDelete.Click
Dim selIndex As Integer = -1
' Are you sure you want to delete a record?
' Find selIndex
If cmbSearchBy.SelectedValue = "Nomenclature" Then
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product s").Rows(x ).Item("Pr oductName" ) Then
selIndex = x
Exit For
End If
Next
Else
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product s").Rows(x ).Item("Pr oductCode" ) Then
selIndex = x
Exit For
End If
Next
End If
Dim dr As DataRow
dr = DS_DBItem1.Tables("Product s").Rows(s elIndex)
DS_DBItem1.Tables("Product s").Rows(s elIndex).D elete()
dr.EndEdit()
Try
' When you call the update ADO.net will go through all the record that have a rowstate 'Modified' and will update that record
SqlDataAdapter3.Update(DS_ DBItem1)
Catch ex As Exception
MsgBox(ex.Message)
End Try
DS_DBItem1.AcceptChanges()
selIndex = selIndex - 1
If selIndex < 0 Then selIndex = 0
' Fill controls
' =============
' Fill controls
selIndex = 0
txtID.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctCode")
txtNom.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctName")
txtManf.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Manuf acturer")
txtModel.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Model ")
txtUPri.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("UnitP rice")
cmbUIss.SelectedValue = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Qty_I ssue")
' Repop search box, removing old item, and selecting new item.
' Fill in the search combos
cmbSearch.Items.Clear()
If cmbSearchBy.SelectedValue = "Nomenclature" Then
lblSearch.Text = "Nomenclature"
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
cmbSearch.Items.Add(DS_DBI tem1.Table s("product s").Rows(x ).Item("Pr oductName" ))
Next
Else
lblSearch.Text = "Item ID"
For x As Integer = 0 To DS_DBItem1.Tables("product s").Rows.C ount - 1
cmbSearch.Items.Add(DS_DBI tem1.Table s("product s").Rows(x ).Item("Pr oductCode" ))
Next
End If
cmbSearch.SelectedIndex = selIndex
' Fill controls
txtID.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctCode")
txtNom.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Produ ctName")
txtManf.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Manuf acturer")
txtModel.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Model ")
txtUPri.Text = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("UnitP rice")
cmbUIss.SelectedValue = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Qty_I ssue")
' Determine supplier from ID code
Dim supIndex As Integer = -1
For x As Integer = 0 To DS_DBItem1.Tables("supplie rs").Rows. Count - 1
If DS_DBItem1.Tables("supplie rs").Rows( x).Item("S upplierID" ) = DS_DBItem1.Tables("product s").Rows(s elIndex).I tem("Suppl ierCode") Then
supIndex = x
Exit For
End If
Next
cmbSup.SelectedValue = DS_DBItem1.Tables("Supplie rs").Rows( supIndex). Item("Comp anyName")
JP
Session("IsAdd") = False
Dim selIndex As Integer = -1
If Session("files") = 0 Then cmdDelete.Visible = False
cmdDelete.Attributes.Add("
'Put user code to initialize the page here
SqlDataAdapter1.Fill(DS_DB
SqlDataAdapter2.Fill(DS_DB
SqlDataAdapter3.Fill(DS_DB
If Not (IsPostBack) Then
cmbSearchBy.Items.Add("Nom
cmbSearchBy.Items.Add("Ite
' Fill in the search combos
cmbSearch.Items.Clear()
If cmbSearchBy.SelectedValue = "Nomenclature" Then
lblSearch.Text = "Nomenclature"
For x As Integer = 0 To DS_DBItem1.Tables("product
cmbSearch.Items.Add(DS_DBI
Next
Else
lblSearch.Text = "Item ID"
For x As Integer = 0 To DS_DBItem1.Tables("product
cmbSearch.Items.Add(DS_DBI
Next
End If
' Fill in the supplier combo
For x As Integer = 0 To DS_DBItem1.Tables("supplie
cmbSup.Items.Add(DS_DBItem
Next
' Fill in the Unit Issue combo
For x As Integer = 0 To DS_DBItem1.Tables("units")
cmbUIss.Items.Add(DS_DBIte
Next
cmbUIss.SelectedValue = "EA"
' Disable controls
txtID.Enabled = False
cmbSup.Enabled = False
txtNom.Enabled = False
txtManf.Enabled = False
txtModel.Enabled = False
txtUPri.Enabled = False
cmbUIss.Enabled = False
' Setup Buttons
cmdAdd.Enabled = True
cmdEdit.Enabled = True
cmdSave.Enabled = False
cmdSave.ImageUrl = "oel2_btn_save_disabled.bm
cmdCancel.Enabled = False
cmdCancel.ImageUrl = "oel2_btn_cancel_disabled.
cmdDelete.Enabled = True
cmdMainMenu.Enabled = True
' Fill controls
' =============
' Fill controls
>> ' selIndex = 0
>> selIndex = LBound(cmbSearch.Items)
>> txtID.Text = DS_DBItem1.Tables("product
>> txtNom.Text = DS_DBItem1.Tables("product
>> txtManf.Text = DS_DBItem1.Tables("product
>> txtModel.Text = DS_DBItem1.Tables("product
>> txtUPri.Text = DS_DBItem1.Tables("product
>> cmbUIss.SelectedValue = DS_DBItem1.Tables("product
' Determine supplier from ID code
Dim supIndex As Integer = -1
For x As Integer = 0 To DS_DBItem1.Tables("supplie
If DS_DBItem1.Tables("supplie
supIndex = x
Exit For
End If
Next
cmbSup.SelectedValue = DS_DBItem1.Tables("Supplie
Else ' Is Postback
' Find selIndex
If cmbSearchBy.SelectedValue = "Nomenclature" Then
For x As Integer = 0 To DS_DBItem1.Tables("product
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product
selIndex = x
Exit For
End If
Next
Else
For x As Integer = 0 To DS_DBItem1.Tables("product
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product
selIndex = x
Exit For
End If
Next
End If
' Fill controls
txtID.Text = DS_DBItem1.Tables("product
txtNom.Text = DS_DBItem1.Tables("product
txtManf.Text = DS_DBItem1.Tables("product
txtModel.Text = DS_DBItem1.Tables("product
txtUPri.Text = DS_DBItem1.Tables("product
cmbUIss.SelectedValue = DS_DBItem1.Tables("product
End If
End Sub
Private Sub cmbSearchBy_SelectedIndexC
cmbSearch.Items.Clear()
If cmbSearchBy.SelectedValue = "Nomenclature" Then
lblSearch.Text = "Nomenclature"
For x As Integer = 0 To DS_DBItem1.Tables("product
cmbSearch.Items.Add(DS_DBI
Next
Else
lblSearch.Text = "Item ID"
For x As Integer = 0 To DS_DBItem1.Tables("product
cmbSearch.Items.Add(DS_DBI
Next
End If
End Sub
Private Sub cmdMainMenu_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv
Response.Redirect("MainMen
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv
Session("IsAdd") = True
' Enable controls
txtID.Enabled = True
cmbSup.Enabled = True
txtNom.Enabled = True
txtManf.Enabled = True
txtModel.Enabled = True
txtUPri.Enabled = True
cmbUIss.Enabled = True
' Setup Buttons
cmdAdd.Enabled = False
cmdAdd.ImageUrl = "oel2_btn_add_disabled.bmp
cmdEdit.Enabled = False
cmdEdit.ImageUrl = "oel2_btn_edit_disabled.bm
cmdSave.Enabled = True
cmdSave.ImageUrl = "oel2_btn_save.bmp"
cmdCancel.Enabled = True
cmdCancel.ImageUrl = "oel2_btn_cancel.bmp"
cmdDelete.Enabled = False
cmdDelete.ImageUrl = "oel2_btn_delete_disabled.
cmdMainMenu.Enabled = False
cmdMainMenu.ImageUrl = "oel2_btn_mainmenu_disable
' Clear controls
txtID.Text = ""
txtNom.Text = ""
txtManf.Text = ""
txtModel.Text = ""
txtUPri.Text = "0.00"
cmbSup.SelectedIndex = 0
cmbUIss.SelectedValue = "EA"
End Sub
Private Sub cmdEdit_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv
' Enable controls
txtID.Enabled = True
cmbSup.Enabled = True
txtNom.Enabled = True
txtManf.Enabled = True
txtModel.Enabled = True
txtUPri.Enabled = True
cmbUIss.Enabled = True
' Setup Buttons
cmdAdd.Enabled = False
cmdAdd.ImageUrl = "oel2_btn_add_disabled.bmp
cmdEdit.Enabled = False
cmdEdit.ImageUrl = "oel2_btn_edit_disabled.bm
cmdSave.Enabled = True
cmdSave.ImageUrl = "oel2_btn_save.bmp"
cmdCancel.Enabled = True
cmdCancel.ImageUrl = "oel2_btn_cancel.bmp"
cmdDelete.Enabled = False
cmdDelete.ImageUrl = "oel2_btn_delete_disabled.
cmdMainMenu.Enabled = False
cmdMainMenu.ImageUrl = "oel2_btn_mainmenu_disable
End Sub
Private Sub cmdCancel_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv
Dim selIndex As Integer = -1
' Enable controls
txtID.Enabled = False
cmbSup.Enabled = False
txtNom.Enabled = False
txtManf.Enabled = False
txtModel.Enabled = False
txtUPri.Enabled = False
cmbUIss.Enabled = False
' Setup Buttons
cmdAdd.Enabled = True
cmdAdd.ImageUrl = "oel2_btn_add.bmp"
cmdEdit.Enabled = True
cmdEdit.ImageUrl = "oel2_btn_edit.bmp"
cmdSave.Enabled = False
cmdSave.ImageUrl = "oel2_btn_save_disabled.bm
cmdCancel.Enabled = False
cmdCancel.ImageUrl = "oel2_btn_cancel_disabled.
cmdDelete.Enabled = True
cmdDelete.ImageUrl = "oel2_btn_delete.bmp"
cmdMainMenu.Enabled = True
cmdMainMenu.ImageUrl = "oel2_btn_mainmenu.bmp"
' Fill controls back up
' =====================
' Find selIndex
If cmbSearchBy.SelectedValue = "Nomenclature" Then
For x As Integer = 0 To DS_DBItem1.Tables("product
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product
selIndex = x
Exit For
End If
Next
Else
For x As Integer = 0 To DS_DBItem1.Tables("product
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product
selIndex = x
Exit For
End If
Next
End If
' Fill controls
txtID.Text = DS_DBItem1.Tables("product
txtNom.Text = DS_DBItem1.Tables("product
txtManf.Text = DS_DBItem1.Tables("product
txtModel.Text = DS_DBItem1.Tables("product
txtUPri.Text = DS_DBItem1.Tables("product
cmbUIss.SelectedValue = DS_DBItem1.Tables("product
' Determine supplier from ID code
Dim supIndex As Integer = -1
For x As Integer = 0 To DS_DBItem1.Tables("supplie
If DS_DBItem1.Tables("supplie
supIndex = x
Exit For
End If
Next
cmbSup.SelectedValue = DS_DBItem1.Tables("Supplie
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv
Dim selIndex As Integer = -1
Dim OSupplier As String
' Find Supplier Code
For d As Integer = 0 To DS_DBItem1.Tables("Supplie
If DS_DBItem1.Tables("supplie
OSupplier = DS_DBItem1.Tables("supplie
End If
Next
' Find selIndex
If cmbSearchBy.SelectedValue = "Nomenclature" Then
For x As Integer = 0 To DS_DBItem1.Tables("product
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product
selIndex = x
Exit For
End If
Next
Else
For x As Integer = 0 To DS_DBItem1.Tables("product
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product
selIndex = x
Exit For
End If
Next
End If
If Session("IsAdd") = False Then ' Edit
Dim dr As DataRow
dr = DS_DBItem1.Tables("product
dr.Item("ProductCode") = txtID.Text
dr.Item("SupplierCode") = OSupplier.ToString
dr.Item("ProductName") = txtNom.Text
dr.Item("UnitPrice") = txtUPri.Text
dr.Item("Manufacturer") = txtManf.Text
dr.Item("Model") = txtModel.Text
dr.Item("Qty_Issue") = cmbUIss.SelectedValue.ToSt
dr.Item("UnitsInStock") = 0
dr.Item("ReorderLevel") = 0
Try
' When you call the update ADO.net will go through all the record that have a rowstate 'Modified' and will update that record
SqlDataAdapter3.Update(DS_
Catch ex As Exception
MsgBox(ex.Message)
End Try
DS_DBItem1.AcceptChanges()
Else ' Add
Dim dr As DataRow
dr = DS_DBItem1.Tables("Product
dr.Item("ProductCode") = txtID.Text
dr.Item("SupplierCode") = OSupplier.ToString
dr.Item("ProductName") = txtNom.Text
dr.Item("UnitPrice") = txtUPri.Text
dr.Item("Manufacturer") = txtManf.Text
dr.Item("Model") = txtModel.Text
dr.Item("Qty_Issue") = cmbUIss.SelectedValue.ToSt
dr.Item("UnitsInStock") = 0
dr.Item("ReorderLevel") = 0
dr.EndEdit()
DS_DBItem1.Tables("Product
Try
' When you call the update ADO.net will go through all the record that have a rowstate 'Modified' and will update that record
SqlDataAdapter3.Update(DS_
Catch ex As Exception
MsgBox(ex.Message)
End Try
DS_DBItem1.AcceptChanges()
End If
' Enable controls
txtID.Enabled = False
cmbSup.Enabled = False
txtNom.Enabled = False
txtManf.Enabled = False
txtModel.Enabled = False
txtUPri.Enabled = False
cmbUIss.Enabled = False
' Setup Buttons
cmdAdd.Enabled = True
cmdAdd.ImageUrl = "oel2_btn_add.bmp"
cmdEdit.Enabled = True
cmdEdit.ImageUrl = "oel2_btn_edit.bmp"
cmdSave.Enabled = False
cmdSave.ImageUrl = "oel2_btn_save_disabled.bm
cmdCancel.Enabled = False
cmdCancel.ImageUrl = "oel2_btn_cancel_disabled.
cmdDelete.Enabled = True
cmdDelete.ImageUrl = "oel2_btn_delete.bmp"
cmdMainMenu.Enabled = True
cmdMainMenu.ImageUrl = "oel2_btn_mainmenu.bmp"
End Sub
Private Sub cmdDelete_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEv
Dim selIndex As Integer = -1
' Are you sure you want to delete a record?
' Find selIndex
If cmbSearchBy.SelectedValue = "Nomenclature" Then
For x As Integer = 0 To DS_DBItem1.Tables("product
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product
selIndex = x
Exit For
End If
Next
Else
For x As Integer = 0 To DS_DBItem1.Tables("product
If cmbSearch.SelectedValue = DS_DBItem1.Tables("product
selIndex = x
Exit For
End If
Next
End If
Dim dr As DataRow
dr = DS_DBItem1.Tables("Product
DS_DBItem1.Tables("Product
dr.EndEdit()
Try
' When you call the update ADO.net will go through all the record that have a rowstate 'Modified' and will update that record
SqlDataAdapter3.Update(DS_
Catch ex As Exception
MsgBox(ex.Message)
End Try
DS_DBItem1.AcceptChanges()
selIndex = selIndex - 1
If selIndex < 0 Then selIndex = 0
' Fill controls
' =============
' Fill controls
selIndex = 0
txtID.Text = DS_DBItem1.Tables("product
txtNom.Text = DS_DBItem1.Tables("product
txtManf.Text = DS_DBItem1.Tables("product
txtModel.Text = DS_DBItem1.Tables("product
txtUPri.Text = DS_DBItem1.Tables("product
cmbUIss.SelectedValue = DS_DBItem1.Tables("product
' Repop search box, removing old item, and selecting new item.
' Fill in the search combos
cmbSearch.Items.Clear()
If cmbSearchBy.SelectedValue = "Nomenclature" Then
lblSearch.Text = "Nomenclature"
For x As Integer = 0 To DS_DBItem1.Tables("product
cmbSearch.Items.Add(DS_DBI
Next
Else
lblSearch.Text = "Item ID"
For x As Integer = 0 To DS_DBItem1.Tables("product
cmbSearch.Items.Add(DS_DBI
Next
End If
cmbSearch.SelectedIndex = selIndex
' Fill controls
txtID.Text = DS_DBItem1.Tables("product
txtNom.Text = DS_DBItem1.Tables("product
txtManf.Text = DS_DBItem1.Tables("product
txtModel.Text = DS_DBItem1.Tables("product
txtUPri.Text = DS_DBItem1.Tables("product
cmbUIss.SelectedValue = DS_DBItem1.Tables("product
' Determine supplier from ID code
Dim supIndex As Integer = -1
For x As Integer = 0 To DS_DBItem1.Tables("supplie
If DS_DBItem1.Tables("supplie
supIndex = x
Exit For
End If
Next
cmbSup.SelectedValue = DS_DBItem1.Tables("Supplie
JP
ASKER
I've actually cut out all the old code (posted above) and started again, and this time it works like this (same result though)
I have cmbSearchby, a dropdownlist that selects between "nomenclature" and "item id" -
cmbSearch1 is for nomenclature, and is bound to the productname field of table products
cmbSearch2 is for item ID, and is bound to the productcode field of table products
cmbSearchby decides which of those two boxes shows, but either one can be used to choose from the same records.
I have several text boxes and a couple combos. These correspond to the fields in table Products. These I want to fill in manually, since I can't properly figure out how to bind them (the text boxes). The two combos are filled with values from other tables, Units and Suppliers. My new code is as follows:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Session("files") = 0 Then cmdDelete.Visible = False
cmdDelete.Attributes.Add(" onclick", "javascript:return confirm('Are you sure you want to delete this element ?');")
'Put user code to initialize the page here
SqlDataAdapter1.Fill(DS_Su ppliers1)
SqlDataAdapter2.Fill(DS_Un its1)
SqlDataAdapter3.Fill(DS_Pr oducts1)
cmbSearch1.DataBind()
cmbSearch2.DataBind()
cmbSup.DataBind()
cmbUIss.DataBind()
If Not (IsPostBack) Then
cmbSearchBy.Items.Add("Nom enclature" )
cmbSearchBy.Items.Add("Ite m ID")
cmbSearch1.Visible = True
cmbSearch2.Visible = False
lblSearch1.Visible = True
lblSearch2.Visible = False
cmbUIss.SelectedValue = "EA"
FillControls()
Else ' Postback
End If
End Sub
Private Sub cmbSearchBy_SelectedIndexC hanged(ByV al sender As System.Object, ByVal e As System.EventArgs) Handles cmbSearchBy.SelectedIndexC hanged
If lblSearch1.Visible = True Then
lblSearch1.Visible = False
lblSearch2.Visible = True
cmbSearch1.Visible = False
cmbSearch2.Visible = True
Else
lblSearch1.Visible = True
lblSearch2.Visible = False
cmbSearch1.Visible = True
cmbSearch2.Visible = False
End If
End Sub
Private Sub FillControls()
Dim selIndex = -1
For a As Integer = 0 To DS_Products1.Tables(0).Row s.Count - 1
If DS_Products1.Tables(0).Row s(a).Item( "ProductNa me") = cmbSearch1.SelectedValue Then
selIndex = a
End If
Next
If selIndex = -1 Then Exit Sub
txtID.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Pr oductCode" )
cmbSup.SelectedValue = DS_Products1.Tables(0).Row s(selIndex ).Item("Su pplierCode ")
txtNom.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Pr oductName" )
txtManf.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Ma nufacturer ")
txtModel.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Mo del")
txtUPri.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Un itPrice")
cmbUIss.SelectedValue = DS_Products1.Tables(0).Row s(selIndex ).Item("qt y_issue")
End Sub
I still get the same error - everything works until the FillControls sub - this one chooses the index by running a search in the dataset table to find the value that's pointed at by cmbSearch1. When I run the program I get:
Server Error in '/OEL2' Application.
-------------------------- ---------- ---------- ---------- ---------- ---------- ----
Specified argument was out of the range of valid values. Parameter name: 1
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeE xception: Specified argument was out of the range of valid values. Parameter name: 1
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentOutOfRangeExcepti on: Specified argument was out of the range of valid values.
Parameter name: 1]
System.Web.UI.WebControls. ListContro l.set_Sele ctedValue( String value) +152
OEL2.DBItem.FillControls() in \\ga1cdc02\c$\Inetpub\wwwr oot\OEL2\D BItem.aspx .vb:454
OEL2.DBItem.Page_Load(Obje ct sender, EventArgs e) in \\ga1cdc02\c$\Inetpub\wwwr oot\OEL2\D BItem.aspx .vb:415
System.Web.UI.Control.OnLo ad(EventAr gs e) +67
System.Web.UI.Control.Load Recursive( ) +35
System.Web.UI.Page.Process RequestMai n() +750
I have no idea how to fix this - even if I build a buffer into cmbSearch1 and cmbSearch2, then when someone chooses that buffer (say, "---" or something), the program will crash because index 0 will have been chosen. What could I be doing to elicit this problem?
JP
I have cmbSearchby, a dropdownlist that selects between "nomenclature" and "item id" -
cmbSearch1 is for nomenclature, and is bound to the productname field of table products
cmbSearch2 is for item ID, and is bound to the productcode field of table products
cmbSearchby decides which of those two boxes shows, but either one can be used to choose from the same records.
I have several text boxes and a couple combos. These correspond to the fields in table Products. These I want to fill in manually, since I can't properly figure out how to bind them (the text boxes). The two combos are filled with values from other tables, Units and Suppliers. My new code is as follows:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Session("files") = 0 Then cmdDelete.Visible = False
cmdDelete.Attributes.Add("
'Put user code to initialize the page here
SqlDataAdapter1.Fill(DS_Su
SqlDataAdapter2.Fill(DS_Un
SqlDataAdapter3.Fill(DS_Pr
cmbSearch1.DataBind()
cmbSearch2.DataBind()
cmbSup.DataBind()
cmbUIss.DataBind()
If Not (IsPostBack) Then
cmbSearchBy.Items.Add("Nom
cmbSearchBy.Items.Add("Ite
cmbSearch1.Visible = True
cmbSearch2.Visible = False
lblSearch1.Visible = True
lblSearch2.Visible = False
cmbUIss.SelectedValue = "EA"
FillControls()
Else ' Postback
End If
End Sub
Private Sub cmbSearchBy_SelectedIndexC
If lblSearch1.Visible = True Then
lblSearch1.Visible = False
lblSearch2.Visible = True
cmbSearch1.Visible = False
cmbSearch2.Visible = True
Else
lblSearch1.Visible = True
lblSearch2.Visible = False
cmbSearch1.Visible = True
cmbSearch2.Visible = False
End If
End Sub
Private Sub FillControls()
Dim selIndex = -1
For a As Integer = 0 To DS_Products1.Tables(0).Row
If DS_Products1.Tables(0).Row
selIndex = a
End If
Next
If selIndex = -1 Then Exit Sub
txtID.Text = DS_Products1.Tables(0).Row
cmbSup.SelectedValue = DS_Products1.Tables(0).Row
txtNom.Text = DS_Products1.Tables(0).Row
txtManf.Text = DS_Products1.Tables(0).Row
txtModel.Text = DS_Products1.Tables(0).Row
txtUPri.Text = DS_Products1.Tables(0).Row
cmbUIss.SelectedValue = DS_Products1.Tables(0).Row
End Sub
I still get the same error - everything works until the FillControls sub - this one chooses the index by running a search in the dataset table to find the value that's pointed at by cmbSearch1. When I run the program I get:
Server Error in '/OEL2' Application.
--------------------------
Specified argument was out of the range of valid values. Parameter name: 1
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentOutOfRangeE
Source Error:
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.
Stack Trace:
[ArgumentOutOfRangeExcepti
Parameter name: 1]
System.Web.UI.WebControls.
OEL2.DBItem.FillControls()
OEL2.DBItem.Page_Load(Obje
System.Web.UI.Control.OnLo
System.Web.UI.Control.Load
System.Web.UI.Page.Process
I have no idea how to fix this - even if I build a buffer into cmbSearch1 and cmbSearch2, then when someone chooses that buffer (say, "---" or something), the program will crash because index 0 will have been chosen. What could I be doing to elicit this problem?
JP
Try writing:
cmbUIss.SelectedIndex = selIndex
instead of:
cmbUIss.SelectedValue = DS_Products1.Tables(0).Row s(selIndex ).Item("qt y_issue")
cmbUIss.SelectedIndex = selIndex
instead of:
cmbUIss.SelectedValue = DS_Products1.Tables(0).Row
ASKER
cmbUIss is unit issue - it draws from a different table than everything else. This one works fine - it's the rest that doesn't.
Another thing I'm having trouble with:
On postback, whatever I choose in cmbSearch1 immediately reverts to index 0. I don't know how to stop this.
JP
Another thing I'm having trouble with:
On postback, whatever I choose in cmbSearch1 immediately reverts to index 0. I don't know how to stop this.
JP
ASKER
Oops, nevermind on the problem in that last post, I had to shift the Dataset fills and the databinds into the if not (ispostback) section.
JP
JP
You mean by the rest this code?
txtID.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Pr oductCode" )
cmbSup.SelectedValue = DS_Products1.Tables(0).Row s(selIndex ).Item("Su pplierCode ")
txtNom.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Pr oductName" )
txtManf.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Ma nufacturer ")
txtModel.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Mo del")
txtUPri.Text = DS_Products1.Tables(0).Row s(selIndex ).Item("Un itPrice")
txtID.Text = DS_Products1.Tables(0).Row
cmbSup.SelectedValue = DS_Products1.Tables(0).Row
txtNom.Text = DS_Products1.Tables(0).Row
txtManf.Text = DS_Products1.Tables(0).Row
txtModel.Text = DS_Products1.Tables(0).Row
txtUPri.Text = DS_Products1.Tables(0).Row
What is cmbSup?
a ComboBox or ListBox
I tried doing this:
selIndex = 0
txtID.Text = ds.Tables(0).Rows(selIndex ).Item("Cu stomerID")
and it works.
i want to try it with cmbSup, but need to know the type of control.
a ComboBox or ListBox
I tried doing this:
selIndex = 0
txtID.Text = ds.Tables(0).Rows(selIndex
and it works.
i want to try it with cmbSup, but need to know the type of control.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
cmbsup is a dropdownlist - I'm used to calling them combo boxes from windows apps.
Also, cmbSup contains the company name, the value is the suppliercode - I'll go look at that right away. Thanks!
I've found a few other minor errors in the code and am still debugging at it - I think something to do with several smaller errors is producing the larger error... will get back in a bit...
JP
Also, cmbSup contains the company name, the value is the suppliercode - I'll go look at that right away. Thanks!
I've found a few other minor errors in the code and am still debugging at it - I think something to do with several smaller errors is producing the larger error... will get back in a bit...
JP
- Joe