Reputation: 1
I have two problem in my data entry page: probml 1)Though I am doing client side validation, why pointer is going to code behind page.. I have a text field called amount if that field is empty I am one alert message and focusing on that field. But immidieately control going to code behind page: in this line: decimal amount = Convert.ToDecimal(txtAmount.Text);Here I am getting the exception, “Input paramater was not in correct format” Here is javascript code:
<script type="text/javascript">
function validate()
{
var value = document.getElementById("txtAmount").value;
if(value=="")
{
alert("Please enterAmount.");
document.getElementById("txtAmount").focus();
return false;
}
else
{
return true;
}
}
</script>
<asp:Button ID="btnSave" runat="server" Text="Save" Width="64px" Font-Bold="true" OnClientClick="validate()" onclick="btnSave_Click"/>
Prblm2: While I am entering duplicate values I am expecting the message like “This id already exists, please try with another id”, but I am getitng the excetpion like primary key conflict……… Here is code for that..
if (!Page.IsValid)
return;
int sum = 0;
ContactPersonBO contactpersonbo = new ContactPersonBO();
string personid = txtPersonid.Text;
decimal amount = Convert.ToDecimal(txtAmount.Text);
try
{
contactpersonbo.PersonID = personid;
contactpersonbo.Amount = amount;
sum = ontactpersonbo.InsertPerson();
if (sum > 0)
{
lblMessage.Text = "person has been succesfully added.”;
}
else
{
lblMessage.Text = "This person already exists, please try with otherid”;
}
}
catch (Exception ex)
{
lblMessage.Text = ex.Message.ToString();
}
Note: Here control is never coming to this part:
else
{
lblMessage.Text = "This person already exists, please try with otherid”;
}
Upvotes: 0
Views: 109
Reputation: 263187
Concerning your first problem, you should return false
from your onclick
handler in order to prevent default processing to take place and the page to be submitted:
<asp:Button ID="btnSave" runat="server" Text="Save" Width="64px"
Font-Bold="true" OnClientClick="return validate()" onclick="btnSave_Click" />
Concerning your second problem, it looks like your InsertPerson()
method is throwing an exception instead of returning 0
if the person already exists. From the code you posted below, it seems the method doesn't perform any check on the existence of the new person.You might want to add that test using e.g. a select
query.
Upvotes: 1
Reputation: 26747
Probabily the txtAmound doesn't cointain a parsable sring (it could be empty or the decimal separator doesn't stick to the setting you are using) you can use Decimal.TryParse instead.bear in mind it just doesn't raise the exception if the string is not in a correct format
http://msdn.microsoft.com/en-us/library/system.decimal.tryparse.aspx
for the second case I don't know you DAL(data access layer) but I can guess one the table/object you are trying to insert the person there is a primary key constrain. Probably you are using an Id that has been already saved
Upvotes: 0