我正在使用SQL命令插入和删除网格,但在执行时遇到以下错误。
错误1使用未分配的本地变量' cmd' (这是INSERT INTO表(ID,摘要)值('" +转换)
错误2' System.DateTime'不包含'文字'的定义
错误3' System.EventArgs'不包含' CommandName'的定义没有扩展方法' CommandName'接受类型' System.EventArgs'的第一个参数。可以找到(你错过了使用指令或汇编参考吗?)
错误4' System.EventArgs'不包含' CommandArgument'的定义没有扩展方法' CommandArgument'接受类型' System.EventArgs'的第一个参数。可以找到(你错过了使用指令或程序集引用吗?)
错误5名称' fillgrid'在当前上下文中不存在
HTML部分是正确的。但我认为我没有正确编写插入和删除功能 这是HTML代码
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title> </title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="label" runat="server">New Task</asp:Label>
<asp:TextBox ID="textbox1" runat="server" placeHolder="Type
new task summary here, and click add" Width="353px">
</asp:TextBox>
<asp:Button ID="button1" runat="server" OnClick="click_add"
Text="Add" style="margin-left: 54px" Width="67px" />
<br />
</div>
<asp:GridView ID="grdStatus" runat="server"
OnRowCommand="grdStatus_RowCommand" AutoGenerateColumns="false">
<Columns>
<asp:TemplateField HeaderText="Done?">
<ItemTemplate>
<asp:CheckBox ID="checkbx" runat="server"
Checked='<%#Eval("Done") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="label12" runat="server"
Checked='<%#Container.DataItemIndex+1 %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Summary">
<ItemTemplate>
<asp:Label ID="label3" runat="server"
Checked='<%#Eval("Summary") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Created On">
<ItemTemplate>
<asp:Label ID="label4" runat="server"
Checked='<%#Eval("Date")%>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="dltButton" runat="server"
ImageUrl="~/images/delete.png"
CommandName="DeleteTask" Text="Delete" Width="30px"
Height="30px" CommandArgument="<%#
Container.DataItemIndex%>" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
</body>
</html>
c#代码就是这个
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string connectionString =
WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
string selectSQL = "SELECT * FROM Table";
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand(selectSQL, con);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adapter.Fill(ds, "Table");
grdStatus.DataSource = ds;
grdStatus.DataBind();
}
}
protected void click_add(object sender, EventArgs e)
{
string connectionString =
WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("INSERT INTO
Table(ID,Summary) values ('" +
convert.ToInt32(cmd.Parameters["label2"].Value.ToString())
+ "'" + textbox1.Text + "'" DateTime.Text+ "'", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
protected void grdStatus_RowCommand(object sender, EventArgs e)
{
if(e.CommandName == "DeleteTask")
{
string connectionString =
WebConfigurationManager.ConnectionStrings["Northwind"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
con.Open();
string query = "delete from Table where id=" +
e.CommandArgument + "'";
SqlCommand cmd = new SqlCommand(query, con);
cmd.ExecuteNonQuery();
con.Close();
fillgrid();
}
}
}