未经授权访问的网络开发人员 [英] Unauthorised access deanied dotnet developer
问题描述
protected void OnRowUpdating(object sender, GridViewUpdateEventArgs e)
{
Label lblEditID = (Label)gvDepartments.Rows[e.RowIndex].FindControl("lblRowId");
GridViewRow row = gvDepartments.Rows[e.RowIndex];
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();
//VKJ
}
我的尝试:
i试图它无法正常工作,但是它显示出来的错误请帮助我
What I have tried:
i tried its not working, but its showing casting error please help me
推荐答案
我是第二个关于SQL的OriginalGriff注入。
关于你的问题,既然你没有指定错误爆发的那一行,那么我猜你在哪一行FindControl
或访问Controls
集合。
从GridView
引用服务器控件
时,那么你可以使用FindControl
或控制
集合,就像你现在拥有的一样。但请记住,他们有特定的目的。当服务器控件驻留在TemplateField
列时,通常会使用FindControl
方法。当您使用BoundField
列时,通常会使用Controls [index]
。
例如,让我们设置一个GridView
,它将显示2列数据,如下所示:
I second to OriginalGriff's take regarding SQL Injection.
Down to your issue, since you haven't specified which line the error blows up, then I'm guessing at the line where you do aFindControl
or accessing theControls
collection.
When referencing aServer Control
fromGridView
, then you can either useFindControl
orControls
collection just like what you have now. But keep in mind that they have a specific purpose. You would typically use theFindControl
method when your server control is residing with aTemplateField
column. And you would typically use theControls[index]
when your are using aBoundField
column.
For example, let's set a aGridView
which will show 2 columns of data like in the following:
<asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Employee Name">
<EditItemTemplate>
<asp:TextBox ID="TextBoxEditEmployee" runat="server" Text='<%# Bind("Employees") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelEmployee" runat="server" Text='<%# Bind("Employees") %>'/>
</ItemTemplate>
</asp:TemplateField >
<asp:TemplateField HeaderText="Position">
<EditItemTemplate>
<asp:TextBox ID="TextBoxEditPosition" runat="server" Text='<%# Bind("Position") %>'/>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="LabelPosition" runat="server" Text='<%# Bind("Position") %>'/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
现在,如果你想在 RowUpdating
事件中访问上面 TextBox
的值,您可以这样做:
Now, if you want to access the values of TextBox
above at RowUpdating
event, you could do something like this:
protected void GridViewEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e){
//Accessing Edited values from the GridView
string employee = ((TextBox)GridViewEmployee.Rows[e.RowIndex].Cells[0].FindControl("TextBoxEditEmployee")).Text; //Employee
string position = ((TextBox)GridViewEmployee.Rows[e.RowIndex].Cells[1].FindControl("TextBoxEditPosition")).Text; //Position
// Your Update code here
// Do database changes
GridViewEmployee.EditIndex = -1; //Turn the Grid back to read only mode
BindGridView(); // Rebind GridView to reflect changes made
}
注意我们使用 Cells [0] .FindControl(TextBoxEditEmployee)引用
。这表明我们通过引用 Control
) Cells [0]
来查看 GridView
的第一列。 GridView列索引始终从零开始。然后我们使用 FindControl(TextBoxEditEmployee)
在列
中找到我们想要的控件。服务器控件的 ID
应该与您传入 FindControl
方法的那个匹配,否则它会抛出一个空引用异常
。
从 BoundField $ c $访问控件c>列不同。例如,如果我们有这样的GridView标记:
Noticed that we reference the Control
using Cells[0].FindControl("TextBoxEditEmployee")
. This indicates that we are looking at the first Column of the GridView
by referencing Cells[0]
. GridView column index will always starts at Zero. Then we find the control we want within that Column
using FindControl("TextBoxEditEmployee")
. The ID
of the server control should match with the one you are passing into the FindControl
method or else it will throw a Null Reference Exception
.
Accessing a control from a BoundField
Column is different. For example, if we have a GridView markup like this:
<asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Employees" HeaderText="Name"/>
<asp:BoundField DataField="Position" HeaderText="Position"/>
</Columns>
</asp:GridView>
要在RowUpdating事件中访问已编辑的值,您可以执行以下操作:
To access the edited values at RowUpdating event, you can do something like this:
protected void GridViewEmployee_RowUpdating(object sender, GridViewUpdateEventArgs e){
//Accessing Edited values from the GridView
string employee = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0]).Text;
string position = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
// Your Update code here
// Do database changes
GridViewEmployee.EditIndex = -1; //Turn the Grid back to read only mode
BindGridView(); // Rebind GridView to reflect changes made
}
注意到我们现在使用 Cells [0] .Controls [0]
来引用生成的服务器控制
。这表明我们通过引用 Cells [0]来查看
。并且我们在 GridView
的第一个列
] 列
中使用控件[0]
这是自动生成的编辑 TextBox
。
这里要记住的是你应该从 GridView
访问Control时指定Cell的正确索引,否则将抛出 Null Reference Exception
。
如果你有一个类型转换错误
,那么它可能与你的数据库数据类型不匹配有关。换句话说,您传递的是来自 TextBox
/ Label <的
字符串
类型/ code>到数据库中的整数
类型。例如,在您的SQL Where子句
中,您使用的是 id
,这可能是 int
从您的数据库中输入,但是您将 Label.Text
值传递给它,这是一个字符串
。您应该验证要传递值的每个类型并修复它,并考虑使用参数化查询,以避免 Sql Injection攻击。
Noticed that we are now using Cells[0].Controls[0]
to reference the generated Server Control
. This indicates that we are looking at the first Column
of the GridView
by referencing Cells[0]
.and hen we find the control we want within that Column
using Controls[0]
which is the auto-generated Edit TextBox
.
The thing to keep in mind here is that you should specify the correct index of Cells when accessing a Control from GridView
, otherwise it will throw a Null Reference Exception
.
If you are having a type cast error
, then perhaps it is related to your Database datatype mismatch. In other words, you are passing an string
type that comes from your TextBox
/Label
to a integer
type in your database. For example, in your SQL Where Clause
, you are using an id
which could be an int
type from your database but you were passing the Label.Text
value to it which is a string
. You should verify each types you are passing values to and fix that and consider using parameterize query as suggested to avoid Sql Injection attack.
我试过它不能正常工作,但是显示出来的错误请帮忙我
i tried its not working, but its showing casting error please help me
它还应该告诉你错误的行号。
It should also tell you the line number of error.
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);
不是你问题的解决方案,而是你遇到的另一个问题。
从不通过concatena构建SQL查询弦乐。迟早,您将使用用户输入来执行此操作,这会打开一个名为SQL注入的漏洞,这对您的数据库很容易并且容易出错。
名称中的单引号你的程序崩溃。如果用户输入像Brian O'Conner这样的名称可能会使您的应用程序崩溃,那么这是一个SQL注入漏洞,崩溃是最少的问题,恶意用户输入,并且它被提升为具有所有凭据的SQL命令。
SQL注入 - 维基百科 [ ^ ]
SQL注入 [ ^ ]
按示例进行SQL注入攻击 [ ^ ]
PHP:SQL注入 - 手册 [ ^ ]
SQL注入预防备忘单 - OWASP [ ^ ]
我该怎么办?解释没有技术术语的SQL注入? - 信息安全堆栈交换 [ ^ ]
Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
不要这样做!永远不要连接字符串来构建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.
这篇关于未经授权访问的网络开发人员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!