Click here to Skip to main content
13,701,775 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
I have a small error. I'm not able to understand. According to me the code is correct but it is not running. plz Please help me.code is here


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
      a = Val(TextBox1.Text)
      b = Val(TextBox2.Text)
      DataGridView1.Rows.Add(b)

  End Sub

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

      DataGridView1.Item(j, i).Value = TextBox2.Text
      i = i + 1
      If i = Val(TextBox1.Text) Then

          j = j + 1
          i = 0
      End If
  End Sub

  Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
      For i = 0 To b - 1
          x = 0
          If i < b Then
              x = x + DataGridView1.Item(j, i).Value * DataGridView1.Item(j + 1, i + 1).Value
          ElseIf i = b - 1 Then

              x1 = x + DataGridView1.Item(j, b - 1).Value * DataGridView1.Item(j + 1, 0).Value
              i = i + 1
          End If

      Next (i)
      TextBox4.Text = Str(x1)
End Sub
Posted 4-Jun-12 7:11am
Updated 4-Jun-12 8:12am
v3
Comments
ledtech3 4-Jun-12 14:25pm
   
After adding extra code to get it to load i got the error at this line
x = x + DataGridView1.Item(j, i).Value * DataGridView1.Item(j + 1, i + 1).Value

Try setting a break point there and hover the pointer over i,j,b, x1, and oters to see what the values are. you may need to step once to get the error and to gett the values.
That should point you to the problem.
ledtech3 4-Jun-12 15:43pm
   
I’m not sure what you are trying to do here.
Using and testing the code you supplied along with what I added to get it to load.
The button1
Click is taking the number from textbox 2 and creating that many rows, and 1 column
If you keep clicking on button 1 then it keeps adding rows and columns till it gets an error.
Button 2
Click will keep adding the value in from textbox2 to the next row till amount rows in that text box -1 and you get an out of range exception.
Button 3
Who know what you are trying to do here.
You appear to be trying to multiply two cells of the datagridview but unless you clicked button1 and button 2 multi times you only have 1 Cell with data in it.
You keep reusing the same letters and nothing is declared in the click events so they must be global variables.
Such as Private i as Integer
Also you need to add more error handling which could help you figure out what is going on.
Such as try catch blocks
Personally while I am testing I like to add extra throw away text boxes to see the values that I am getting at different points.
Takes a little more time in the beginning but can point you to problems quicker some times.
Just remember to get rid of the references in the code when you get rid of the text boxes.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

For i = 0 To b - 1

When b is nothing equals 0 how do you want to substract 1 from 0 ???
You must add something like this;

If Val(Textbox2.Text) > 0 Then
...write your code
For i = 0 To b - 1
....
End If
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Your code makes a lot of assumptions about what's in those textboxes. It's also making assumptions about what's in the DGV too.

Verify EVERYTHING before you going using the values in those boxes. Do they have values? Are they valid values? Does the DGV have as many rows in it as the user is specifying? You're not making any of these checks before you attempt to use the values in the DGV specified by the data coming in.
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180906.1 | Last Updated 4 Jun 2012
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100