Click here to Skip to main content
16,015,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am having a textbox with datatype double,inside the submit button.After clicking on the button,i want to convert blank textbox into int value 0.
for that i did something but how to set 0 int value in that box not getting.
below sending the code pls check it out and if any suggestion is there pls send me.
C#
protected void btnsubmit2_Click(object sender, ImageClickEventArgs e)
{
    double stotal_recpt2;  
    if(stotal_recpt2 = "")
    {
        stotal_recpt2.text = "0";
    } 
    else
    {
        Convert.ToDouble(txttotal_recpt2.Text.Trim());
    }
}


the textbox is double and i am sending string value ok there is a bug,
pls help me how blank textbox convert to 0 value.

thanks.
Posted
Updated 11-Jun-20 22:29pm
v2

protected void btnsubmit2_Click(object sender, ImageClickEventArgs e)
{

double stotal_recpt2;

//(0 + textBox1.Text) is a string block and concatenate 0 with text box value
// If texbox is empty then string block is (0)
// If text box has a value 123.5 then string block is (0123.5)
// At the above (0) and (0123.5) cases and by using the following method,
// stotal_recpt2 is equal to 0 and 123.5
// If text box has value as text or other charactors or unrecognizable pattern
// then it goes 0 by catch block

try
{
stotal_recpt2 = System.Convert.ToDouble(0 + textBox1.Text);
}
catch
{
stotal_recpt2 = 0;
}
}
 
Share this answer
 
Comments
CHill60 12-Jun-20 5:37am    
It is bad practice to code to exceptions like this. See solution 4 - use Tryparse instead
XML
<script type="text/javascript">
function Value() {
    var txtbox1 = document.getElementById("txtbox1");
    txtbox1.value = 0;
}
</script>


<form>
<input type="textbox" id="txtbox1">
<input type="button" value="value -->" onclick="Value();">
</form>
 
Share this answer
 
Lets first assume you have a little typo, and that you have a textbox called txtRecpt. I propose the following (and safest, imo) solution:

C#
double stotal_recpt2 = 0;
double.TryParse(txtRecpt.Text, out stotal_recpt2);

//Do what you need to with stotal_recpt2  


Hope this helps,

--Dom
 
Share this answer
 
"i am having a textbox with datatype double"

No I am afraid that you do not. Textbox's contain strings not doubles. As Richard said, there is plenty of errors in your code. But to answer your question:

C#
protected void btnsubmit2_Click(object sender, ImageClickEventArgs e)
{
    double stotal_recpt2;
    if (string.IsNullOrEmpty(txttotal_recpt2.Text)
    {
        stotal_rectp2 = 0;
    }

    // Do something with stotal_recpt2;
    
}
 
Share this answer
 
Your code seems to be rather random. You create the uninitialised variable double stotal_recpt2; and then try to test if it contains the text value "". You then try to set a text property of that object which does not exist. And in your else clause you convert some text value to a double and immediately throw it away.
 
Share this answer
 
hi dear,

Double can not compare with string like ""



C#
double stotal_recpt2;
if(stotal_recpt2 = "")

if(Convert.ToDouble(stotal_recpt2) = 0;)
{
stotal_recpt2.text = "0";
}

else
{
Convert.ToDouble(txttotal_recpt2.Text.Trim());
}
}



I hope It Will Helpfull to you

Mahesh Patel
 
Share this answer
 
v2
Comments
[no name] 3-May-12 7:42am    
Why are you converting a double to a double here?
Mr. Mahesh Patel 4-May-12 0:10am    
i am sorry
see below line in above code

if(Convert.ToDouble(stotal_recpt2.Text) = 0;)

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900