将varchar值'divya'转换为数据类型int时,如何解决转换失败。 [英] How to solve conversion failed when converting the varchar value 'divya ' to data type int.

查看:63
本文介绍了将varchar值'divya'转换为数据类型int时,如何解决转换失败。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  protected   void  GridView1_RowUpdating(对象发​​件人,GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys [e.RowIndex] ]。值);

GridViewRow row =(GridViewRow)GridView1.Rows [e.RowIndex];
标签lblID =(标签)row.FindControl( lblID);

TextBox textName =(TextBox)row.Cells [ 0 ]。控件[ 0 ];
TextBox textadd =(TextBox)row.Cells [ 1 ]。控件[ 0 ];
// TextBox textc =(TextBox)row.Cells [2] .Controls [0];



GridView1.EditIndex = -1;
GridView1.DataBind();
conn.Open();

SqlCommand cmd = new SqlCommand( update empdet set name =' + textName.Text + ',salary =' + textadd.Text + 'where id =' + id + ,conn);


cmd.ExecuteNonQuery();

conn.Close();
gvbind();
}





我尝试过:



i试图编辑名称。但它显示错误消息

将varchar值'divya'转换为数据类型int时转换失败。

该怎么办?

解决方案

停止这样做!永远不要连接字符串来构建SQL命令。它会让您对意外或故意的SQL注入攻击敞开大门,这可能会破坏整个数据库。请使用参数化查询。



连接字符串时会导致问题,因为SQL会收到如下命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  Baker' s Wood '   

就SQL而言,用户添加的引用会终止字符串,但是你会遇到问题。但是可能会更糟。如果我出现并键入thi而是:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x';  DROP   MyTable ;   -   ' 

哪个SQL看作三个单独的命令:

  SELECT  *  FROM  MyTable  WHERE  StreetAddress = '  x'; 

完全有效的SELECT

  DROP   MyTable; 

完全有效的删除表格命令

   -    

其他一切都是评论。

所以它确实:选择任何匹配的行,从数据库中删除表,并忽略其他任何内容。



所以总是使用参数化查询!或者准备好经常从备份中恢复数据库。你经常定期备份,不是吗?



当你通过整个应用程序解决了这个问题后,你会发现你的问题可能已经消失了。


protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
      {
          int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

          GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
          Label lblID = (Label)row.FindControl("lblID");

          TextBox textName = (TextBox)row.Cells[0].Controls[0];
          TextBox textadd = (TextBox)row.Cells[1].Controls[0];
         // TextBox textc = (TextBox)row.Cells[2].Controls[0];



          GridView1.EditIndex = -1;
          GridView1.DataBind();
          conn.Open();

          SqlCommand cmd = new SqlCommand("update empdet set name='" + textName.Text + "',salary='" + textadd.Text + "'where id='" + id + "'", conn);


          cmd.ExecuteNonQuery();

          conn.Close();
          gvbind();
      }



What I have tried:

i tried to edit the name . but it display an error messge
"Conversion failed when converting the varchar value 'divya' to data type int.
what to do?

解决方案

Stop doing that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:

SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:

SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

A perfectly valid SELECT

DROP TABLE MyTable;

A perfectly valid "delete the table" command

--'

And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And when you have fixed that through your whole app, you will find that your problem has probably disappeared.


这篇关于将varchar值'divya'转换为数据类型int时,如何解决转换失败。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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