Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Newbie-Index out of range error

Reply
Thread Tools

Newbie-Index out of range error

 
 
=?Utf-8?B?UnVkeQ==?=
Guest
Posts: n/a
 
      02-02-2005
Hello all!

I am trying to get my update button to function from my datagrid. But I
keep getting a index out of range error. I see this post alot, and now I'm
proabably more confused about tha ever. When I hit my edit button, it loads
the right fields into my textboxes, there I can change the info, then hit
save. But then the error pop's up. Now I can add a new record with out any
problem. I do not have a textbox for my ID column, which is my key.
To my understanding, it's not reading the "key" column properly.

I guess i'm not sure what I'm looking for. what would be the line of code
that would cause this problem, or what areas may I look at to make sure I
have it set up right. For all I know, I may not even have the right line of
code in there.
I tried the EditItemIndex = e.item thing, but I'm not sure where to put it.

I'm sorry I don't have my code with me right now, but I can post later
tonite if needed. I know it makes it alot easier to figure out.

As always, thanks!

Rudy
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Sm9u?=
Guest
Posts: n/a
 
      02-02-2005
Rudy,

I think it's probably best to post your code. The error typically means
you're trying to update a row (item) but the index or row positions that
you've set is greater than the total number of rows.

Jon

"Rudy" wrote:

> Hello all!
>
> I am trying to get my update button to function from my datagrid. But I
> keep getting a index out of range error. I see this post alot, and now I'm
> proabably more confused about tha ever. When I hit my edit button, it loads
> the right fields into my textboxes, there I can change the info, then hit
> save. But then the error pop's up. Now I can add a new record with out any
> problem. I do not have a textbox for my ID column, which is my key.
> To my understanding, it's not reading the "key" column properly.
>
> I guess i'm not sure what I'm looking for. what would be the line of code
> that would cause this problem, or what areas may I look at to make sure I
> have it set up right. For all I know, I may not even have the right line of
> code in there.
> I tried the EditItemIndex = e.item thing, but I'm not sure where to put it.
>
> I'm sorry I don't have my code with me right now, but I can post later
> tonite if needed. I know it makes it alot easier to figure out.
>
> As always, thanks!
>
> Rudy

 
Reply With Quote
 
 
 
 
=?Utf-8?B?UnVkeQ==?=
Guest
Posts: n/a
 
      02-03-2005
Hi Jon!
Here is the code that I have.

Private Sub SaveItem()

Dim strSQL As String

If btnSave.CommandArgument = "Add" Then
strSQL = _
"INSERT INTO IMSProducts " & _
" (ProductName, SupplierPart, UnitCost, UnitsInStock, " & _
" Discontinued, UnitSRP, Used) " & _
"VALUES " & _
" (@ProductName, @SupplierPart, @UnitCost, @UnitsInStock,
" & _
" @Discontinued, @UnitSRP, @Used)"
Else ' The user is updating an existing item.
strSQL = _
"UPDATE IMSProducts " & _
"SET ProductName = @ProductName, " & _
" SupplierPart = @SupplierPart, " & _
" UnitSRP = @UnitSRP, " & _
" UnitCost = @UnitCost, " & _
" UnitsInStock = @UnitsInStock, " & _
" Discontinued = @Discontinued, " & _
" Used = @Used " & _
"WHERE ProductID = @ProductID"

End If

Dim Sqlconnection1 As New SqlConnection(SQL_CONNECTION_STRING)
Dim scmd As New SqlCommand(strSQL, Sqlconnection1)

' Add all the required SQL parameters.
With scmd.Parameters
' The ProductID parameter is only needed for updating.
If btnSave.CommandArgument <> "Add" Then
.Add(New SqlParameter("@ProductID", _
SqlDbType.Int)).Value = _

CInt(grdProducts.DataKeys(grdProducts.SelectedInde x).ToString)
' grdProducts.EditItemIndex = -1
End If

.Add(New SqlParameter("@ProductName", _
SqlDbType.NVarChar, 40)).Value = txtProductName.Text
.Add(New SqlParameter("@SupplierPart", _
SqlDbType.NVarChar, 50)).Value = txtSupplier.Text
.Add(New SqlParameter("@UnitCost", _
SqlDbType.Money)).Value = CDbl(txtCost.Text)
.Add(New SqlParameter("@UnitSRP", _
SqlDbType.Money)).Value = CDbl(txtSRP.Text)
.Add(New SqlParameter("@UnitsInStock", _
SqlDbType.Int)).Value = CInt(txtInStock.Text)
.Add(New SqlParameter("@Discontinued", _
SqlDbType.Bit)).Value = chkDiscontinued.Checked
'.Add(New SqlParameter("@New", _
' SqlDbType.Bit)).Value = chkNew.Checked
.Add(New SqlParameter("@Used", _
SqlDbType.Bit)).Value = chkUsed.Checked
End With

Try
Sqlconnection1.Open()
scmd.ExecuteNonQuery()

Cache.Remove("dvProducts")
BindProductsGrid()

strMsg = "Your a star! Product successfully saved to the
database."
pnlForm.Visible = True



Catch exp As Exception
strErrorMsg = "Database error! Product not saved to the " & _
"database. Error message: " & exp.Message
Finally
Sqlconnection1.Close()
End Try
End Sub
Here is the code for the save btn.
' This routine handles the "Save Changes" button Click event.
Private Sub btnSave_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSave.Click


If IsValid Then
SaveItem()
txtProductName.Text = ""
txtSupplier.Text = ""
txtCost.Text = ""
txtSRP.Text = ""
txtInStock.Text = ""
chkDiscontinued.Checked = False
chkUsed.Checked = False


End If
End Sub

I hope this makes a little more sense.
Thank you for your time!!

Rudy



"Jon" wrote:

> Rudy,
>
> I think it's probably best to post your code. The error typically means
> you're trying to update a row (item) but the index or row positions that
> you've set is greater than the total number of rows.
>
> Jon
>
> "Rudy" wrote:
>
> > Hello all!
> >
> > I am trying to get my update button to function from my datagrid. But I
> > keep getting a index out of range error. I see this post alot, and now I'm
> > proabably more confused about tha ever. When I hit my edit button, it loads
> > the right fields into my textboxes, there I can change the info, then hit
> > save. But then the error pop's up. Now I can add a new record with out any
> > problem. I do not have a textbox for my ID column, which is my key.
> > To my understanding, it's not reading the "key" column properly.
> >
> > I guess i'm not sure what I'm looking for. what would be the line of code
> > that would cause this problem, or what areas may I look at to make sure I
> > have it set up right. For all I know, I may not even have the right line of
> > code in there.
> > I tried the EditItemIndex = e.item thing, but I'm not sure where to put it.
> >
> > I'm sorry I don't have my code with me right now, but I can post later
> > tonite if needed. I know it makes it alot easier to figure out.
> >
> > As always, thanks!
> >
> > Rudy

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I transform source range to destination range that is thesame as source? Lambda C++ 2 07-16-2008 05:18 PM
range() is not the best way to check range? Summercoolness@gmail.com Python 46 07-25-2006 08:10 PM
Index was out of range ERROR <NEWBIE ERROR> =?Utf-8?B?UnVkeQ==?= ASP .Net 4 07-06-2005 05:35 PM
Scene range vs dynamic range Robert Feinman Digital Photography 2 07-04-2005 09:30 PM
Range does not take an Range object. Tomoyuki Kosimizu Ruby 3 11-27-2003 12:42 AM



Advertisments