[SOLVED] How to update Gridview with value textbox = Null
-
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[^]
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 :(
-
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 :(
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 tonull
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 numberOnce 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[^]
-
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 tonull
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 numberOnce 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[^]
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) +18How can this be? my textbox suppose to have number as input not string. So i used convert.int32. Is this the correct way?
-
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) +18How can this be? my textbox suppose to have number as input not string. So i used convert.int32. Is this the correct way?
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[^]
-
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[^]
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..
-
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..
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 IfOut of curiosity; when did you start with programming?
Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]
-
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 IfOut of curiosity; when did you start with programming?
Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]
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.
-
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 IfOut of curiosity; when did you start with programming?
Bastard Programmer from Hell :suss: if you can't read my code, try converting it here[^]
:) 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 :) :) :) :)
-
:) 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 :) :) :) :)
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[^]
-
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>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 tryHope this helps the idea is to check for a value before trying to use it.