如何解决错误“输入字符串格式不正确?" [英] how to solve the error "input string is not in correct format?

查看:1052
本文介绍了如何解决错误“输入字符串格式不正确?"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都知道这...

这是价格范围的编码........但是执行时会产生错误,例如输入字符串的格式不正确" .......



Anyone know this......

this is the coding for price ranges........but while executing it produce error like" input string is not in correct format".......



protected void BulletedList3_Click(object sender, BulletedListEventArgs e)
    {
       
       string price = BulletedList3.Items[e.Index].Value;
        if (con.State == ConnectionState.Closed)
        {
            con.Open();
        }
        if (Convert.ToInt32( price )< 500)
        {
            SqlCommand cmd = new SqlCommand("select pro_img,pro_name,price from product where price='" + price + "' and status='A' and type='retail'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            DataList1.DataSource = ds;
            DataList1.DataBind();
            con.Close();
        }
    }

推荐答案

请改用参数化查询-并将您之前转换后的价格提供给它.无论如何,您都在进行转换,所以为什么不正确使用该值呢?
Use a parametrized query instead - and feed it the converted price you did earlier. You are doing the conversion anyway, so why not use the value properly?
int i = Convert.ToInt32( price );
if (i < 500)
{
SqlCommand cmd = new SqlCommand("select pro_img,pro_name,price from product where price=@PR and status='A' and type='retail'", con);
cmp.Parameters.AddWithValue("@PR", i);


检查一次数据库中为Price 给出的数据类型
并在代码中.如果它们不相同,则需要转换代码侧变量的数据类型
Check once what data type you have given for Price in database
and in code. If they are not same you need to convert the data type of code side variable


伙计……看看您输入的方式是否正确,那么您将不会得到此错误.例如,如果您要买东西,您将输入从2000到4000的奖金范围...就像您必须手动输入正确的输入字符串一样
hey buddy... see if you give your input in correct way, then u wont get this error... for example if you going to buy something, u will enter prize range starts from 2000 to 4000 ... as like you have to give correct input strings manually


这篇关于如何解决错误“输入字符串格式不正确?"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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