错误:输入字符串格式不正确 [英] Error: Input String is not a correct format

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

问题描述



在这里,我尝试编写用于从gridview删除行的代码,但是当我单击Delete按钮时,出现错误:输入字符串的格式不正确.

这是我的删除按钮代码.

Hi,

Here I have tried to write a code for deleting a row from gridview but when I clicked on delete button, I get an error: input string is not in correct format.

Here is my delete button code.

protected void gridItem_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["connectionstring"].ToString());
                                   
            SqlCommand sqlDelete = new SqlCommand("delete from product_detail where Product_Id=@Product_Id", con);
            sqlDelete.Parameters.Add("@Product_Id", SqlDbType.Int).Value = Convert.ToInt32(gridItem.Rows[e.RowIndex].Cells[1].Text.Trim());
            sqlDelete.Connection = con;
            con.Open();
            sqlDelete.ExecuteNonQuery();
            con.Dispose();
            con.Close();
            fillgrid();
            gridItem.DataBind();
        }


请告诉我这段代码在哪里?


Please tell me where am I wrong in this code?

推荐答案

我认为可能error 在这里,
I think the probable error is here,
sqlDelete.Parameters.Add("@Product_Id", SqlDbType.Int).Value = Convert.ToInt32(gridItem.Rows[e.RowIndex].Cells[1].Text.Trim());


您是否检查过Cell值可转换为Integer ?

或请指定出现错误的行,否则我们将无法承担.

如果有帮助,请 投票 接受答案 .


Have you checked that the Cell value is convertible to Integer ?

or Please specify the line where you are getting error otherwise we can''t assume.

Please vote and Accept Answer if it Helped.


添加'如果您的产品ID是字母数字类型,则在SqlCommand中使用''(从product_detail中删除,其中Product_Id = @ Product_Id",同上).



它是db中的一个整数,但是在从网格中获取它表示的内容时:在分配它之前,请尝试以下

Add '''' in your SqlCommand ("delete from product_detail where Product_Id=@Product_Id", con) if your Product ID is of alphanumeric type.



It is an Integer in db but while getting from grid what it represents: before assigning it try this

int prod_id = Convert.ToInt32(gridItem.Rows[e.RowIndex].Cells[1].Text.Trim().ToString()); 


sqlDelete.Parameters.Add("@Product_Id", prod_id);



还要在sqlCommand中放置一个空格



Also put a space in the sqlCommand

SqlCommand sqlDelete = new SqlCommand("delete from product_detail where Product_Id = @Product_Id", con);


设置一个断点并检查行获取错误..

我更喜欢使用GridView的 DataKeyNames 属性来获取选定的行值,而不是使用gridItem.Rows [e.RowIndex] .Cells [1].

put a breakpoint and check the line getting error..

i prefer using DataKeyNames property of GridView for getting selected row value instead of gridItem.Rows[e.RowIndex].Cells[1].


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

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