无法通过C#

时间:2018-08-21 16:07:44

标签: c# asp.net asp.net-mvc ssms

我正在创建一个系统,在该系统中,我想通过管理员使用数据网格视图从数据库中编辑多个记录:

此表中使用的表模式是

CREATE TABLE [dbo].[tbl_Employee] (  
[ID]   INT          NOT NULL,  
[Name] VARCHAR (50) NULL,  
[City] VARCHAR (50) NULL,  
PRIMARY KEY CLUSTERED ([ID] ASC)  
);  

我已经在GridView中添加了Add OnRowEditing,OnRowUpdating和OnRowCancelingEdit事件。

<form id="form1" runat="server">  
<div>      
    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="6" OnRowCancelingEdit="GridView1_RowCancelingEdit"   

OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">  
        <Columns>  
            <asp:TemplateField>  
                <ItemTemplate>  
                    <asp:Button ID="btn_Edit" runat="server" Text="Edit" CommandName="Edit" />  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:Button ID="btn_Update" runat="server" Text="Update" CommandName="Update"/>  
                    <asp:Button ID="btn_Cancel" runat="server" Text="Cancel" CommandName="Cancel"/>  
                </EditItemTemplate>  
            </asp:TemplateField>  
            <asp:TemplateField HeaderText="ID">  
                <ItemTemplate>  
                    <asp:Label ID="lbl_ID" runat="server" Text='<%#Eval("ID") %>'></asp:Label>  
                </ItemTemplate>  
            </asp:TemplateField>  
            <asp:TemplateField HeaderText="Name">  
                <ItemTemplate>  
                    <asp:Label ID="lbl_Name" runat="server" Text='<%#Eval("Name") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="txt_Name" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField>  
            <asp:TemplateField HeaderText="City">  
                <ItemTemplate>  
                    <asp:Label ID="lbl_City" runat="server" Text='<%#Eval("City") %>'></asp:Label>  
                </ItemTemplate>  
                <EditItemTemplate>  
                    <asp:TextBox ID="txt_City" runat="server" Text='<%#Eval("City") %>'></asp:TextBox>  
                </EditItemTemplate>  
            </asp:TemplateField>  
        </Columns>  
        <HeaderStyle BackColor="#663300" ForeColor="#ffffff"/>  
        <RowStyle BackColor="#e7ceb6"/>  
    </asp:GridView>  

</div>  

还有C#代码

using System;  
using System.Data;  
using System.Data.SqlClient;  
using System.Configuration;  
using System.Web.UI.WebControls;  

public partial class _Default : System.Web.UI.Page  
{  
//Connection String from web.config File  
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;  
SqlConnection con;  
SqlDataAdapter adapt;  
DataTable dt;  
protected void Page_Load(object sender, EventArgs e)  
{  
    if(!IsPostBack)  
    {  
        ShowData();  
    }  
}  
//ShowData method for Displaying Data in Gridview  
protected void ShowData()  
{  
    dt = new DataTable();  
    con = new SqlConnection(cs);  
    con.Open();  
    adapt = new SqlDataAdapter("Select ID,Name,City from tbl_Employee",con);  
    adapt.Fill(dt);  
    if(dt.Rows.Count>0)  
    {  
        GridView1.DataSource = dt;  
        GridView1.DataBind();  
    }  
    con.Close();  
}  

protected void GridView1_RowEditing(object sender, System.Web.UI.WebControls.GridViewEditEventArgs e)  
{  
    //NewEditIndex property used to determine the index of the row being edited.  
    GridView1.EditIndex = e.NewEditIndex;  
    ShowData();  
}  
protected void GridView1_RowUpdating(object sender, System.Web.UI.WebControls.GridViewUpdateEventArgs e)  
{  
    //Finding the controls from Gridview for the row which is going to update  
    Label id=GridView1.Rows[e.RowIndex].FindControl("lbl_ID") as Label;  
    TextBox name = GridView1.Rows[e.RowIndex].FindControl("txt_Name") as TextBox;  
    TextBox city = GridView1.Rows[e.RowIndex].FindControl("txt_City") as TextBox;  
    con = new SqlConnection(cs);  
    con.Open();  
    //updating the record  
    SqlCommand cmd = new SqlCommand("Update tbl_Employee set Name='"+name.Text+"',City='"+city.Text+"' where ID="+Convert.ToInt32(id.Text),con);  
    cmd.ExecuteNonQuery();  
    con.Close();  
    //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
    GridView1.EditIndex = -1;  
    //Call ShowData method for displaying updated data  
    ShowData();  
}  
protected void GridView1_RowCancelingEdit(object sender, System.Web.UI.WebControls.GridViewCancelEditEventArgs e)  
 {  
    //Setting the EditIndex property to -1 to cancel the Edit mode in Gridview  
    GridView1.EditIndex = -1;  
    ShowData();  
 }  
}

代码未显示任何错误,但数据库未更新。

我要去哪里错了?

0 个答案:

没有答案