Showing posts with label update. Show all posts
Showing posts with label update. Show all posts

Thursday, March 22, 2012

Newbie-Index out of range error

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!

RudyRudy,

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
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

Newie needs data access

I was not sure which group deals with this is I apologize.
I need to connect to an MS Access DB and add. delete & update from an ASPX
page (using vb.net for code behind if needed)

Are there any sites that show code snippets ?
Both DSN & DSNless connections please

I can find them for ASP but none for aspx
Looks like aspx uses ado.net instead of ado & code is probably different if
only slightly (I don't know)

Thanks a lot in advance.Hi Ralf,

ASPX (that is, ASP .NET) pages indeed use ADO .NET. ADO .NET, in turn, can
talk to the Microsoft Jet OLEDB provider through its Managed OLEDB Provider
found in the System.Data.OleDB namespace. As far as I remember DSNs are
related to ODBC and ODBC is not a native way of working with MS Access
databases. So, you will be using OLEDB-compatible connection strings as if
you were working with the Jet OLEDB provider.

You can find information and examples on working with ADO .NET in the
"Accessing Data with ADO.NET" on the MSDN Library
(http://msdn.microsoft.com/library).

--
Dmitriy Lapshin [C# / .NET MVP]
X-Unity Test Studio
http://x-unity.miik.com.ua/teststudio.aspx
Bring the power of unit testing to VS .NET IDE

"ralf" <--__0ralfr@.yahoo.com> wrote in message
news:bluefh$gisue$1@.ID-158805.news.uni-berlin.de...
> I was not sure which group deals with this is I apologize.
> I need to connect to an MS Access DB and add. delete & update from an ASPX
> page (using vb.net for code behind if needed)
> Are there any sites that show code snippets ?
> Both DSN & DSNless connections please
> I can find them for ASP but none for aspx
> Looks like aspx uses ado.net instead of ado & code is probably different
if
> only slightly (I don't know)
> Thanks a lot in advance.
Thanks for posting but unfortunately I have no time to reinvent the wheel by
studying ado.net from scratch
I posted because someone may already have written the code that I can reuse
and that is what I am
looking for .

"Dmitriy Lapshin [C# / .NET MVP]" <x-code@.no-spam-please.hotpop.com> wrote
in message news:etZaLeNjDHA.4048@.TK2MSFTNGP11.phx.gbl...
> Hi Ralf,
> ASPX (that is, ASP .NET) pages indeed use ADO .NET. ADO .NET, in turn, can
> talk to the Microsoft Jet OLEDB provider through its Managed OLEDB
Provider
> found in the System.Data.OleDB namespace. As far as I remember DSNs are
> related to ODBC and ODBC is not a native way of working with MS Access
> databases. So, you will be using OLEDB-compatible connection strings as if
> you were working with the Jet OLEDB provider.
> You can find information and examples on working with ADO .NET in the
> "Accessing Data with ADO.NET" on the MSDN Library
> (http://msdn.microsoft.com/library).
> --
> Dmitriy Lapshin [C# / .NET MVP]
> X-Unity Test Studio
> http://x-unity.miik.com.ua/teststudio.aspx
> Bring the power of unit testing to VS .NET IDE
> "ralf" <--__0ralfr@.yahoo.com> wrote in message
> news:bluefh$gisue$1@.ID-158805.news.uni-berlin.de...
> > I was not sure which group deals with this is I apologize.
> > I need to connect to an MS Access DB and add. delete & update from an
ASPX
> > page (using vb.net for code behind if needed)
> > Are there any sites that show code snippets ?
> > Both DSN & DSNless connections please
> > I can find them for ASP but none for aspx
> > Looks like aspx uses ado.net instead of ado & code is probably different
> if
> > only slightly (I don't know)
> > Thanks a lot in advance.