附加信息:无法将类型为'system.web.ui.literalcontrol'的对象强制转换为'system.web.ui.webcontrols.textbox'。 [英] Additional information: unable to cast object of type 'system.web.ui.literalcontrol' to type 'system.web.ui.webcontrols.textbox'.

查看:87
本文介绍了附加信息:无法将类型为'system.web.ui.literalcontrol'的对象强制转换为'system.web.ui.webcontrols.textbox'。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
       {
           Label lblEditID = (Label)gvDepartments.Rows[e.RowIndex].FindControl("lblRowId");
           GridViewRow row = gvDepartments.Rows[e.RowIndex];
           // int row = Convert.ToInt32(gvDepartments.DataKeys[e.RowIndex].Value);
           //throw new Exception(id);
           TextBox txtCDOR = (TextBox)row.Cells[4].Controls[0];
           TextBox txtCOrganization = (TextBox)row.Cells[5].Controls[0];
           TextBox txtCBranch = (TextBox)row.Cells[7].Controls[0];
           TextBox txtCLocation = (TextBox)row.Cells[8].Controls[0];
           SqlCommand cmd = new SqlCommand("update tbl_hr_client_Reg_Data set CDOR='" + txtCDOR.Text + "',COrganization='" + txtCOrganization.Text + "',CBranch='" + txtCBranch.Text + "',CLocation='" + txtCLocation.Text + "' where id=" + lblEditID.Text, con);
           cmd.CommandTimeout = 0;
           cmd.ExecuteNonQuery();
           con.Close();
           bindgrid();
       }





我尝试过:



我试图在网格视图中更新它的工作,请任何人都可以帮助我。



What I have tried:

Im Tried to updating in grid-view its working please anyone can help me.

推荐答案

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



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

Don't do it like 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. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'

就SQL而言,用户添加的引号会终止字符串,并且您会遇到问题。但情况可能更糟。如果我来并改为输入:x'; DROP TABLE MyTable; - 然后SQL收到一个非常不同的命令:

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;--'

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

Which SQL sees as three separate commands:

SELECT * FROM MyTable WHERE StreetAddress = 'x';

完全有效的SELECT

A perfectly valid SELECT

DROP TABLE MyTable;

完全有效的删除表格通讯和

A perfectly valid "delete the table" command

--'

其他一切都是评论。

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



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



一旦你修复了它,你可以开始看这个 - 但是你的GridView可能包含字符串,不是TextBoxes。

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?

Once you have fixed that, you can start looking at this - but the chances are that your GridView contains strings, not TextBoxes.


请不要同时发布重复的帖子。我们已经在您的其他主题中解决了您的问题的可能原因:未经授权的访问权限dotnet开发人员 [ ^ ]



记下关于在GridView中访问控件时突出显示的要点。
Please don't post duplicate posts at the same time. We've already addressed the possible cause of your issue in your other thread here: Unauthorised access deanied dotnet developer[^]

Keep a note regarding the points i've highlighted regarding accessing controls within GridView.


这篇关于附加信息:无法将类型为'system.web.ui.literalcontrol'的对象强制转换为'system.web.ui.webcontrols.textbox'。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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