FacebookTwitter
Hatrack River Forum   
my profile login | search | faq | forum home

  next oldest topic   next newest topic
» Hatrack River Forum » Active Forums » Books, Films, Food and Culture » Deleting rows from a gridview/datatable? (Page 1)

  This topic comprises 3 pages: 1  2  3   
Author Topic: Deleting rows from a gridview/datatable?
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
code:
        Dim CheckBox As CheckBox
Dim KeyId As Int32

For Each GridViewRow As GridViewRow In GridView1.Rows
'---Look at each checkbox here
CheckBox = CType(GridViewRow.FindControl("chkSelector"), CheckBox)

If CheckBox.Checked Then
'---Get the database id
KeyId = CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value, Int32)

'---Use the KeytId to delete from the database.

End If
Next
GridView1.DataBind()

at CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value, Int32)

When i run it to that point it crashes out saying:

quote:

Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index

I am not entirely sure what the issue is, I have my gridview being populated from a datatable. the code for which I made using the session variable like so:

code:
        myDataColumn = New Data.DataColumn
myDataColumn.DataType = GetType(String)
myDataColumn.ColumnName = "Product ID"
myDataColumn.AutoIncrement = False
myDataColumn.Caption = "Product ID"
myDataColumn.ReadOnly = False
myDataColumn.Unique = False
myDataTable.Columns.Add(myDataColumn)

And repeated this several times for each column and then doing this:

code:
            gt = Session("sessionvar")

myrow = gt.NewRow()
myrow(0) = ddlPr_Code.SelectedValue
myrow(1) = ddlPr_Name.SelectedValue
myrow(2) = txtPrice.Text
myrow(3) = txtQuantity.Text
myrow(4) = txtShipping.Text
myrow(5) = (txtQuantity.Text * txtPrice.Text)

gt.Rows.Add(myrow)
GridView1.DataSource = gt
GridView1.DataBind()

voila I have ym gridview populated by what the client wants to "order".

And then i added text boxes and am now trying to figure out how to delete from this gridview but I am not sure how and am currently stumped by the index error.

IP: Logged | Report this post to a Moderator
Mucus
Member
Member # 9735

 - posted      Profile for Mucus           Edit/Delete Post 
What, is your debugger broken?
Posts: 7593 | Registered: Sep 2006  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
I used my debugger and I have no idea what to do about "Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index" error.

IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
I think you may want to see why the index is either negative or not less than the size of the collection.

This is a very basic and common error. edit: and extremely easy to find where it occurs using a debugger.

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
MattP
Member
Member # 10495

 - posted      Profile for MattP   Email MattP         Edit/Delete Post 
