Can anybody help with this code?

  • Thread starter Catalin Porancea
  • Start date
C

Catalin Porancea

Can anybody tell me what is wrong with this code? I would really appreciate
some help here.
The data grid has 4 columns (0,1,2,3), and 1 is not visible.
Whenever I click Edit, I get this error:
_____________________________

Specified argument was out of the range of valid values. Parameter name:
index
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.ArgumentOutOfRangeException: Specified argument
was out of the range of valid values. Parameter name: index

Source Error:

Line 131: Sub dgUpdate(ByVal s As Object, ByVal e As
DataGridCommandEventArgs) Handles dg.ItemCommand
Line 132: con.Open()
Line 133: Dim txtDescr As TextBox = e.Item.Cells(1).Controls(0)
Line 134: Dim txtPrice As TextBox = e.Item.Cells(2).Controls(0)
Line 135: Dim strProdID As String = e.Item.Cells(1).Text


Source File: C:\Inetpub\wwwroot\Copy_of_WebApplication2\products.aspx.vb
Line: 133

Stack Trace:

[ArgumentOutOfRangeException: Specified argument was out of the range of
valid values.
Parameter name: index]
System.Web.UI.ControlCollection.get_Item(Int32 index) +58
WebApplication2.products.dgUpdate(Object s, DataGridCommandEventArgs e)
in C:\Inetpub\wwwroot\Copy_of_WebApplication2\products.aspx.vb:133
System.Web.UI.WebControls.DataGridCommandEventHandler.Invoke(Object
source, DataGridCommandEventArgs e) +0
System.Web.UI.WebControls.DataGrid.OnItemCommand(DataGridCommandEventArgs
e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs
e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source,
EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.LinkButton.OnCommand(CommandEventArgs e)

System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.Rai
sePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain() +1277





Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET
Version:1.1.4322.573
____________________________________

This is the code:

Public Class products
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
________________________
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
con.Open()
da.Fill(Ds_prodcat)
dg.DataBind()
con.Close()
End If
End Sub
_____________________
Sub Edit(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles
dg.ItemCommand
con.Open()
dg.EditItemIndex = e.Item.ItemIndex
da.Fill(Ds_prodcat)
dg.DataBind()
con.Close()
End Sub
_____________________
Sub Cancel(ByVal Source As Object, ByVal E As DataGridCommandEventArgs)
Handles dg.ItemCommand
con.Open()
dg.EditItemIndex = -1
da.Fill(Ds_prodcat)
dg.DataBind()
con.Close()
End Sub
_____________________
Sub Update(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles
dg.ItemCommand
con.Open()
Dim txtDescr As TextBox = e.Item.Cells(2).Controls(0)
Dim txtPrice As TextBox = e.Item.Cells(3).Controls(0)
Dim strProdID As String = e.Item.Cells(1).Text
Dim strDescr As String = txtDescr.Text
Dim strPrice As String = txtPrice.Text
cmd.CommandText = "update product set description = '" & strDescr & "',
price = '" & strPrice & "' where productid = '" & strProdID & "'"
cmd.ExecuteNonQuery()
da.Fill(Ds_prodcat)
dg.DataBind()
con.Close()
End Sub
_____________________
End Class


Thank You.
Catalin
 
H

H Branyan

Line 131: Sub dgUpdate(ByVal s As Object, ByVal e As
DataGridCommandEventArgs) Handles dg.ItemCommand
Line 132: con.Open()
Line 133: Dim txtDescr As TextBox = e.Item.Cells(1).Controls(0)
Line 134: Dim txtPrice As TextBox = e.Item.Cells(2).Controls(0)
Line 135: Dim strProdID As String = e.Item.Cells(1).Text

This code doesn't match this code:
Sub Update(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles
dg.ItemCommand
con.Open()
Dim txtDescr As TextBox = e.Item.Cells(2).Controls(0)
Dim txtPrice As TextBox = e.Item.Cells(3).Controls(0)
Dim strProdID As String = e.Item.Cells(1).Text


Are you using the most recent build?
 
C

Catalin Porancea

My mistake, I changed the code afterwards but I had the error page still
open when I copied it.
I made some progress though. This is the new code:

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
con.Open()
da.Fill(Ds_prodcat)
dg.DataBind()
con.Close()
End If
End Sub
______________________
Sub Edit(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles
dg.ItemCommand
con.Open()
dg.EditItemIndex = e.Item.ItemIndex
da.Fill(Ds_prodcat)
dg.DataBind()
con.Close()
End Sub
_____________________
Sub Cancel(ByVal Source As Object, ByVal E As DataGridCommandEventArgs)
Handles dg.ItemCommand
con.Open()
da.Fill(Ds_prodcat)
dg.EditItemIndex = -1
dg.DataBind()
con.Close()
End Sub
_____________________
Sub Update(ByVal s As Object, ByVal e As DataGridCommandEventArgs) Handles
dg.ItemCommand
con.Open()
Dim strProdID As String = e.Item.Cells(1).Text
Dim strDescr As String = e.Item.Cells(2).Text
Dim strPrice As String = e.Item.Cells(3).Text
cmd.CommandText = "update product set description = '" & strDescr & "',
price = convert(money,replace('" & strPrice & "','$','')) where productid =
'" & strProdID & "'"
cmd.ExecuteNonQuery()
dg.EditItemIndex = -1
da.Fill(Ds_prodcat)
dg.DataBind()
con.Close()
End Sub
__________________

Now, I don't get that error anymore and the data grid goes into edit mode.
However, Cancel doesn't exit the edit mode. Also, Edit and Cancel trigger
the update command.

Can you help with this?

Thank you

--
Catalin Porancea
: :
: > Line 131: Sub dgUpdate(ByVal s As Object, ByVal e As
: > DataGridCommandEventArgs) Handles dg.ItemCommand
: > Line 132: con.Open()
: > Line 133: Dim txtDescr As TextBox = e.Item.Cells(1).Controls(0)
: > Line 134: Dim txtPrice As TextBox = e.Item.Cells(2).Controls(0)
: > Line 135: Dim strProdID As String = e.Item.Cells(1).Text
:
: This code doesn't match this code:
:
: > Sub Update(ByVal s As Object, ByVal e As DataGridCommandEventArgs)
Handles
: > dg.ItemCommand
: > con.Open()
: > Dim txtDescr As TextBox = e.Item.Cells(2).Controls(0)
: > Dim txtPrice As TextBox = e.Item.Cells(3).Controls(0)
: > Dim strProdID As String = e.Item.Cells(1).Text
:
:
: Are you using the most recent build?
:
: --
: [[((hillarie))]]
:
: To reply by email, remove ".SPAMBLOCK" from email address
:
:
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top