-3
    Dim Query2 As String
             For i As Integer = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
                Dim Query2 As String
   ****error***Query2 = "Update booklist SET quantity=(quantity-1) where quantity = '" & Me.DataGridView1.SelectedRows(i).Cells(0).Value.ToString() & "'"***error***
                COMMAND = New MySqlCommand(Query, MysqlConn)
                SDA.SelectCommand = COMMAND
                SDA.Fill(dbDataSet)
                bSource.DataSource = dbDataSet
                DataGridView1.DataSource = bSource
                SDA.Update(dbDataSet)

I am trying to deduct 1 quantity of a book in the database when clicked in datagridview, but Im having this type of error. What should i do? any suggestions please.

G. Vister
  • 9
  • 1
  • 7
  • 2
    I understand your position. You are a free spirit who doesn't want to follow the inflexible rules of the oppressive (VB.NET) system. You want your indices to be free as the wind and to take any value they wish! I am with you, sister/brother! Unfortunately VB.NET is too strong and we cannot apply our free-indices ideas without some consequences... I am afraid that you will have to adapt your code to the VB.NET tirany, sorry about that :) – varocarbas Nov 30 '15 at 12:11
  • i understand sir, but i am just a newbie programmer. – G. Vister Nov 30 '15 at 12:25
  • Sir, this point (you being unexperienced with VB.NET) was crystal clear to me since the first moment; although this fact is not relevant here as far as the error is quite descriptive and the solution evident. SO is not a place to learn basics (if you don't know enough, you should be learning rather than asking here), but to ask questions which cannot easily be found somewhere else. An error message clearly indicating what the problem is (and how to solve it) doesn't belong to SO. – varocarbas Nov 30 '15 at 12:55
  • Well if it is clear for you sir why are wasting your time commenting on my problem?? because for me it is not clear what could be the error is. And just want to tell you sir that you are not helping. You are just teasing us newbie programmers. Sorry about that also :) – G. Vister Nov 30 '15 at 13:05
  • It is kind of funny, sir, because precisely yesterday I started a Meta thread whose goal was precisely improving the communication with newbies (mainly in SO, but also to programming). My proposal didn't get any support (right the contrary) because apparently, sir, quite a few people here think that newbies being scared from SO is a good thing. You, sir, as most of not-knowledgeable-enough-on-certain-matter people, don't know what is best for you. Asking here each single detail is certainly not. In this case, you, sir, have brought such a behaviour to its pure limits... – varocarbas Nov 30 '15 at 13:16
  • ... by referring to an error message which is extremely clear. Believe me, sir, if you don't understand what this error message means, you would need to do some serious learning (= by your own, rather than by asking each single bit). Logically, sir, you are free to not follow my advice; the problem is that SO doesn't have to bear your (sir) lack of understanding of the reality. In summary: asking in SO is not good for you; getting a custom-answer for this problem is not good for you; and, on top of everything, your behaviour is not good for SO. Sorry about that. – varocarbas Nov 30 '15 at 13:20
  • Possible duplicate of [What is IndexOutOfRangeException and how do I fix it?](http://stackoverflow.com/questions/20940979/what-is-indexoutofrangeexception-and-how-do-i-fix-it) – Bjørn-Roger Kringsjå Nov 30 '15 at 13:21
  • "not-knowledgeable-enough-on-certain-matter people" – G. Vister Nov 30 '15 at 13:28
  • Any problem to understand my reference, sir? Do you need examples? Example 1: "driving a car"; a person not knowing how to drive a car is "not-knowledgeable-enough-on-driving-cars" and this person doesn't know what is the best way to learn to drive a car (unless this person is in denial). Example 2: "swimming"; a person not knowing how to swim is "not-knowledgeable-enough-on-swimming" and this person doesn't know what is the best way to learn to swim (unless this person is in denial). Please, sir, let me know if you need more examples to understand this sentence or any other issue. – varocarbas Nov 30 '15 at 13:33

2 Answers2

-1

Your update sql statement is incorrect. The Values keyword is only correct when inserting new rows. Is assume you want to have a where clause with a BookListId set to your selected row:

Query2 = "Update booklist SET quantity=(quantity-1) Where  <BookListIdOrSomething>  = '" & Me.DataGridView1.SelectedRows(i).Cells(0).Value.ToString() & "'"

EDIT: The index error does not come from the wrong SQL but it would concern you later anyway. The index exception comes from accessing .Cells(0) which does not exist in the context according to the error message. But without further info or code about DataGridView1 it´s hard to help.

Alex B.
  • 2,145
  • 1
  • 16
  • 24
  • What does Me.DataGridView1.SelectedRows(i).Cells return during debugging? – Alex B. Nov 30 '15 at 12:25
  • i is returning zero sir. i dont know how to get the value of quantity column. is there any suggestion sir? – G. Vister Nov 30 '15 at 12:46
  • You can access the Cells Collection also with the Column name. So e.g. if your column in the grid is called Quantity you would access it like `DataGridView1.SelectedRows(i).Cells("Quantity").Value`. How many columns do you have in your grid? – Alex B. Nov 30 '15 at 13:29
  • Thank you for your effort sir, I have 6 columns sir. – G. Vister Nov 30 '15 at 13:33
  • what is the result of getting the value by column name? – Alex B. Nov 30 '15 at 13:40
-1
      Dim Query2 As String
         For i As Integer = Me.DataGridView1.SelectedRows.Count - 1 To 0 Step -1
            Dim Query2 As String
            Query2 = "Update booklist SET quantity=(quantity-1) where quantity = '" & Me.DataGridView1.SelectedRows(i).Cells(0).Value.ToString() & "'"
            COMMAND = New MySqlCommand(Query2, MysqlConn)
            SDA.SelectCommand = COMMAND
            SDA.Fill(dbDataSet)
            bSource.DataSource = dbDataSet
            DataGridView1.DataSource = bSource
            SDA.Update(dbDataSet)

Finally found it!The error is in line 5. Sorry for the disturbance and thank you for suggestions.

G. Vister
  • 9
  • 1
  • 7