It just means that you're asking for a row that doesn't exist - either you're providing a negative row number or a row number greater than the number of rows available. It should be easy to see where that's happening in the debugger.
Posts: 3275 | Registered: May 2007  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
According to the debugger it is the proper row index.
IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
Also according to the debugger RowIndex=0 (for when there's only 1 row for example) and a ? Gridview1.rows shows a count a 1 row.

0 last I checked was less then 1.

IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
Well, it's great to get that fixed then.

Blayne, I know exactly what your problem is. It is very easy to find and fix. Look at the error message and check the conditions it is telling you would cause this error.

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
Easy for you but I don't understand why its not working, the count on number of rows is indeed greater then the current rowindex, I do not understand.

? GridView1.Rows
count 3

? GridView1.Index
2

say i have 3 rows in the GV, and I have checked the chkbox on the third one.

The above is correct but the line of code:

? KeyId = CType(GridView1.DataKeys.Item(GridViewRow1.RowIndex).Value, Integer) (interger or int32 i tried both)

gives me the error, I don't understand why the data supplied is correct but its not working.

IP: Logged | Report this post to a Moderator
MattP
Member
Member # 10495

 - posted      Profile for MattP   Email MattP         Edit/Delete Post 
Do you have any code following the "'---Use the KeytId to delete from the database." that actually deletes rows?
Posts: 3275 | Registered: May 2007  |  IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
Blayne,
In your debug, look at the specific collection you are trying to hit. If there are supposed to be 3 entries, you can try dropping watches onto each of those entries to see if they exist and if they have the values you expect. Do that.

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
no i dont have a delete thing yet. ill try what squickies says in a minute.
IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
hmm im at a different computer now (different lecture) and I cant seem to find the intermediate window in vs2005
IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
k found the immediate window but i cant drop watches and seems to not debug properly on this computer, says its in design mode when it is not.
IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
Watches in the immediate window? You should use the watch window for that.
Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
thats the problem i cant find the watch window, this machine Im on now is really weird. (alot of the stuff turns white when i run it until i click on it)
IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
type ctrl+alt+w 1 when you've hit a breakpoint
Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
says watch is not currently availiable.
IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
quote:
type ctrl+alt+w 1 when you've hit a breakpoint

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
I did, thats the error I got.
IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
I don't know what to tell you then. With standard settings, that should always bring up the watch window in that case.

What do you understand hitting a breakpoint to mean?

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
liie for example the green arrow debug button i can press multiple times and startup multiple isntances of my asp website, the pause and stop buttons dont appear.
IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
k on a proper debuger machine. what am I watching for again?
IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
Blayne,
This is not where I lead you by the hand or give you the answer. I've given you enough to figure this out. You're better off on your own from here.

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
I cant figure it out, it is not figurable out, I do not know anything about how these asp/vb classes/methods work so I do not know where to begin looking or what to look for. How am I supposed to know which .etc does what I want it to do?
IP: Logged | Report this post to a Moderator
Nighthawk
Member
Member # 4176

 - posted      Profile for Nighthawk   Email Nighthawk         Edit/Delete Post 
quote:
gt = Session("sessionvar")
Wha-huh? Am I reading this correctly: you're using a data table stored in the Session object?!? This is probably beyond the scope of what you're learning now, but are you aware that IIS serializes access to the Session object? Not to mention the amount of chaos that will ensue if you do things like double-post the page (hit "F5" repeatedly) by accident. This method sounds... well... painful and risky.

Secondly, when I do something related (I don't do it the way you suggest; I do it the "old fashioned" way), I add a unique identifier in to the data set that's a HIDDEN column in the grid. Therefore, when I'm doing things like this, I don't have to worry about the "DataKeys" collection, but I simply access "GridViewRow[UniqueKey]".

I don't even know that the "DataKeys" collection is, but I don't know why you're checking the size of the "Rows" collection. You should be comparing "GridView1.DataKeys.Count" (not "GridView1.Rows.Count") to "GridViewRow.RowIndex".

I'm not a fan of Microsoft's way of doing this; I usually don't even use the ViewState (which I've seen be monumental in size when I do something similar) in cases like this and just create non-ASP controls with the unique identifier as the value.

Posts: 3486 | Registered: Sep 2002  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
how do I compare "GridView1.DataKeys.Count" to "GridViewRow.RowIndex"?
IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
If you weren't you, I wouldn't believe that that was a serious question. For pete's sake, think for yourself.
Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
I don't know what I am doing, how the heck would I know what to do? Thinking accomplishes nothing if no amount of thinking determins what the frak am I supposed to do and google fails on me.
IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
I'm going to give you the benefit of the doubt and figure you're just really stressed out.

Answer this question. Why would you want to compare GridView1.DataKeys.Count to GridViewRow.RowIndex?

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Morbo
Member
Member # 5309

 - posted      Profile for Morbo   Email Morbo         Edit/Delete Post 
People got stuff done before google, Blayne. Debugging sucks but keep plugging.
Good luck. [Smile]

Posts: 6316 | Registered: Jun 2003  |  IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
Um. You know, Blayne, if you wanted to inspire my confidence that I want to work with you on the converter project, this was kind of not the way to do it. But let me give you a hint anyway. When people say 'think', they don't mean that you should try figuring out the Grand Unified Theory from first principles, they mean that you should step through the code by hand. You do understant what I mean by that, yes?

*Walks off muttering about schools these days*

Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
El JT de Spang
Member
Member # 7742

 - posted      Profile for El JT de Spang   Email El JT de Spang         Edit/Delete Post 
quote:
Originally posted by Blayne Bradley:
Thinking accomplishes nothing if no amount of thinking determins what the frak am I supposed to do and google fails on me.

Thinking also accomplishes nothing if you've no practice at it. I can't imagine how you expect to be a competent coder if you're not willing to spend a few hours debugging your own faulty code.
Posts: 5462 | Registered: Apr 2005  |  IP: Logged | Report this post to a Moderator
Mucus
Member
Member # 9735

 - posted      Profile for Mucus           Edit/Delete Post 
Indeed. It will even be more amazing when Blayne has to debug other people's faulty code.
Posts: 7593 | Registered: Sep 2006  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
In the end I think ill just use KeyId = GridViewRow1.RowIndex tha gives what row we want to delete.

however my deletion commands dont wanna work.

The GridView 'GridView1' fired event RowDeleting which wasn't handled.

What does wasn't handled mean?

IP: Logged | Report this post to a Moderator
King of Men
Member
Member # 6684

 - posted      Profile for King of Men   Email King of Men         Edit/Delete Post 
quote:
Originally posted by Blayne Bradley:
I cant figure it out, it is not figurable out, I do not know anything about how these asp/vb classes/methods work so I do not know where to begin looking or what to look for. How am I supposed to know which .etc does what I want it to do?

Ah. That is why there is this nifty thing called documentation. The only way to learn an API is to

a) Use it (you got that part) and
b) Look up what the devil it is actually doing.

