当前位置: 动力学知识库 > 问答 > 编程问答 >

c# - Insert and Delete error Gridview ASP.NET

问题描述:

I am using SQL command to insert and delete into the grid but getting following errors while executing.

Error 1 Use of unassigned local variable 'cmd' (This is at INSERT INTO Table(ID,Summary) values ('" + Convert)

Error 2 'System.DateTime' does not contain a definition for 'Text'

Error 3 'System.EventArgs' does not contain a definition for 'CommandName' and no extension method 'CommandName' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

Error 4 'System.EventArgs' does not contain a definition for 'CommandArgument' and no extension method 'CommandArgument' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

Error 5 The name 'fillgrid' does not exist in the current context

The HTML part is correct . But I think I have not written the insert and delete function properly

This is the HTML code

<%@ 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>

The c# code is this

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();

}

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: