我无法在gridview中进行编辑更新 [英] i cant edit-update in gridview

查看:152
本文介绍了我无法在gridview中进行编辑更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我执行此代码并按编辑"按钮时出现错误,因此无法在网格视图中进行编辑更新:-

hello, i cant edit-update in grid view when i execute this code and press edit button the error occurly coime like:-

'cmbType' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value




请建议我应该为此做些什么请建议我解决此问题




pls suggest me wht should i do for this pls suggest me to solve this problem

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
         <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:customerConnectionString %>"

            SelectCommand="SELECT [id], [name], [gender], [city], [state] FROM [cust_info]">
        </asp:SqlDataSource>
        <asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing">
<Columns>
 <asp:TemplateField HeaderText="ID" Visible="False">
   <ItemTemplate>
    <asp:Label ID="lblid" runat="server" Text='<%# Eval("id")%>'></asp:Label>
         </ItemTemplate>
    </asp:TemplateField>
<asp:TemplateField HeaderText="Name" SortExpression="Name">
<EditItemTemplate>
  <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
  <asp:TextBox ID="txtNewName" runat="server"></asp:TextBox> </FooterTemplate>
<ItemTemplate>
  <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Gender">
<EditItemTemplate>
  <asp:DropDownList ID="cmbGender" runat="server"  DataTextField="Gender" DataValueField="Gender" >  </asp:DropDownList>
  <asp:TextBox ID="txtgndr" runat="server" Text='<%# Eval("gender") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
  <asp:Label ID="lbGender" runat="server" Text='<%# Eval("gender") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
  <asp:DropDownList ID="cmbNewGender" runat="server"  DataTextField="Gender" DataValueField="Gender"> </asp:DropDownList>
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="City">
<EditItemTemplate>
  <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
</EditItemTemplate>
<FooterTemplate>
  <asp:TextBox ID="txtNewCity" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
  <asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="State" SortExpression="State">
<EditItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Eval("state") %>'></asp:Label>

</EditItemTemplate>
<FooterTemplate>
  <asp:TextBox ID="txtNewState" runat="server" ></asp:TextBox>
</FooterTemplate>
<ItemTemplate>
  <asp:Label ID="Label4" runat="server" Text='<%# Bind("state") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Type">
<EditItemTemplate>
  <asp:DropDownList ID="cmbType" runat="server" DataTextField="Type" DataValueField="Type" SelectedValue='<%# Eval("type") %>'> </asp:DropDownList>

</EditItemTemplate>
<ItemTemplate>
  <asp:Label ID="Label5" runat="server" Text='<%# Eval("type") %>'></asp:Label>
</ItemTemplate>
<FooterTemplate>
  <asp:DropDownList ID="cmbNewType" runat="server" DataTextField="Type" DataValueField="Type"> </asp:DropDownList>

</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Edit" ShowHeader="False">
<EditItemTemplate>
  <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton>
  <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
<FooterTemplate>
  <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton>
</FooterTemplate>
<ItemTemplate>
  <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" />

</Columns>
</asp:GridView>
    </div>
    </form>
</body>
</html>