I might add that you appear to be learning at the wrong level. You are figuring out - slowly, with many 'Hatrack lookups' and probably much pestering of TAs, how to solve specific problems using buggy, fragile, impossible-to-maintain code. What you ought to be learning is how to think about solving problems using an unfamiliar API/language/toolset. You ought to have a general process for becoming familiar with an API at least at the overview level (not too easy with the vast libraries Microsoft puts out, I admit). You should know roughly the methods that each class you use often offers, and preferably have a rough idea of what they do. The only way to get this is to look at the documentation, not just in a frantic search for what you need right now, but also as the literature of your field, to get some idea of what's there.

A further point is that you appear to be suffering from the ill effects of this problem at the more basic level as well. You don't just have a problem with APIs, you have a similar problem in that you didn't learn to think about algorithms for problem-solving in a general way, you only learned to solve specific problems - using bad, buggy code and many Hatrack lookups. (I recall spending three hours in chat trying to make you understand pointers. Can I have them back, please?) To make an analogy with math, you never learned to do addition, you just learned that '10' was the correct answer if the question was '5+5'.

You have got to go back to basics, learn how to write pseudo-code that expresses what the problem is and how you intend to solve it, and learn to program, not to hack together specific solutions.

Posts: 10645 | Registered: Jul 2004  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
Exams coming up no time to "go back to basics" and the exams require the work having actually being done since they open book open notes.

As for those hours we were playing CK at the same time, so no soup for you!

IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
here's my current code:

code:
        For Each GridViewRow1 As GridViewRow In GridView1.Rows
'---Look at each checkbox here
CheckBox1 = CType(GridViewRow1.FindControl("chkSelector"), CheckBox)
Dim instance As New GridViewRowEventHandler

If CheckBox1.Checked Then
chkBox = True

'Dim ProductID As String = GridView1.DataKeys(GridViewRow1.RowIndex)("pr_code").ToString
'---Get the database id
KeyId = GridViewRow1.RowIndex
'KeyId = CType(GridView1.DataKeys.Item(GridViewRow1.RowIndex).Value, Int32)
'---Use the KeytId to delete from the database.
'GridView1.DeleteRow(KeyId)

gt.Rows(KeyId).Delete()
GridView1.DeleteRow(KeyId)



End If
Next
GridView1.DataSource = gt
GridView1.DataBind()

gt.Rows(KeyId).Delete()
GridView1.DeleteRow(KeyId)

there lines aren't working right. Is the second line the correct way to delete a gridview row, or am i doing ti wrong.

IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
The ASP course doesn't teach us anything bout methods, how to use them or what they do, were taught which batches of ccde are used to accomplish which task and are assigned to slowly build an ASP website using the code/pseudo code given in class.

The Learn through google approch is what were more or less learning for the ASP/User Interface course.

IP: Logged | Report this post to a Moderator
mr_porteiro_head
Member
Member # 4644

 - posted      Profile for mr_porteiro_head   Email mr_porteiro_head         Edit/Delete Post 
