错误是语法错误“男性” ?如何更新Gridview中的下拉列表 [英] Error is Incorrect Syntax "Male" ? How to update the drop down in Gridview

查看:38
本文介绍了错误是语法错误“男性” ?如何更新Gridview中的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此行更新事件



'/'应用程序中的服务器错误。



附近语法不正确'女性'。



描述:执行当前Web请求期间发生了未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。



This Row Updating Event

Server Error in '/' Application.

Incorrect syntax near 'Female'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near 'Female'.

Source Error: 


Line 97:             SqlCommand cmd = new SqlCommand("update Tbl_EmpJuly31 set Name ='" + txtName.Text + "',Gender '" + DDLGender.SelectedValue + "',Country='" + DDlCountry.SelectedValue + "' where EmpId=" + EmpID , con);
Line 98:             
Line 99:             int result = cmd.ExecuteNonQuery();
Line 100:            
Line 101:            if (result == 1)

Source File: c:\Users\Admin\Desktop\LogicalTasks\InsertUpdateDelete.aspx.cs    Line: 99





代码是:





Code is :

protected void GridEmpDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int EmpID = Convert.ToInt32(GridEmpDetails.DataKeys[e.RowIndex].Value.ToString());
            string UserName = GridEmpDetails.DataKeys[e.RowIndex].Values["EmailId"].ToString();
            TextBox txtName = (TextBox)GridEmpDetails.Rows[e.RowIndex].FindControl("txtFullName");
            DropDownList DDLGender = (DropDownList)GridEmpDetails.Rows[e.RowIndex].FindControl("ddlGender");
            DropDownList DDlCountry = (DropDownList)GridEmpDetails.Rows[e.RowIndex].FindControl("DDlCountry");
            con.Open();
            SqlCommand cmd = new SqlCommand("update Tbl_EmpJuly31 set Name ='" + txtName.Text + "',Gender '" + DDLGender.SelectedValue + "',Country='" + DDlCountry.SelectedValue + "' where EmpId=" + EmpID , con);
            cmd.ExecuteNonQuery();
            con.Close();
            lblMessage.Text = UserName + "Details Updated Sucessfully";
            GridEmpDetails.EditIndex = -1;
            BindGridView();

        }







这...代码



源代码:






This ...Code

Source Code :

<asp:TemplateField HeaderText="Gender">
    <EditItemTemplate>
        <asp:DropDownList ID="ddlGender" AutoPostBack="true" SelectedValue='<%#Eval("Gender") %>'  runat="server">
            <asp:ListItem Value="Select Gender" Text="Select Gender" />
            <asp:ListItem Value="Male" Text="Male" />
            <asp:ListItem Value="Female" Text="Female" />
        </asp:DropDownList>
    </EditItemTemplate>
    <ItemTemplate>
        <asp:Label ID="lblGender" Text='<%#Eval("Gender") %>' runat="server" />
    </ItemTemplate>
    <FooterTemplate>
           <asp:DropDownList ID="ddlvalGender" AutoPostBack="true" runat="server">
           <asp:ListItem Value="Select Gender" Text="Select Gender" />
            <asp:ListItem Value="Male" Text="Male" />
            <asp:ListItem Value="Female" Text="Female" />
        </asp:DropDownList>
        <asp:RequiredFieldValidator ID="ReqvalGender" ErrorMessage="*" ForeColor="Red" ControlToValidate="ddlvalGender" ValidationGroup="V1" runat="server" />
    </FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText ="Country">
    <EditItemTemplate>
        <asp:DropDownList ID="DDlCountry" AutoPostBack="true" SelectedValue='<%#Eval("Country") %>'  runat="server">
            <asp:ListItem  Value="Select Country" Text="Select Country" />
            <asp:ListItem Value="INDIA" Text="INDIA" />
            <asp:ListItem Value="USA" Text="USA" />
            <asp:ListItem Value="UK" Text="UK" />
            <asp:ListItem Value="South Africa" Text="South Africa" />
            <asp:ListItem Value="EngLand" Text="England" />
            <asp:ListItem Value="Dubai" Text="Dubai" />
        </asp:DropDownList>







我的问题是gridview中的两滴doqns我需要更新Mathod



代码块添加 - OriginalGriff [/ edit]




My probelm is Two drop doqns in gridview I need Update Mathod

[edit]Code block added - OriginalGriff[/edit]

推荐答案

首先,它总是更好地使用参数化查询以防止对您的应用程序的恶意攻击。



此外,您缺少' = ' 性别后签名。



问候..
First of all, its always better to use parameterized query in order to prevent malicious attacks on your application.

Further, you are missing '=' sign after Gender.

Regards..


基本上,在设置性别时错过了=:

Basically, you missed out the "=" when setting the Gender:
...,Gender '" + DDLGender.SelectedValue + "',...



应该是


Should be

...,Gender ='" + DDLGender.SelectedValue + "',...





但是不要这样做!

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





But don't do it like that!
Do not 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.

SqlCommand cmd = new SqlCommand("update Tbl_EmpJuly31 set Name =@NM, Gender =@GN, Country=@CT where EmpId=@ID", con);
cmd.Parameters.AddWithValue("@NM", txtName.Text);
cmd.Parameters.AddWithValue("@GN", DDLGender.SelectedValue);
cmd.Parameters.AddWithValue("@CT", DDlCountry.SelectedValue);
cmd.Parameters.AddWithValue("@ID", EmpID);
cmd.ExecuteNonQuery();


这篇关于错误是语法错误“男性” ?如何更新Gridview中的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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