using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page 
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["customerConnectionString"].ToString());
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadGridView();
        }
    }
 
    private void LoadGridView()
    {
        SqlDataAdapter adap = new SqlDataAdapter("select * from cust_info", conn);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "AddNew")
        {
            string name = (GridView1.FooterRow.FindControl("txtNewName") as TextBox).Text;
            string gender = (GridView1.FooterRow.FindControl("cmbNewGender") as DropDownList).Text;
            string city = (GridView1.FooterRow.FindControl("txtNewCity") as TextBox).Text;
            string state = (GridView1.FooterRow.FindControl("txtNewState") as TextBox).Text;
            string type = (GridView1.FooterRow.FindControl("cmbNewType") as DropDownList).Text;
 
            SqlCommand comm = new SqlCommand();
            comm.CommandText = "insert into cust_info (name,gender,city,state,type) values(@name,@gender,@city,@state,@type)";
            comm.Connection = conn;
 
            comm.Parameters.AddWithValue("@name", name.ToString());
            comm.Parameters.AddWithValue("@gender", gender.ToString());
            comm.Parameters.AddWithValue("@city", city.ToString());
            comm.Parameters.AddWithValue("@state", state.ToString());
            comm.Parameters.AddWithValue("@type", type.ToString());
 
            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();
 
            LoadGridView();
 
        }
    }
 
    public DataTable FetchCustomerType()
    {
        string sql = "Select Distinct type From cust_info";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
 
    public DataTable FetchCustomerGender()
    {
        string sql = "Select Distinct gender From cust_info";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList cmbtype = (DropDownList)e.Row.FindControl("cmbType");
            DropDownList cmbgnder = (DropDownList)e.Row.FindControl("cmbGender");
            if (cmbtype != null && cmbgnder != null)
            {
 
                cmbgnder.DataSource = FetchCustomerGender();
                cmbgnder.DataBind();
                cmbgnder.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[2].ToString();
                cmbtype.DataSource = FetchCustomerType();
                cmbtype.DataBind();
                cmbtype.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
 
            }
        }
 

 
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList cmbnewtype = (DropDownList)e.Row.FindControl("cmbNewType");
            DropDownList cmbnewgnder = (DropDownList)e.Row.FindControl("cmbNewGender");
            cmbnewgnder.DataSource = FetchCustomerGender();
            cmbnewgnder.DataBind();
            cmbnewtype.DataSource = FetchCustomerType();
            cmbnewtype.DataBind();
 

        }
        
        
            
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
 
        string id = (row.FindControl("lblID") as Label).Text;
        string name = (row.FindControl("txtName") as TextBox).Text;
        string gender = (row.FindControl("cmbGender") as DropDownList).Text;
        string city = (row.FindControl("txtCity") as TextBox).Text;
        string state = (row.FindControl("txtstate") as TextBox).Text;
        string type = (row.FindControl("cmbType") as DropDownList).Text;
 
        SqlCommand comm = new SqlCommand();
        comm.CommandText = "update cust_info set name=@name,city=@city,state=@state where id=@id";
        comm.Connection = conn;
 
        comm.Parameters.AddWithValue("@name", name.ToString());
        comm.Parameters.AddWithValue("@id", id.ToString());
        comm.Parameters.AddWithValue("@gender", gender.ToString());
        comm.Parameters.AddWithValue("@city", city.ToString());
        comm.Parameters.AddWithValue("@state", state.ToString());
        comm.Parameters.AddWithValue("@type", type.ToString());
 
        conn.Open();
        comm.ExecuteNonQuery();
        conn.Close();
 
        GridView1.EditIndex = -1;
        LoadGridView();
 
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        LoadGridView();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadGridView();
    }
}



请建议我执行本示例"cmbType"时解决此示例

错误发生了...



pls suggest me to solution for this example whn i execute this example "cmbType"

error occurly come...

推荐答案