quote:
Exams coming up no time to "go back to basics" and the exams require the work having actually being done since they open book open notes.
If you don't have the basics and the exams are coming up, then you really do need to go back to basics.
Posts: 16551 | Registered: Feb 2003  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
tests are open book, open notes for ASP, what matters is doing the work as the tests are based on the work.
IP: Logged | Report this post to a Moderator
Pegasus
Member
Member # 10464

 - posted      Profile for Pegasus   Email Pegasus         Edit/Delete Post 
Out of morbid curiosity, and since I know nothing on the subject, what is it that you are coding?
Posts: 369 | Registered: Apr 2007  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
a website where people can buy stock for their franchise im doign vidoe games.
IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
okay by doing what I am doing I can now successfully delete rows it just cant delete multiple rows at once.
IP: Logged | Report this post to a Moderator
TomDavidson
Member
Member # 124

 - posted      Profile for TomDavidson   Email TomDavidson         Edit/Delete Post 
Blayne, tell me: do you ever talk to the other students in your class? How do THEY solve these problems?
Posts: 37449 | Registered: May 1999  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
google or ask other people.
IP: Logged | Report this post to a Moderator
MrSquicky
Member
Member # 1802

 - posted      Profile for MrSquicky   Email MrSquicky         Edit/Delete Post 
Blayne,
I don't know what else you've accomplished through this thread, but you've made me regret trying to help you.

Being able to debug a program is pretty much the most important, necessary skill in programming. It is absolutely vital that you get much, much better at it than you have shown if you ever expect to make a living as a programmer. I've lost any interest in trying to help you develop it though.

Posts: 10177 | Registered: Apr 2001  |  IP: Logged | Report this post to a Moderator
Blayne Bradley
unregistered


 - posted            Edit/Delete Post 
This isn't about that, this is vb/asp completely different.

This is just 1 tiny problem and while learning to debug programs is a valuable skill and something I am decent at in any other language VB/ASP pisses the hell out of me because it is overly complicated, we were not taught what ANYTHING does and are actively encouraged to figure things out through using google, this is not a course where your high handed and impractical advice of "going back to the basics" would work especially when there are higher priorities, I am under alot of stress to get work done, saying that "you know exctly of what is wrong" and then saying you wont help me at all except through abstract advice that I don't even know how to begin to understand or apply to code is just plain stupid at worst and arrogant at best.

The way I have been taught is shown how by what is wrong, followed by why it went wrong, ending which being shown how to avoid that in the future. The whole throwing me on my own to tackle an issue that I have spent ten hours trying to figure out on my own before coming to Hatrack will not work and will never work.

quote:
but you've made me regret trying to help you.
This is stupidously rude. How the heck do you expect people to treat you seriously when you say such things.
IP: Logged | Report this post to a Moderator
Mucus
Member
Member # 9735

 - posted      Profile for Mucus           Edit/Delete Post 
Here's a thought: lashing out at the one person that has had the patience to try to walk you through solving your problem, unpaid and unrewarded, is not exactly going to encourage anyone to help you.
Posts: 7593 | Registered: Sep 2006  |  IP: Logged | Report this post to a Moderator
MattP
Member
Member # 10495

 - posted      Profile for MattP   Email MattP         Edit/Delete Post 
quote:
This is just 1 tiny problem and while learning to debug programs is a valuable skill and something I am decent at in any other language VB/ASP pisses the hell out of me because it is overly complicated
I've only barely played with VB/ASP and it was years ago and not even the .NET flavor which is really its own beast. Despite that, it was quite clear to me how this problem should have been addressed and it never would have occurred to me to ask Hatrack for help. This is not a language problem - it *is* about fundamentals. Index out of bounds errors occur in all major languages and the process for resolving them is going to be the same - debug and learn the API.

No one is trying to give you a hard time just for the sake of giving you a hard time. You're just getting a bunch of momma bird programmers trying to push you out of the nest so you can just use those wings you've already got and fly on your own. It'll be so much more satisfying than getting carried around everywhere.

Posts: 3275 | Registered: May 2007  |  IP: Logged | Report this post to a Moderator
  This topic comprises 3 pages: 1  2  3   

   Close Topic   Feature Topic   Move Topic   Delete Topic next oldest topic   next newest topic
 - Printer-friendly view of this topic
Hop To:


Contact Us | Hatrack River Home Page

Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2