Newbie-Index out of range error

G

Guest

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
 
G

Guest

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
 
G

Guest

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.SelectedIndex).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
 

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

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top