Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi!

I have a grid-view which displays the details of an employee. It also allows editing and deletion of the employee and his details. If Edit is selected, there are a few text boxes, on the form page, which get updated with the selected employee details. If delete is selected, ALL the employee details get deleted. However, there seems to be some issue with my program - when I click on Edit(Select Command renamed to edit) OR Delete, there is an error which states -

Invalid column name 'mjl'.

Where mjl is the empLoginId (Data Kay) of the grid.

The code is as follows :

On the .aspx page -
XML
<asp:GridView ID="GridView1" Width="100%" Height="180px" runat="server"
                    AutoGenerateColumns="False" AllowPaging="True" DataKeyNames="empLoginId"
                     OnRowDeleting="adminDelete" PageSize="5"
                    onselectedindexchanged="GridView1_SelectedIndexChanged"
                    OnPageIndexChanging="GridView1_PageIndexChanging" >
                    <Columns>
                        <asp:BoundField DataField="empFname" HeaderText="Employee First Name"
                            SortExpression="empFname" />
                        <asp:BoundField DataField="empLname" HeaderText="Employee Last Name"
                            SortExpression="empLname" />
                        <asp:BoundField DataField="pwd" HeaderText="Password" SortExpression="pwd" />
                        <asp:BoundField DataField="empRole" HeaderText="Employee Role"
                            SortExpression="empRole" />
                        <asp:CommandField ShowSelectButton="True" SelectText="Edit" />
                        <asp:CommandField ShowDeleteButton="True" />
                    </Columns>
                </asp:GridView>



On the .aspx.cs page -
C#
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    int rowIndex = GridView1.SelectedIndex;

    string strValue = GridView1.DataKeys[rowIndex].Value.ToString();
    using (SqlConnection con1 = new SqlConnection("server=.\\SQLEXPRESS;database=dB;Integrated Security=SSPI;"))
    {
        con1.Open();
        using (SqlCommand command = con1.CreateCommand())
        {
            command.CommandText = "SELECT empFname,empLname,pwd,empTitle,city,empEmail,empType,empRole,dept FROM [emp] WHERE ( [empLoginId] = " + strValue + " ) ";
            SqlDataReader reader = command.ExecuteReader();

            while (reader.Read())
            {
                TextBox5.Text = reader["empFname"].ToString().ToUpper();
                TextBox20.Text = reader["empLname"].ToString().ToUpper();
                TextBox11.Text = strValue.ToUpper();
                TextBox13.Text = reader["pwd"].ToString();
                TextBox14.Text = reader["pwd"].ToString();
                TextBox15.Text = reader["empTitle"].ToString().ToUpper();
                TextBox16.Text = reader["dept"].ToString().ToUpper();
                TextBox17.Text = reader["empEmail"].ToString();

            }
        }
        con1.Close();
    }
}


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    GridView1_SelectedIndexChanged(sender, e);
}


protected void adminDelete(object sender, GridViewDeleteEventArgs e)
{
    int rowIndex = GridView1.SelectedIndex;

    string strValue = GridView1.DataKeys[e.RowIndex].Values["empLoginId"].ToString();
    SqlConnection con1 = new SqlConnection("server=.\\SQLEXPRESS;database=dB;Integrated Security=SSPI;");
    con1.Open();
    SqlCommand cmd = new SqlCommand("delete from emp where empLoginId=" + strValue, con1);
    int result = cmd.ExecuteNonQuery();
    con1.Close();
    if (result == 1)
    {
        Label4.Visible = true;
        Label4.ForeColor = Color.Red;
        Label4.Text = strValue + "'s details deleted successfully";
    }
    else
    {
        Label4.Visible = false;
    }
}


I can't seem to figure out where I am going wrong! Please help me out!
Posted
Updated 31-May-13 21:03pm
v4

instead of this:-
C#
string strValue = GridView1.DataKeys[e.RowIndex].Values["empLoginId"].ToString();

try this
C#
string strValue =GridView1.DataKeys[index].Value.ToString()
 
Share this answer
 
v2
Comments
vsdaking 1-Jun-13 2:21am    
nada. Doesn't seem to work. Still the same thing. In fact i tried even changing the data key name value ... there's some silly error .. which we seem to be missing! :/
any other solutions ?
instead of this:
C#
string strValue = GridView1.DataKeys[e.RowIndex].Values["empLoginId"].ToString();

try this
C#
string strValue = GridView1.DataKeys[e.RowIndex].Value.ToString();
 
Share this answer
 
v2
Comments
vsdaking 1-Jun-13 2:21am    
no, this doesn't seem to work either. I still get the same error. In fact i tried even changing the data key name value ... there's some silly error .. which we seem to be missing! :/
any other solutions ?
See this link, may help ful to u.

Row updating event in gridview ASP.NET[^]

thank u.
 
Share this answer
 
Comments
vsdaking 2-Jun-13 7:25am    
i could not get my code to work ... so have taken the help of the code from your link. But i shall try my best after this submission to get to the root of this silly error and update all.
Thanks for the link in any case :)

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