ConnectionStrings:customerConnectionString %> " span> =" 从[cust_info]中选择[id],[name],[gender],[city],[state]" < /asp:SqlDataSource > < asp:GridView ID =" runat 服务器" ShowFooter true" AutoGenerateColumns 错误" OnRowCommand =" OnRowDataBound =" =" GridView1_RowUpdating" OnRowCancelingEdit =" OnRowEditing =" GridView1_RowEditing" < > < asp:TemplateField HeaderText =" 可见 错误" < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Eval(" )%> ' > < /asp:Label > < /ItemTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" SortExpression 名称" < EditItemTemplate > < asp:TextBox ID =" runat 服务器" 文本 <%#Eval(" )%> ' > < /asp:TextBox > < /EditItemTemplate > < FooterTemplate > < asp:TextBox ID =" runat 服务器" < > < /FooterTemplate > < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Bind(" )%> ' > < /asp:Label > < /ItemTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" < EditItemTemplate > < asp:DropDownList ID =" runat 服务器" =" =" 性别" > < /asp: DropDownList > < asp:TextBox ID =" runat 服务器" 文本 <%#Eval(" )%> ' > < /asp:TextBox > < /EditItemTemplate > < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Eval(" )%> ' > < /asp:Label > < /ItemTemplate > < FooterTemplate > < asp:DropDownList ID =" runat 服务器" =" =" 性别" > < /asp:DropDownList > < /FooterTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" < EditItemTemplate > < asp:TextBox ID =" runat 服务器" 文本 <%#Bind(" )> < /asp:TextBox > < /EditItemTemplate > < FooterTemplate > < asp:TextBox ID =" runat 服务器" > /asp:TextBox > < /FooterTemplate > < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Bind(" )> < /asp:Label > < /ItemTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" SortExpression 状态" < EditItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Eval(" )> < /asp:Label > < /EditItemTemplate > < FooterTemplate > < asp:TextBox ID =" runat 服务器" > /asp:TextBox > < /FooterTemplate > < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Bind(" )> < /asp:Label > < /ItemTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" < EditItemTemplate > < asp:DropDownList ID =" runat 服务器" DataTextField 类型" DataValueField 类型" SelectedValue =' <%# Eval(" ) %> ' > < /asp:DropDownList > < /EditItemTemplate > < ItemTemplate > < asp:Label ID =" runat 服务器" 文本 <%#Eval(" )%> ' > < /asp:Label > < /ItemTemplate > < FooterTemplate > < asp:DropDownList ID =" runat 服务器" DataTextField 类型" DataValueField 类型" < > < /FooterTemplate > < /asp:TemplateField > < asp:TemplateField HeaderText =" ShowHeader 错误" < EditItemTemplate > < asp:LinkBut​​ton ID =" runat 服务器" CausesValidation True" CommandName 更新" 文本 =" >> < /asp:Lin kButton > < asp:LinkBut​​ton ID =" runat 服务器" CausesValidation 错误" CommandName 取消" 文本 =" >> < /asp:Li nkBut​​ton > < /EditItemTemplate > < FooterTemplate > < asp:LinkBut​​ton ID =" runat 服务器" CausesValidation 错误" CommandName AddNew" 文本 =" >> ; < /asp:L inkBut​​ton > < /FooterTemplate > < ItemTemplate > < asp:LinkBut​​ton ID =" runat 服务器" CausesValidation 错误" CommandName 编辑" 文本 =" >> < /asp:LinkBu tton > < /ItemTemplate > < /asp:TemplateField > < asp:CommandField HeaderText =" ShowDeleteButton True " ShowHeader True" / < /列 > < /asp:GridView > < /div > < /form > < /body > < /html >
ConnectionStrings:customerConnectionString %>" SelectCommand="SELECT [id], [name], [gender], [city], [state] FROM [cust_info]"> </asp:SqlDataSource> <asp:GridView ID="GridView1" runat="server" ShowFooter="true" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand" OnRowDataBound="GridView1_RowDataBound" OnRowUpdating="GridView1_RowUpdating" OnRowCancelingEdit="GridView1_RowCancelingEdit" OnRowEditing="GridView1_RowEditing"> <Columns> <asp:TemplateField HeaderText="ID" Visible="False"> <ItemTemplate> <asp:Label ID="lblid" runat="server" Text='<%# Eval("id")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Name" SortExpression="Name"> <EditItemTemplate> <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewName" runat="server"></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Gender"> <EditItemTemplate> <asp:DropDownList ID="cmbGender" runat="server" DataTextField="Gender" DataValueField="Gender" > </asp:DropDownList> <asp:TextBox ID="txtgndr" runat="server" Text='<%# Eval("gender") %>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lbGender" runat="server" Text='<%# Eval("gender") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="cmbNewGender" runat="server" DataTextField="Gender" DataValueField="Gender"> </asp:DropDownList> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="City"> <EditItemTemplate> <asp:TextBox ID="txtCity" runat="server" Text='<%# Bind("city") %>'></asp:TextBox> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewCity" runat="server" ></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label3" runat="server" Text='<%# Bind("city") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="State" SortExpression="State"> <EditItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("state") %>'></asp:Label> </EditItemTemplate> <FooterTemplate> <asp:TextBox ID="txtNewState" runat="server" ></asp:TextBox> </FooterTemplate> <ItemTemplate> <asp:Label ID="Label4" runat="server" Text='<%# Bind("state") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Type"> <EditItemTemplate> <asp:DropDownList ID="cmbType" runat="server" DataTextField="Type" DataValueField="Type" SelectedValue='<%# Eval("type") %>'> </asp:DropDownList> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label5" runat="server" Text='<%# Eval("type") %>'></asp:Label> </ItemTemplate> <FooterTemplate> <asp:DropDownList ID="cmbNewType" runat="server" DataTextField="Type" DataValueField="Type"> </asp:DropDownList> </FooterTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Edit" ShowHeader="False"> <EditItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update" Text="Update"></asp:LinkButton> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> </EditItemTemplate> <FooterTemplate> <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="AddNew" Text="Add New"></asp:LinkButton> </FooterTemplate> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> </ItemTemplate> </asp:TemplateField> <asp:CommandField HeaderText="Delete" ShowDeleteButton="True" ShowHeader="True" /> </Columns> </asp:GridView> </div> </form> </body> </html>







