Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. [SOLVED] How to update Gridview with value textbox = Null

[SOLVED] How to update Gridview with value textbox = Null

Scheduled Pinned Locked Moved Visual Basic
htmlsysadminhelptutorialannouncement
18 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    zaimah wrote:

    I try to put value '0' if the text box = Null, but it didn't work.
    Its been few days I've been trying to find answers. Hope someone can suggest any idea for me to solve this.

    From

    Dim C1 As Integer = DirectCast(row.FindControl("Label1"), TextBox).Text

    To

    Dim C1S As String = DirectCast(row.FindControl("Label1"), TextBox).Text
    Dim C1 As Integer = Convert.ToInt32(C1S)

    That should do it :)

    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

    Z Offline
    Z Offline
    zaimah
    wrote on last edited by
    #3

    It didn't work with the same error. before this, the error is at:

    Dim C1 As Integer = DirectCast(row.FindControl("Label1"), TextBox).Text

    after i edit using your suggestion, the error is at:

    Dim C1S As String = DirectCast(row.FindControl("Label1"), TextBox).Text

    its as if it didn't even reach the convert part, the error occurred.. I have try similar method, trying to convert the value so that it can update the value Null to my table, but the error is still at the find control.. Is there something wrong with my coding at find control part? But the update is successful, except when the value is NULL.. This really gives me a headache.. Thanks for replying my question :) Is there any solution?

    L 1 Reply Last reply
    0
    • Z zaimah

      It didn't work with the same error. before this, the error is at:

      Dim C1 As Integer = DirectCast(row.FindControl("Label1"), TextBox).Text

      after i edit using your suggestion, the error is at:

      Dim C1S As String = DirectCast(row.FindControl("Label1"), TextBox).Text

      its as if it didn't even reach the convert part, the error occurred.. I have try similar method, trying to convert the value so that it can update the value Null to my table, but the error is still at the find control.. Is there something wrong with my coding at find control part? But the update is successful, except when the value is NULL.. This really gives me a headache.. Thanks for replying my question :) Is there any solution?

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #4

      zaimah wrote:

      its as if it didn't even reach the convert part, the error occurred..

      Put a breakpoint there and find out if it does, or does not execute.

      zaimah wrote:

      Is there something wrong with my coding at find control part?

      That wouldn't change due to a value being empty, would it? Didn't notice before, but you're looking for Label1 and casting it to a TextBox. Shouldn't that be a Label you're casting to?

      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

      Z 1 Reply Last reply
      0
      • L Lost User

        zaimah wrote:

        its as if it didn't even reach the convert part, the error occurred..

        Put a breakpoint there and find out if it does, or does not execute.

        zaimah wrote:

        Is there something wrong with my coding at find control part?

        That wouldn't change due to a value being empty, would it? Didn't notice before, but you're looking for Label1 and casting it to a TextBox. Shouldn't that be a Label you're casting to?

        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

        Z Offline
        Z Offline
        zaimah
        wrote on last edited by
        #5

        the name for textbox is Label1.. Sorry for the confusion :) :) Originally, the gridview only have edit button for each row of data in gridview. So i change my gridview in html. I change all the label to textbox and bound the text box. So i would only have one button to update everything that i key-in in the gridview. After i change the label to textbox, lazy of me, im not changing the name of the previous label ;P Would that effect the coding? But i can update the data. The problem is only when one of the textbox is empty. If the textbox that i cast for C10 is empty, then the error would be on C10, not C1.

        L 1 Reply Last reply
        0
        • Z zaimah

          the name for textbox is Label1.. Sorry for the confusion :) :) Originally, the gridview only have edit button for each row of data in gridview. So i change my gridview in html. I change all the label to textbox and bound the text box. So i would only have one button to update everything that i key-in in the gridview. After i change the label to textbox, lazy of me, im not changing the name of the previous label ;P Would that effect the coding? But i can update the data. The problem is only when one of the textbox is empty. If the textbox that i cast for C10 is empty, then the error would be on C10, not C1.

          L Offline
          L Offline
          Lost User
          wrote on last edited by
          #6

          C1 seems to be always containing NULL; from the code you pasted;

          cmd.Parameters("@c1").Value = Convert.DBNull(C1)

          Shouldn't that be an integer?

          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

          Z 1 Reply Last reply
          0
          • L Lost User

            C1 seems to be always containing NULL; from the code you pasted;

            cmd.Parameters("@c1").Value = Convert.DBNull(C1)

            Shouldn't that be an integer?

            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

            Z Offline
            Z Offline
            zaimah
            wrote on last edited by
            #7

            Not always. If C1-C5 have value, and C6 is empty, then the error would be on C6. The weird thing is, the error only occur at textbox that are empty. It runs OK when there's no empty textbox.

            L 1 Reply Last reply
            0
            • Z zaimah

              Not always. If C1-C5 have value, and C6 is empty, then the error would be on C6. The weird thing is, the error only occur at textbox that are empty. It runs OK when there's no empty textbox.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              That's not weird; you'll have to verify the users' input. If the database does not allow empty values (which it obviously doesn't), then you need to eliminate those before executing the query. It's either checking each value, or modify the database. ..you could copy the example-line, and add something similar to each parameter :)

              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

              Z 1 Reply Last reply
              0
              • L Lost User

                That's not weird; you'll have to verify the users' input. If the database does not allow empty values (which it obviously doesn't), then you need to eliminate those before executing the query. It's either checking each value, or modify the database. ..you could copy the example-line, and add something similar to each parameter :)

                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                Z Offline
                Z Offline
                zaimah
                wrote on last edited by
                #9

                Do u mean the example that u gave me:

                Dim C1S As Integer = DirectCast(row.FindControl("Label1"), TextBox).Text
                Dim C1 As Integer = Convert.ToInt32(C1S)

                I have try using this method. It didn't work. How to check the value 1st and if its Null, put '0' in it? the one that i put convert.dbnull is one of my testing. sorry for the confusion. Sorry, my english s not that good :(

                L 1 Reply Last reply
                0
                • Z zaimah

                  Do u mean the example that u gave me:

                  Dim C1S As Integer = DirectCast(row.FindControl("Label1"), TextBox).Text
                  Dim C1 As Integer = Convert.ToInt32(C1S)

                  I have try using this method. It didn't work. How to check the value 1st and if its Null, put '0' in it? the one that i put convert.dbnull is one of my testing. sorry for the confusion. Sorry, my english s not that good :(

                  L Offline
                  L Offline
                  Lost User
                  wrote on last edited by
                  #10

                  zaimah wrote:

                  Do u mean the example that u gave me:

                  Dim C1S As Integer = DirectCast(row.FindControl("Label1"), TextBox).Text
                  Dim C1 As Integer = Convert.ToInt32(C1S)

                  That's not the example I gave you - the one you shown here tries to assign a string to an integer. This was the example;

                  Dim C1S As String = DirectCast(row.FindControl("Label1"), TextBox).Text
                  Dim C1 As Integer = Convert.ToInt32(C1S)

                  zaimah wrote:

                  Sorry, my english s not that good :(

                  It's good enough; we're making progress :)

                  zaimah wrote:

                  I have try using this method. It didn't work. How to check the value 1st and if its Null, put '0' in it?

                  The problem with the line is that it does multiple things at once; it contains multiple instructions. It get's easier if you divide it into smaller pieces, as smaller problems are easier to conquer. So, first one needs to find the control. You already know how to do that;

                  Dim TextBoxC1 As TextBox = DirectCast(row.FindControl("TextBox1"), TextBox)

                  Then, put a breakpoint on that line, and make sure it can actually find the control. If it doesn't, it might generate an exception, claiming it's null. Once you got the correct control, you get it's value. Keep in mind that textboxes (and labels) contain strings, and that an empty string might be equal to null again. For an integer, we'd do something like the example; Convert.ToInt32 will change a "null" to 0. If you're saving a string, you'd use Convert.ToString. So, the code would continue like this;

                  Dim TextBoxC1 As TextBox = DirectCast(row.FindControl("TextBox1"), TextBox)
                  Dim ValueC1 As String = Convert.ToString(TextBoxC1.Text) ' assuming you save a text, not a number

                  Once you got that, you can check whether "ValueC1" is "null" or an actual value (and replace it if required).

                  Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                  Z 1 Reply Last reply
                  0
                  • L Lost User

                    zaimah wrote:

                    Do u mean the example that u gave me:

                    Dim C1S As Integer = DirectCast(row.FindControl("Label1"), TextBox).Text
                    Dim C1 As Integer = Convert.ToInt32(C1S)

                    That's not the example I gave you - the one you shown here tries to assign a string to an integer. This was the example;

                    Dim C1S As String = DirectCast(row.FindControl("Label1"), TextBox).Text
                    Dim C1 As Integer = Convert.ToInt32(C1S)

                    zaimah wrote:

                    Sorry, my english s not that good :(

                    It's good enough; we're making progress :)

                    zaimah wrote:

                    I have try using this method. It didn't work. How to check the value 1st and if its Null, put '0' in it?

                    The problem with the line is that it does multiple things at once; it contains multiple instructions. It get's easier if you divide it into smaller pieces, as smaller problems are easier to conquer. So, first one needs to find the control. You already know how to do that;

                    Dim TextBoxC1 As TextBox = DirectCast(row.FindControl("TextBox1"), TextBox)

                    Then, put a breakpoint on that line, and make sure it can actually find the control. If it doesn't, it might generate an exception, claiming it's null. Once you got the correct control, you get it's value. Keep in mind that textboxes (and labels) contain strings, and that an empty string might be equal to null again. For an integer, we'd do something like the example; Convert.ToInt32 will change a "null" to 0. If you're saving a string, you'd use Convert.ToString. So, the code would continue like this;

                    Dim TextBoxC1 As TextBox = DirectCast(row.FindControl("TextBox1"), TextBox)
                    Dim ValueC1 As String = Convert.ToString(TextBoxC1.Text) ' assuming you save a text, not a number

                    Once you got that, you can check whether "ValueC1" is "null" or an actual value (and replace it if required).

                    Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                    Z Offline
                    Z Offline
                    zaimah
                    wrote on last edited by
                    #11

                    i did like this

                            Dim C1S As TextBox = DirectCast(row.FindControl("TextBox1"), TextBox)
                            Dim C1 As Integer = Convert.ToInt32(C1S)
                    

                    And the error is this:

                    [InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'.]
                    System.Convert.ToInt32(Object value) +18

                    How can this be? my textbox suppose to have number as input not string. So i used convert.int32. Is this the correct way?

                    L 1 Reply Last reply
                    0
                    • Z zaimah

                      i did like this

                              Dim C1S As TextBox = DirectCast(row.FindControl("TextBox1"), TextBox)
                              Dim C1 As Integer = Convert.ToInt32(C1S)
                      

                      And the error is this:

                      [InvalidCastException: Unable to cast object of type 'System.Web.UI.WebControls.TextBox' to type 'System.IConvertible'.]
                      System.Convert.ToInt32(Object value) +18

                      How can this be? my textbox suppose to have number as input not string. So i used convert.int32. Is this the correct way?

                      L Offline
                      L Offline
                      Lost User
                      wrote on last edited by
                      #12

                      You're right, it's better if we look at them one at a time. A word of warning btw; I'm not a webdeveloper, just leaning on some general VB-knowledge. Exception means that it can't find the control. That has to be fixed first, before we can convert values.

                      Dim C1S As TextBox = DirectCast(row.FindControl("TextBox1"), TextBox)
                      Dim C1 As Integer = Convert.ToInt32(C1S)

                      As you can see, I'm looking for "TextBox1", which would have been "Label1" in your code. You already stated that you renamed it, so if you change it to the code below, it should find it;

                      Dim C1S As TextBox = DirectCast(row.FindControl("Label1"), TextBox)
                      Dim C1 As Integer = Convert.ToInt32(C1S)

                      Like you said, it's a textbox, just named "Label1". The line after that should not throw an error.

                      Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                      Z 1 Reply Last reply
                      0
                      • L Lost User

                        You're right, it's better if we look at them one at a time. A word of warning btw; I'm not a webdeveloper, just leaning on some general VB-knowledge. Exception means that it can't find the control. That has to be fixed first, before we can convert values.

                        Dim C1S As TextBox = DirectCast(row.FindControl("TextBox1"), TextBox)
                        Dim C1 As Integer = Convert.ToInt32(C1S)

                        As you can see, I'm looking for "TextBox1", which would have been "Label1" in your code. You already stated that you renamed it, so if you change it to the code below, it should find it;

                        Dim C1S As TextBox = DirectCast(row.FindControl("Label1"), TextBox)
                        Dim C1 As Integer = Convert.ToInt32(C1S)

                        Like you said, it's a textbox, just named "Label1". The line after that should not throw an error.

                        Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                        Z Offline
                        Z Offline
                        zaimah
                        wrote on last edited by
                        #13

                        I did like your told me.. But the error is still the same.. I try to using this

                                Dim C1S As String = DirectCast(row.FindControl("Label1"), TextBox).Text
                                Dim C1 As Integer = Integer.TryParse(C1S, C1)
                        

                        It works... it can update even with null value textbox. But another probem happens.. All of my value turn as negative in my table.. If i enter 1, it will be -1 in my table..

                        L 1 Reply Last reply
                        0
                        • Z zaimah

                          I did like your told me.. But the error is still the same.. I try to using this

                                  Dim C1S As String = DirectCast(row.FindControl("Label1"), TextBox).Text
                                  Dim C1 As Integer = Integer.TryParse(C1S, C1)
                          

                          It works... it can update even with null value textbox. But another probem happens.. All of my value turn as negative in my table.. If i enter 1, it will be -1 in my table..

                          L Offline
                          L Offline
                          Lost User
                          wrote on last edited by
                          #14

                          TryParse is indeed preferred over Convert.ToInt32. Didn't you find it weird that you had to add C1 as a parameter? It's not returning an integer (a number) but a boolean (y/no). Now, if we cast the boolean to an integer, it'll be -1. Could you try the value "42"? If it returns -42, a sign will be wrong, but I'm expecting it to return -1 also. This should work;

                          Dim C1 As Integer = 0
                          Integer.TryParse(C1S, C1)

                          ..with the boolean indicating whether the conversion to an integer was successful or not (since the string can contain "Hello world", and might not be able to return a number). That could also be used like below;

                          Dim C1 As Integer
                          If Integer.TryParse(C1S, C1) Then
                          'use C1
                          Else
                          'couldn't be converted to a number, insert default
                          End If

                          Out of curiosity; when did you start with programming?

                          Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                          Z 2 Replies Last reply
                          0
                          • L Lost User

                            TryParse is indeed preferred over Convert.ToInt32. Didn't you find it weird that you had to add C1 as a parameter? It's not returning an integer (a number) but a boolean (y/no). Now, if we cast the boolean to an integer, it'll be -1. Could you try the value "42"? If it returns -42, a sign will be wrong, but I'm expecting it to return -1 also. This should work;

                            Dim C1 As Integer = 0
                            Integer.TryParse(C1S, C1)

                            ..with the boolean indicating whether the conversion to an integer was successful or not (since the string can contain "Hello world", and might not be able to return a number). That could also be used like below;

                            Dim C1 As Integer
                            If Integer.TryParse(C1S, C1) Then
                            'use C1
                            Else
                            'couldn't be converted to a number, insert default
                            End If

                            Out of curiosity; when did you start with programming?

                            Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                            Z Offline
                            Z Offline
                            zaimah
                            wrote on last edited by
                            #15

                            ok.. I'm in trouble rite now.. Suddenly it cannot update anything.. :( i have to submit this prgm tomorrow... :(( 1st time i learn doing this kind of prgm last year.. but only did this once. after almost a year, i have to do this project because im studying rite now. I learn to do this in 2 weeks.

                            1 Reply Last reply
                            0
                            • L Lost User

                              TryParse is indeed preferred over Convert.ToInt32. Didn't you find it weird that you had to add C1 as a parameter? It's not returning an integer (a number) but a boolean (y/no). Now, if we cast the boolean to an integer, it'll be -1. Could you try the value "42"? If it returns -42, a sign will be wrong, but I'm expecting it to return -1 also. This should work;

                              Dim C1 As Integer = 0
                              Integer.TryParse(C1S, C1)

                              ..with the boolean indicating whether the conversion to an integer was successful or not (since the string can contain "Hello world", and might not be able to return a number). That could also be used like below;

                              Dim C1 As Integer
                              If Integer.TryParse(C1S, C1) Then
                              'use C1
                              Else
                              'couldn't be converted to a number, insert default
                              End If

                              Out of curiosity; when did you start with programming?

                              Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                              Z Offline
                              Z Offline
                              zaimah
                              wrote on last edited by
                              #16

                              :) It WORKS... yayyyy.. if i put empty textbox it will put 0 to my table... THANK YOU A LOT :) my code would be like this ...

                              Dim C1S As String = DirectCast(row.FindControl("Label1"), TextBox).Text
                              Dim C1 As Integer = 0
                              Integer.TryParse(C1S, C1)

                              Can u pls explain to me how it works? I think i sleep with a smile on my face tonite.. Its 12midnite rite now at my place... I've been building this prgm learning to do it by google.. Cannot find anyone to teach so i turn to Google.. But when this error come out... I cannot find the solution in google... THANK YOU AGAIN :) :) :) :)

                              L 1 Reply Last reply
                              0
                              • Z zaimah

                                :) It WORKS... yayyyy.. if i put empty textbox it will put 0 to my table... THANK YOU A LOT :) my code would be like this ...

                                Dim C1S As String = DirectCast(row.FindControl("Label1"), TextBox).Text
                                Dim C1 As Integer = 0
                                Integer.TryParse(C1S, C1)

                                Can u pls explain to me how it works? I think i sleep with a smile on my face tonite.. Its 12midnite rite now at my place... I've been building this prgm learning to do it by google.. Cannot find anyone to teach so i turn to Google.. But when this error come out... I cannot find the solution in google... THANK YOU AGAIN :) :) :) :)

                                L Offline
                                L Offline
                                Lost User
                                wrote on last edited by
                                #17

                                zaimah wrote:

                                It WORKS..

                                Whehe, well done - read both posts after another.

                                zaimah wrote:

                                Can u pls explain to me how it works?

                                The first instruction finds the control, casts it to a textbox, gets it's value from the Text-property and assigns it to a new variable with the name C1S. That's a whole lot of responsibilities on a single line. The second line declares a new variable (of the integer type) and assigns '0' to it. The third line then tries to assign a number to C1, based on the text that's in C1S.

                                zaimah wrote:

                                I think i sleep with a smile on my face tonite..

                                Rightly so :thumbsup:

                                Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]

                                1 Reply Last reply
                                0
                                • Z zaimah

                                  Hi, its been years i don't do vb. So its like im a junior in this. I have problem update my Gridview. My gridview is to update marks for student. The problem is, the update have problem if the textbox is with a NULL value. If i put 0 in the textbox, the update runs ok. This is my gridview html

                                  <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
                                  <Columns>
                                  <asp:TemplateField HeaderText="Student ID" SortExpression="stID">
                                  <ItemTemplate>
                                  <asp:Label ID="Label11" runat="server" Width="50px" Text='<%# Bind("stID") %>'></asp:Label>
                                  </ItemTemplate>
                                  </asp:TemplateField>
                                  <asp:TemplateField HeaderText="CO1" SortExpression="CO1">
                                  <ItemTemplate>
                                  <asp:TextBox ID="Label1" runat="server" Width="50px" Text='<%# Bind("CO1") %>'></asp:TextBox>
                                  </ItemTemplate>
                                  </asp:TemplateField>
                                  <asp:TemplateField HeaderText="CO2" SortExpression="CO2">
                                  <ItemTemplate>
                                  <asp:TextBox ID="Label2" runat="server" Width="50px" Text='<%# Bind("CO2") %>'></asp:TextBox>
                                  </ItemTemplate>
                                  </asp:TemplateField>
                                  <asp:TemplateField HeaderText="CO3" SortExpression="CO3">
                                  <ItemTemplate>
                                  <asp:TextBox ID="Label3" runat="server" Width="50px" Text='<%# Bind("CO3") %>'></asp:TextBox>
                                  </ItemTemplate>
                                  </asp:TemplateField>
                                  <asp:TemplateField HeaderText="CO4" SortExpression="CO4">
                                  <ItemTemplate>
                                  <asp:TextBox ID="Label4" runat="server" Width="50px" Text='<%# Bind("CO4") %>'></asp:TextBox>
                                  </ItemTemplate>
                                  </asp:TemplateField>
                                  <asp:TemplateField HeaderText="CO5" SortExpression="CO5">
                                  <ItemTemplate>
                                  <asp:TextBox ID="Label5" runat="server" Width="50px" Text='<%# Bind("CO5") %>'></asp:TextBox>
                                  </ItemTemplate>
                                  </asp:TemplateField>

                                  J Offline
                                  J Offline
                                  josephSurgeon
                                  wrote on last edited by
                                  #18

                                  try
                                  if DirectCast(row.FindControl("Label1"), Textbox).Text is Nothing then
                                  'Skip here
                                  End If
                                  Catch ex as exception
                                  'It's Empty move to another control
                                  end try

                                  Hope this helps the idea is to check for a value before trying to use it.

                                  1 Reply Last reply
                                  0
                                  Reply
                                  • Reply as topic
                                  Log in to reply
                                  • Oldest to Newest
                                  • Newest to Oldest
                                  • Most Votes


                                  • Login

                                  • Don't have an account? Register

                                  • Login or register to search.
                                  • First post
                                    Last post
                                  0
                                  • Categories
                                  • Recent
                                  • Tags
                                  • Popular
                                  • World
                                  • Users
                                  • Groups