asp.net编码问题 [英] asp.net coding problem

查看:114
本文介绍了asp.net编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

if (FileUpload1.HasFile)
{
   string fn1 = Guid.NewGuid() + FileUpload1.FileName.Substring(FileUpload1.FileName.LastIndexOf("."));
   string sp = Server.MapPath("NewFolder1");
   sp += "/" + fn1;
   FileUpload1.PostedFile.SaveAs(sp);
   objprp.Pimage = (fn1).ToString();;
   objprp.priceland = Convert.ToInt32(TextBox1.Text);
   objprp.sqrft = Convert.ToInt32(TextBox2.Text);
   objprp.propertytype = TextBox4.Text;
   objprp.location = TextBox5.Text;
   obj.addland(objprp);
   Literal1.Text = "Land Information Saved";
}
else
{
    Literal1.Text = "Task Not completed Sucessfully";
}



以下是错误



Following is the error

Input string was not in a correct format.

at objprp.priceland = Convert.ToInt32(TextBox1.Text);
   objprp.sqrft = Convert.ToInt32(TextBox2.Text);




以下是类编码




Following is the class coding

public void addland(clslandprp r)
{
   SqlCommand cmd = new SqlCommand("insland", con);
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add("@priceland", SqlDbType.Int).Value =Convert.ToInt32(r.priceland);
   cmd.Parameters.Add("@sqrft", SqlDbType.Int).Value = Convert.ToInt32(r.sqrft);
   cmd.Parameters.Add("@Pimage", SqlDbType.VarChar, 100).Value = r.Pimage;
   cmd.Parameters.Add("@propertype", SqlDbType.VarChar, 50).Value = r.propertytype;
   cmd.Parameters.Add("@location", SqlDbType.VarChar, 50).Value = r.location;
   if (con.State == ConnectionState.Closed)
   {
       con.Open();
   }
   cmd.ExecuteNonQuery();
   cmd.Dispose();
   con.Close();
}




请帮帮我.
我被困在这里.




Please help me out.
I am stuck over here.

推荐答案

错误确切地告诉您问题出在哪里:在文本框中输入的内容不是有效数字.您应该先验证输入,然后再确保输入符合您的期望-输入可能包含非数字字符.
The error is telling you exactly what the problem is: whatever is entered into the textbox is not a valid number. You should validate the input before proceeding to ensure that the input matches your expectations - perhaps the input contained non-numeric characters.


在大多数情况下,请使用int.TryParse解析整数.

使用int.TryParse [ ^ ]
Use int.TryParse to parse integers in most situations.

Using int.TryParse[^]


在您的情况下,请使用CompareValidator并验证输入.

您可以在网上找到许多示例,这是有关此主题的MSDN链接: http://msdn.microsoft.com/zh-CN/library/system.web.ui.webcontrols.comparevalidator.aspx
In your case use CompareValidator and validate input.

You can find many examples on net and here is the MSDN link about this topic: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.comparevalidator.aspx


这篇关于asp.net编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