using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page 
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["customerConnectionString"].ToString());
 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            LoadGridView();
        }
    }
 
    private void LoadGridView()
    {
        SqlDataAdapter adap = new SqlDataAdapter("select * from cust_info", conn);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "AddNew")
        {
            string name = (GridView1.FooterRow.FindControl("txtNewName") as TextBox).Text;
            string gender = (GridView1.FooterRow.FindControl("cmbNewGender") as DropDownList).Text;
            string city = (GridView1.FooterRow.FindControl("txtNewCity") as TextBox).Text;
            string state = (GridView1.FooterRow.FindControl("txtNewState") as TextBox).Text;
            string type = (GridView1.FooterRow.FindControl("cmbNewType") as DropDownList).Text;
 
            SqlCommand comm = new SqlCommand();
            comm.CommandText = "insert into cust_info (name,gender,city,state,type) values(@name,@gender,@city,@state,@type)";
            comm.Connection = conn;
 
            comm.Parameters.AddWithValue("@name", name.ToString());
            comm.Parameters.AddWithValue("@gender", gender.ToString());
            comm.Parameters.AddWithValue("@city", city.ToString());
            comm.Parameters.AddWithValue("@state", state.ToString());
            comm.Parameters.AddWithValue("@type", type.ToString());
 
            conn.Open();
            comm.ExecuteNonQuery();
            conn.Close();
 
            LoadGridView();
 
        }
    }
 
    public DataTable FetchCustomerType()
    {
        string sql = "Select Distinct type From cust_info";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
 
    public DataTable FetchCustomerGender()
    {
        string sql = "Select Distinct gender From cust_info";
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dt = new DataTable();
        da.Fill(dt);
        return dt;
    }
 
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            DropDownList cmbtype = (DropDownList)e.Row.FindControl("cmbType");
            DropDownList cmbgnder = (DropDownList)e.Row.FindControl("cmbGender");
            if (cmbtype != null && cmbgnder != null)
            {
 
                cmbgnder.DataSource = FetchCustomerGender();
                cmbgnder.DataBind();
                cmbgnder.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[2].ToString();
                cmbtype.DataSource = FetchCustomerType();
                cmbtype.DataBind();
                cmbtype.SelectedValue = GridView1.DataKeys[e.Row.RowIndex].Values[1].ToString();
 
            }
        }
 

 
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            DropDownList cmbnewtype = (DropDownList)e.Row.FindControl("cmbNewType");
            DropDownList cmbnewgnder = (DropDownList)e.Row.FindControl("cmbNewGender");
            cmbnewgnder.DataSource = FetchCustomerGender();
            cmbnewgnder.DataBind();
            cmbnewtype.DataSource = FetchCustomerType();
            cmbnewtype.DataBind();
 

        }
        
        
            
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        GridViewRow row = GridView1.Rows[e.RowIndex] as GridViewRow;
 
        string id = (row.FindControl("lblID") as Label).Text;
        string name = (row.FindControl("txtName") as TextBox).Text;
        string gender = (row.FindControl("cmbGender") as DropDownList).Text;
        string city = (row.FindControl("txtCity") as TextBox).Text;
        string state = (row.FindControl("txtstate") as TextBox).Text;
        string type = (row.FindControl("cmbType") as DropDownList).Text;
 
        SqlCommand comm = new SqlCommand();
        comm.CommandText = "update cust_info set name=@name,city=@city,state=@state where id=@id";
        comm.Connection = conn;
 
        comm.Parameters.AddWithValue("@name", name.ToString());
        comm.Parameters.AddWithValue("@id", id.ToString());
        comm.Parameters.AddWithValue("@gender", gender.ToString());
        comm.Parameters.AddWithValue("@city", city.ToString());
        comm.Parameters.AddWithValue("@state", state.ToString());
        comm.Parameters.AddWithValue("@type", type.ToString());
 
        conn.Open();
        comm.ExecuteNonQuery();
        conn.Close();
 
        GridView1.EditIndex = -1;
        LoadGridView();
 
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        LoadGridView();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        LoadGridView();
    }
}



请建议我执行本示例"cmbType"时解决此示例

发生错误...



pls suggest me to solution for this example whn i execute this example "cmbType"

error occurly come...


该错误在CommandText字符串中-语句中缺少@type值,但已将其添加为参数.

您的commandtext只有4个参数,但是您已将5个参数添加到sql命令对象.
试试
comm.CommandText =更新cust_info设置名称= @名称,城市= @城市,状态= @状态,类型= @类型其中id = @ id";
The mistake is in the CommandText string - the @type value is missing from the statement, but you have added it as a parameter.

Your commandtext has only 4 parameters but you''ve added 5 to the sql command object.
Try
comm.CommandText = "update cust_info set name=@name,city=@city,state=@state,type=@type where id=@id";


这篇关于我无法在gridview中进行编辑更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