问题描述:
被要求做一个网站的后台,可是我用gridview自带的编辑,删除功能时老出错“System.Data.SqlClient.SqlException: 数据类型 ntext 和 nvarchar 在 equal to 运算符中不兼容。”请问下怎么解决?如果哪位有后台的学习源码,请发我邮箱mapery@163.com,谢谢了!
问题解答:
恰好我刚写了一个和你一样的例子!
<asp:GridView ID="GVAdmin" runat="server" AutoGenerateColumns="False" OnRowEditing="GVAdmin_RowEditing" OnPageIndexChanging="GVAdmin_PageIndexChanging" OnRowCancelingEdit="GVAdmin_RowCancelingEdit" OnRowDeleting="GVAdmin_RowDeleting" DataKeyNames="AdminName" OnRowUpdating="GVAdmin_RowUpdating" Width="446px" AllowPaging="True" s>
<Columns>
<asp:TemplateField HeaderText="编号">
<ItemTemplate>
<%# Container.DataItemIndex+1 %>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="用户名">
<ItemTemplate>
<asp:Label ID="lblname" runat="server" Text='<%# Eval("AdminName") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtname" runat="server" Text='<%# Eval("AdminName") %>' Width="89px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="密码">
<ItemTemplate>
<asp:Label ID="lblpwd" runat="server" Text='<%# Eval("PWD") %>'></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txtpwd" runat="server" Text='<%# Eval("PWD") %>' Width="83px"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="操作" ShowHeader="False">
<EditItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
Text="更新"></asp:LinkButton>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
Text="取消"></asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
Text="编辑"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="删除" ShowDeleteButton="True" ButtonType="Button" />
</Columns>
</asp:GridView>
---------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SelectGVAdmin();
}
}
string strsql = null;
public void SelectGVAdmin()
{
strsql = "select * from Admin";
GVAdmin.DataSource = SQLHelper.Query(strsql);//调用写好的数据访问类
GVAdmin.DataBind();
}
protected void GVAdmin_RowEditing(object sender, GridViewEditEventArgs e) //编辑
{
GVAdmin.EditIndex = e.NewEditIndex;
SelectGVAdmin();
}
protected void GVAdmin_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) //取消
{
GVAdmin.EditIndex = -1;
SelectGVAdmin();
}
protected void GVAdmin_PageIndexChanging(object sender, GridViewPageEventArgs e) //分页
{
GVAdmin.PageIndex=e.NewPageIndex;
SelectGVAdmin();
}
protected void GVAdmin_RowDeleting(object sender, GridViewDeleteEventArgs e) //删除行数据
{
strsql = "delete from Admin where AdminName='"+GVAdmin.DataKeys[e.RowIndex].Value.ToString()+"'";
int result = SQLHelper.ExecuteSql(strsql);
SelectGVAdmin();
}
protected void GVAdmin_RowUpdating(object sender, GridViewUpdateEventArgs e) //行更新
{
strsql = "update Admin set AdminName='" + ((TextBox)(GVAdmin.Rows[e.RowIndex].FindControl("txtname"))).Text.ToString().Trim() + "',PWD='"+((TextBox)(GVAdmin.Rows[e.RowIndex].FindControl("txtpwd"))).Text.ToString().Trim()+"' where AdminName='"+GVAdmin.DataKeys[e.RowIndex].Value.ToString()+"'";
int result = SQLHelper.ExecuteSql(strsql);
GVAdmin.EditIndex = -1;
SelectGVAdmin();
}
使用向导生成的删除函数的参数太多了,只保留你的表的主键就可以,这个问题就会解决。