protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string Createid =((Label)GridView1.Rows[e.RowIndex].FindControl("lblID")).Text;
string Client_Name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtClientName")).Text;
string Client_Contact_Name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtContactPerson")).Text;
string Client_Contact_Role = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPosition")).Text;
string Client_Contact_Email_ID = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtMailID")).Text;
string Phone_Number = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtPhNumber")).Text;
string No_of_Openings = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtOpenings")).Text;
string Technology = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtTech")).Text;
string Status = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtStatus")).Text;
string Op_Start_Date = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtStrtDate")).Text;
string Op_End_Date = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEndDate")).Text;
con.Open();
SqlCommand cmd = new SqlCommand("UPDATE Client_Det set Client_Name=@Client_Name, " + " Client_Contact_Name= @Client_Contact_Name, " + " Client_Contact_Role= @Client_Contact_Role, " + " Client_Contact_Email_ID = @Client_Contact_Email_ID , " + " Phone_Number= @Phone_Number , " + "No_of_Openings=@No_of_Openings , " + "Technology=@Technology, " + "Status=@Status, " + " Op_Start_Date=@Op_Start_Date , " + " Op_End_Date=@Op_End_Date where Createid= @Createid", con);
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
gvbind();
}
我的表数据类型
以上是表数据类型。
点击更新按钮后,它甚至没有显示任何错误
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" align="center"
DataKeyNames="Createid" OnPageIndexChanging="GridView1_PageIndexChanging"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
EnableViewState="False" AllowPaging="True" PageSize="5">
<Columns>
<asp:TemplateField ItemStyle-Width = "30px" HeaderText = "ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server"
Text='<%# Eval("Createid")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Client">
<ItemTemplate>
<asp:Label ID="lblClientName" runat="server"
Text='<%# Eval("Client_Name")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtClientName" runat="server"
Text='<%# Eval("Client_Name")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtClientName" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Contact Person">
<ItemTemplate>
<asp:Label ID="lblContactPerson" runat="server"
Text='<%# Eval("Client_Contact_Name")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtContactPerson" runat="server"
Text='<%# Eval("Client_Contact_Name")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtContactPerson" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Position">
<ItemTemplate>
<asp:Label ID="lblPosition" runat="server"
Text='<%# Eval("Client_Contact_Role")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPosition" runat="server"
Text='<%# Eval("Client_Contact_Role")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPosition" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Mail ID">
<ItemTemplate>
<asp:Label ID="lblMailID" runat="server"
Text='<%# Eval("Client_Contact_Email_ID")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtMailID" runat="server"
Text='<%# Eval("Client_Contact_Email_ID")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtMailID" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Phone Number">
<ItemTemplate>
<asp:Label ID="lblPhNumber" runat="server"
Text='<%# Eval("Phone_Number")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtPhNumber" runat="server"
Text='<%# Eval("Phone_Number")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtPhNumber" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "No of Openings">
<ItemTemplate>
<asp:Label ID="lblOpenings" runat="server"
Text='<%# Eval("No_of_Openings")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtOpenings" runat="server"
Text='<%# Eval("No_of_Openings")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtOpenings" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Technology">
<ItemTemplate>
<asp:Label ID="lblTech" runat="server"
Text='<%# Eval("Technology")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtTech" runat="server"
Text='<%# Eval("Technology")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtTech" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Status">
<ItemTemplate>
<asp:Label ID="lblStatus" runat="server"
Text='<%# Eval("Status")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtStatus" runat="server"
Text='<%# Eval("Status")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtStatus" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "Start Date">
<ItemTemplate>
<asp:Label ID="lblStrtDate" runat="server"
Text='<%# Eval("Op_Start_Date","{0:dd-MM-yyyy}")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtStrtDate" runat="server"
Text='<%# Eval("Op_Start_Date","{0:dd-MM-yyyy}")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtStrtDate" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField ItemStyle-Width = "100px" HeaderText = "End Date">
<ItemTemplate>
<asp:Label ID="lblEndDate" runat="server"
Text='<%# Eval("Op_End_Date","{0:dd-MM-yyyy}")%>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtEndDate" runat="server"
Text='<%# Eval("Op_End_Date","{0:dd-MM-yyyy}")%>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox ID="txtEndDate" runat="server"></asp:TextBox>
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="true" />
</Columns>
</asp:GridView>
这是gridview的源代码...... 我在提供正确的字段时做错了什么..请告诉我
答案 0 :(得分:0)
你做错了。您的更新查询不执行任何操作。您应该将其指定为参数,或者您应该直接给出变量名称。
选项1
SqlCommand cmd = new SqlCommand("UPDATE Client_Det set Client_Name='"+ Client_Name +"', Client_Contact_Name='"+ Client_Contact_Name +"', Client_Contact_Role='"+Client_Contact_Role+"', Client_Contact_Email_ID ='"+ Client_Contact_Email_ID+"', Phone_Number='"+Phone_Number+"', No_of_Openings='"+No_of_Openings+"', Technology='"+Technology+"', Status='"+Status+"', Op_Start_Date='"+ Op_Start_Date +"', Op_End_Date='"+Op_End_Date+"' where Createid='"+Createid+"'", con);
存在未正确引用的变量。所以这些值没有附加到查询中。
PS - 这是从安全角度编写查询的非常糟糕的方法。您的代码为SQL注入攻击保留了空间。选项2是编写查询的更好方式
选项2
Through Command parameters
因此,这基本上是您为参数赋值,并且给予命令参数的参数名称应该等于查询中给出的参数名称。以下是您可以实现的一瞥。
string sql = "UPDATE Client_Det set Client_Name=@Client_Name, Client_Contact_Name= @Client_Contact_Name, Client_Contact_Role= @Client_Contact_Role, Client_Contact_Email_ID = @Client_Contact_Email_ID , Phone_Number= @Phone_Number ,No_of_Openings=@No_of_Openings, Technology=@Technology,Status=@Status,Op_Start_Date=@Op_Start_Date, Op_End_Date=@Op_End_Date where Createid= @Createid";
SqlCommand cmd = new SqlCommand(sql, con);
cmd.Parameters.AddWithValue("@Client_Name", Client_Name);
cmd.Parameters.AddWithValue("@Client_Contact_Name",Client_Contact_Name)
/*Add for remaining param values*/
cmd.ExecuteNonQuery();
con.Close();
GridView1.EditIndex = -1;
gvbind();
每个AddValueWith
上的第一个参数,即@Client_Name
和@Client_Contact_Name
应与update query
和第二个参数Client_Name
和Client_Contact_Name
中的相同值来自GridView Controls
,例如TextBox
,Label
等,