我在下面的代码中有错误我试图插入,更新,删除................. [英] i have error in this below code am trying to insert,update,delete.................
问题描述
这是我的aspx.cs代码.........
this is my aspx.cs code.........
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.IO;
public partial class gallery : System.Web.UI.Page
{
String fn;
String path;
SqlConnection cnn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
SqlDataAdapter adp;
DataTable dt;
Int32 id;
String album_name;
String caption;
String image;
protected void Page_Load(object sender, EventArgs e)
{
cnn.ConnectionString = ConfigurationManager.ConnectionStrings["Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True"].ConnectionString;
cnn.Open();
if (cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
cnn.Close();
if (IsPostBack == false)
{
grd_bind();
}
}
protected void btninsert_Click(object sender, EventArgs e)
{
if (txtuploder.PostedFile.ContentLength > 0)
{
fn = Path.GetFileName(txtuploder.FileName);
path = Server.MapPath("images") + "/" + fn;
txtuploder.SaveAs(path);
}
if (cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
SqlCommand cmd = new SqlCommand("tb_gallery_insert", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cnn;
cmd.Parameters.AddWithValue("@album_name", txtalbum.Text);
cmd.Parameters.AddWithValue("@caption", txtcaption.Text);
cmd.Parameters.AddWithValue("@image", fn);
cmd.ExecuteNonQuery();
cmd.Dispose();
grd_bind();
cnn.Close();
clr();
}
private void grd_bind()
{
if (cnn.State == ConnectionState.Closed)
{
cnn.Open(); }
adp = new SqlDataAdapter("SELECT * FROM tb_gallery ", cnn);
dt = new DataTable();
adp.Fill(dt);
adp.Dispose();
GridView1.DataSource = dt;
GridView1.DataBind();
}
private void clr()
{
txtalbum.Text = "";
txtcaption.Text = "";
}
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
try
{
if (cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);
SqlCommand cmd = new SqlCommand("tb_gallery_delete", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cnn;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
SqlDataAdapter adp = new SqlDataAdapter("select * from tb_gallery where id=@id", cnn);
adp.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = id;
DataSet ds = new DataSet();
adp.Fill(ds);
try
{
image = Convert.ToString(ds.Tables[0].Rows[0]["image"]);
File.Delete(Server.MapPath("images") + "\\" + image);
}
catch { }
cmd.ExecuteNonQuery();
cmd.Dispose();
grd_bind();
}
catch {
}
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
grd_bind();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
grd_bind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (cnn.State == ConnectionState.Closed)
{
cnn.Open();
}
id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label4"))).Text);
album_name = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_album_name"))).Text);
caption = (((TextBox)(GridView1.Rows[e.RowIndex].FindControl("txt_caption"))).Text);
SqlCommand cmd = new SqlCommand("tb_gallery_update", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = cnn;
cmd.Parameters.Add("@id", SqlDbType.Int).Value = id;
cmd.Parameters.Add("@album_name", SqlDbType.VarChar, 50).Value = album_name;
cmd.Parameters.Add("@caption", SqlDbType.VarChar, 50).Value = caption;
cmd.ExecuteNonQuery();
cmd.Dispose();
GridView1.EditIndex = -1;
grd_bind();
}
protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
GridView1.PageIndex = e.NewSelectedIndex;
grd_bind();
}
}
this is my aspx code...........
lang="xml"><%@ Page Language="C#" AutoEventWireup="true" CodeFile="gallery.aspx.cs" Inherits="gallery" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:TextBox ID="txtalbum" runat="server"></asp:TextBox>
<asp:Label ID="Label1" runat="server" Text="AlbumName"></asp:Label>
<br />
<br />
<asp:TextBox ID="txtcaption" runat="server"></asp:TextBox>
<asp:Label ID="Label2" runat="server" Text="Caption"></asp:Label>
<br />
<br />
<asp:FileUpload ID="txtuploder" runat="server" />
<asp:Label ID="Label3" runat="server" Text="Image"></asp:Label>
<br />
<br />
<asp:Button ID="btninsert" runat="server" Text="Insert"
onclick="btninsert_Click" />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onselectedindexchanging="GridView1_SelectedIndexChanging"
onselectedindexchanged="GridView1_SelectedIndexChanged">
<Columns>
<%--here i am using templatefields to for binding the gridview--%>
<asp:TemplateField HeaderText="Album_name">
<EditItemTemplate>
<%--here i am using the textbox in the edititmtemplatefield to upadate the data--%>
<asp:TextBox ID="txt_album_name" runat="server"
Text='<%# Eval("album_name") %>'></asp:TextBox>
<asp:Label ID="Label4" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Eval("album_name") %>'></asp:Label>
<asp:Label ID="Label2" runat="server" Text='<%# Eval("id") %>' Visible="False"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Caption">
<EditItemTemplate>
<asp:TextBox ID="txt_caption" runat="server" Text='<%# Eval("caption") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Eval("caption") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Image">
<ItemTemplate>
<%--for displaying the image inside the gidview here i'm using the <img>tag
and specify the path of the folder where image is stored--%>
<img alt ="" src ='images/<%#Eval("image") %>' height="50px" width="50px"/>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<%--here i am using the linkbutton to delete the record and specify the command name
of this linkbutton is delete--%>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False"
CommandName="Delete"
onclientclick="return confirm('are you sure you want to delet this column')">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Update">
<EditItemTemplate>
<%-- here i am using the s linkbuttons to update the record and a cancel button
and set the commandname of update button is update and the cancel button is cancel --%>
<asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="False"
CommandName="Update">Update</asp:LinkButton>
<asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="False"
CommandName="Cancel">Cancel</asp:LinkButton>
</EditItemTemplate>
<ItemTemplate>
<%--here i am using the linkbutton for edit and specify the command name
of this linkbutton is Edit--%>
<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False"
CommandName="Edit">Edit</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
这是错误............... ...
'/ WebSite11'应用程序中的服务器错误。
编译错误
描述:错误在编译服务此请求所需的资源期间发生。请查看以下特定错误详细信息并相应地修改源代码。
编译器错误消息:CS1061:'System.Web.UI.WebControls.GridViewDeletedEventArgs'不包含'RowIndex'的定义,没有扩展方法'RowIndex'接受类型'System.Web.UI.WebControls.GridViewDeletedEventArgs'的第一个参数可以找到(你是否缺少using指令或汇编引用?)
来源错误:
this is error..................
Server Error in '/WebSite11' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS1061: 'System.Web.UI.WebControls.GridViewDeletedEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewDeletedEventArgs' could be found (are you missing a using directive or an assembly reference?)
Source Error:
Line 105: }<br />
Line 106:<br />
Line 107: id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);<br />
Line 108: <br />
Line 109: SqlCommand cmd = new SqlCommand("tb_gallery_delete", cnn);
推荐答案
在这种情况下,我建议阅读异常消息。类System.Web.UI.WebControls.GridViewDeletedEventArgs
确实没有名为'RowIndex
'的成员,至少如果我能相信MSDN。以下是您发布的内容:
In this case I would recommend reading the exception message. The classSystem.Web.UI.WebControls.GridViewDeletedEventArgs
indeed has no member with the name 'RowIndex
', at least if I can believe MSDN. Here is what you posted:
//Line 107:
id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);
假设 e
确实属于 GridViewDeletedEventArgs
类型,这不会编译并导致您发布的错误。除了查看MSDN中的类 GridViewDeletedEventArgs
然后修复此事件处理程序之外,您别无选择。
Assuming that e
is indeed of the type GridViewDeletedEventArgs
, this will not compile and cause exactly the error you have posted. You will have little other choice than to take a look at the class GridViewDeletedEventArgs
in MSDN and then fix this event handler.
使用datakeys原地删除
活动
删除
use datakeys inplace ofDelete
Event
remove
id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);
使用datakeys
using datakeys
使用它代替
id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("label2"))).Text);
使用数据键
using datakeys
string drawid = GridView1.DataKeys[e.RowIndex].Value.ToString();
这篇关于我在下面的代码中有错误我试图插入,更新,删除.................的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!