Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :

C#
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 :

XML
<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>



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

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 31-Jul-14 20:48pm
v2

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..
 
Share this answer
 
Comments
Sai Prasad anumolu 1-Aug-14 2:51am    
KK Can u send me sample Logic
Thanks7872 1-Aug-14 3:06am    
See below answer by OG.
Sai Prasad anumolu 1-Aug-14 2:54am    
thank you by mistake .....
Basically, you missed out the "=" when setting the Gender:
C#
...,Gender '" + DDLGender.SelectedValue + "',...

Should be
C#
...,Gender ='" + DDLGender.SelectedValue + "',...


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.

C#
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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900