Incorrect Entry into database
I have a dropdownlist (with values 'CMC' & 'CHF')and two textbox's. when i select an option from dropdownlist, a text box control appears (visibility is written in javascript). when i enter a number into this textbox and hit the sumbit/next button it should save this information in database. The logic works for the one option but its not working for the other! Both the options have textbox's assoicated with them, which are visible only when the respective option is selected. the frontend logic works (i.e. the visibility) but when I enter a number for 'txt_HFMN' ( the option for this text box to appear in the dropdown is 'CHF' and 'CMC' for textbox 'txt_HFNumber')
Here is the code in .cs file:
if (txt_HFNumber != null)
{
strHFNUM = txt_HFNumber.Text;
}
else if (txt_HFMN != null)
{
strHFNUM = txt_HFMN.Text;
}
else
{
strHFNUM = string.Empty;
}
I tried to debug it, to pin point the error. The above condition checks only for the 'txt_HFNumber', it never checks the 'else if' part. even though I have entered a value in 'txt_HFMN' it checks for 'txt_HFNumber' and since 'txt_HFNumber' doesn't exist开发者_如何学Python in the front end/no value is entered in this textbox , it inserts a 'null' into the database instead of 'txt_HFMN' entered value!
Advise.
Really appreciate your help.
I think you might want something a bit more like this, you're checking to see if the textbox itself exists:
if (!string.IsNullOrEmpty(txt_HFNumber.Text) )
{ strHFNUM = txt_HFNumber.Text; }
...
if (txt_HFNumber != null) {
strHFNUM = txt_HFNumber.Text;
} else {
strHFNUM = string.Empty;
}
if (txt_HFMN != null) {
strHFNUM = txt_HFMN.Text;
} else {
strHFNUM = string.Empty;
}
If you are setting txt_HFNumber and txt_HFNumber you will never get to the txt_HFNumber in your statement, seperate the 2, but the point is your setting the same string twice, strHFNUM is being over ridden so if you have both set on this occasion only txt_HFMN will be set to strHFNUM
or you could do
if (txt_HFNumber != null && xt_HFMN == null) {
strHFNUM = txt_HFNumber.Text;
} else {
strHFNUM = txt_HFMN.Text; //IF xt_HFMN is NULL it will set to null thus setting the empty string is irrelevent
}
精彩评论