SqlDataSource 里可以修改行,删除行却不能吗?
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:zxiaoaiConnectionString %>"
SelectCommand="SELECT * FROM [column]" DeleteCommand="DELETE FROM [column] WHERE (colid = @colid)" UpdateCommand="UPDATE [column] SET name1 = @name1, url = @url WHERE (colid = @colid)"
>
<DeleteParameters>
<asp:Parameter Name="colid" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="name1" />
<asp:Parameter Name="url" />
<asp:Parameter Name="colid" />
</UpdateParameters>
</asp:SqlDataSource>
<br />
<table>
<tr>
<td colspan="3" rowspan="3" style="width: 1511px; height: 256px">
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="629px" DataSourceID="SqlDataSource1">
<Columns>
<asp:BoundField DataField="colid" HeaderText="colid" InsertVisible="False" SortExpression="colid" />
<asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />
<asp:BoundField DataField="name1" HeaderText="name1" SortExpression="name1" />
<asp:BoundField DataField="url" HeaderText="url" SortExpression="url" />
<asp:CommandField ShowEditButton="True" />
<asp:CommandField ShowDeleteButton="True" />
</Columns>
</asp:GridView>
执行修改没问题,删除时却提示
System.Data.SqlClient.SqlException: 必须声明标量变量 "@colid"。
------解决方案--------------------
你先仔细看看你执行修改的那条语句,如何得到@colid
提示很明显是说你@colid没有赋值.
一般我用的是代码来做删除的
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection cn = new SqlConnection(@"server=.\sqlexpress;uid=sa;pwd=;database=pubs");
SqlCommand cmd = new SqlCommand("delete from authors where au_id=@au_id", cn);
cmd.Parameters.Add("@au_id",SqlDbType.VarChar).Value=GridView1.DataKeys[e.RowIndex].Value.ToString();
cn.Open();
cmd.ExecuteNonQuery();
cn.Close();
GridView1.EditIndex = -1;
BindGrid();
}