Newbie-Index out of range error

Discussion in 'ASP .Net' started by =?Utf-8?B?UnVkeQ==?=, Feb 2, 2005.

  1. 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
     
    =?Utf-8?B?UnVkeQ==?=, Feb 2, 2005
    #1
    1. Advertising

  2. 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
     
    =?Utf-8?B?Sm9u?=, Feb 2, 2005
    #2
    1. Advertising

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



    "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
     
    =?Utf-8?B?UnVkeQ==?=, Feb 3, 2005
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Clark Choi
    Replies:
    1
    Views:
    4,550
    Alvin Bruney
    Jan 10, 2004
  2. =?Utf-8?B?UnVkeQ==?=

    Index was out of range ERROR <NEWBIE ERROR>

    =?Utf-8?B?UnVkeQ==?=, Jul 6, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    529
    =?Utf-8?B?UnVkeQ==?=
    Jul 6, 2005
  3. =?Utf-8?B?RGF2ZQ==?=

    Intermittent "Index was out of range." error

    =?Utf-8?B?RGF2ZQ==?=, Jul 20, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    775
    =?Utf-8?B?RGF2ZQ==?=
    Jul 21, 2005
  4. Pat
    Replies:
    0
    Views:
    470
  5. Pat
    Replies:
    0
    Views:
    10,796
Loading...

Share This Page