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