代码如下:
person.cs
using System;namespace WebApplication2._17
{
/// <summary>
/// person 的摘要说明。
/// </summary>
public class person
{ public string pID;
public string pName;
public string pSex;
public person()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
}
=========================================
personOperate.cs 文件
===================================
using System;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication2._17
{
/// <summary>
/// personOperate 的摘要说明。
/// </summary>
public class personOperate
{
public personOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection creatCon()
{
return new SqlConnection("server=wangfeng;database=adonettest;uid=sa;pwd=:"); }
public static bool findPerson(string pID)
{
SqlConnection con=personOperate.creatCon();
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from person where pID='"+pID+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
return true ; }
else
{
return false;
} }
public static DataTable selectAllPerson()
{
SqlConnection con=personOperate.creatCon();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select *from person",con);
DataSet ds=new DataSet();
sda.Fill(ds,"person");
return ds.Tables["table"]; }
public static bool insertOperate(person p)
{
try
{
SqlConnection con=personOperate.creatCon();
con.Open();
SqlCommand cmd=new SqlCommand("insert into person values(@pID,@pName,@pSex)",con);
SqlParameter para=new SqlParameter("@pID",SqlDbType.VarChar,10);
para.Value=p.pID;
cmd.Parameters.Add(para);
para=new SqlParameter("@pName",SqlDbType.VarChar,20);
para.Value=p.pName;
cmd.Parameters.Add(para);
para=new SqlParameter("@pSex",SqlDbType.VarChar,2);
para.Value=p.pSex;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
return true;
}
catch(Exception e)
{
return false;
}
}
}
}
===================================
adonet 文件
================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2._17
{
/// <summary>
/// adonet 的摘要说明。
/// </summary>
public class adonet : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox txtpid;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox txtname;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.RadioButton rbtnnan;
protected System.Web.UI.WebControls.RadioButton rbtnnv;
protected System.Web.UI.WebControls.CustomValidator CustomValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.Button btnadd;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.btnadd.Click += new System.EventHandler(this.btnadd_Click);
this.CustomValidator1.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator1_ServerValidate);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string pID=args.Value;
if(personOperate.findPerson(pID))
{
args.IsValid=false; }
else
{
args.IsValid=true;
}
}
private void fillDg()
{
this.DataGrid1.DataSource=personOperate.selectAllPerson();
this.DataGrid1.DataBind();
} private void btnadd_Click(object sender, System.EventArgs e)
{
if(this.IsValid)
{
person p=new person();
p.pID=this.txtpid.Text;
p.pName=this.txtname.Text;
if(this.rbtnnan.Checked)
{
p.pSex="男";
}
else
{
p.pSex="女";
}
if(personOperate.insertOperate(p))
{
Response.Write("插入成功");
this.fillDg();
}
else
{
Response.Write("插入失败");
}
}
}
}
}
==========================================
文件的代码如上 当测试的时候总是报错
希望大家能够帮助 谢谢了 !
person.cs
using System;namespace WebApplication2._17
{
/// <summary>
/// person 的摘要说明。
/// </summary>
public class person
{ public string pID;
public string pName;
public string pSex;
public person()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
}
}
=========================================
personOperate.cs 文件
===================================
using System;
using System.Data;
using System.Data.SqlClient;
namespace WebApplication2._17
{
/// <summary>
/// personOperate 的摘要说明。
/// </summary>
public class personOperate
{
public personOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public static SqlConnection creatCon()
{
return new SqlConnection("server=wangfeng;database=adonettest;uid=sa;pwd=:"); }
public static bool findPerson(string pID)
{
SqlConnection con=personOperate.creatCon();
con.Open();
SqlCommand cmd=new SqlCommand("select count(*) from person where pID='"+pID+"'",con);
int count=Convert.ToInt32(cmd.ExecuteScalar());
if(count>0)
{
return true ; }
else
{
return false;
} }
public static DataTable selectAllPerson()
{
SqlConnection con=personOperate.creatCon();
SqlDataAdapter sda=new SqlDataAdapter();
sda.SelectCommand=new SqlCommand("select *from person",con);
DataSet ds=new DataSet();
sda.Fill(ds,"person");
return ds.Tables["table"]; }
public static bool insertOperate(person p)
{
try
{
SqlConnection con=personOperate.creatCon();
con.Open();
SqlCommand cmd=new SqlCommand("insert into person values(@pID,@pName,@pSex)",con);
SqlParameter para=new SqlParameter("@pID",SqlDbType.VarChar,10);
para.Value=p.pID;
cmd.Parameters.Add(para);
para=new SqlParameter("@pName",SqlDbType.VarChar,20);
para.Value=p.pName;
cmd.Parameters.Add(para);
para=new SqlParameter("@pSex",SqlDbType.VarChar,2);
para.Value=p.pSex;
cmd.Parameters.Add(para);
cmd.ExecuteNonQuery();
return true;
}
catch(Exception e)
{
return false;
}
}
}
}
===================================
adonet 文件
================================
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace WebApplication2._17
{
/// <summary>
/// adonet 的摘要说明。
/// </summary>
public class adonet : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox txtpid;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.TextBox txtname;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.RadioButton rbtnnan;
protected System.Web.UI.WebControls.RadioButton rbtnnv;
protected System.Web.UI.WebControls.CustomValidator CustomValidator1;
protected System.Web.UI.WebControls.RequiredFieldValidator RequiredFieldValidator1;
protected System.Web.UI.WebControls.Button btnadd;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
} #region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.btnadd.Click += new System.EventHandler(this.btnadd_Click);
this.CustomValidator1.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator1_ServerValidate);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void CustomValidator1_ServerValidate(object source, System.Web.UI.WebControls.ServerValidateEventArgs args)
{
string pID=args.Value;
if(personOperate.findPerson(pID))
{
args.IsValid=false; }
else
{
args.IsValid=true;
}
}
private void fillDg()
{
this.DataGrid1.DataSource=personOperate.selectAllPerson();
this.DataGrid1.DataBind();
} private void btnadd_Click(object sender, System.EventArgs e)
{
if(this.IsValid)
{
person p=new person();
p.pID=this.txtpid.Text;
p.pName=this.txtname.Text;
if(this.rbtnnan.Checked)
{
p.pSex="男";
}
else
{
p.pSex="女";
}
if(personOperate.insertOperate(p))
{
Response.Write("插入成功");
this.fillDg();
}
else
{
Response.Write("插入失败");
}
}
}
}
}
==========================================
文件的代码如上 当测试的时候总是报错
希望大家能够帮助 谢谢了 !
这个你看下源码,是没有定义就使用了.另外我怎么看你这代码像是北大青鸟那里面请讲的代码.
OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged"//在HTML中加上这句 AutoGenerateColumns="False" BorderColor="#93BEE2" Width="100%" AllowPaging="True" PageSize="15">
我按照上面的前辈的做法作了 但是还是有同样的报错 信息
=====================
<%@ Page language="c#" Codebehind="adonet.aspx.cs" AutoEventWireup="false" Inherits="WebApplication2._17.adonet" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>adonet</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:DataGrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 336px; POSITION: absolute; TOP: 296px"
runat="server" Width="296px" Height="216px" DataKeyField="CarID" Font-Size="10.5" OnSelectedIndexChanged="DataGrid1_SelectedIndexChanged"
AutoGenerateColumns="False" BorderColor="#0000ff"></asp:DataGrid>
<asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 344px; POSITION: absolute; TOP: 64px" runat="server">编号:</asp:Label>
<asp:TextBox id="txtpid" style="Z-INDEX: 103; LEFT: 432px; POSITION: absolute; TOP: 64px" runat="server"></asp:TextBox>
<asp:Label id="Label2" style="Z-INDEX: 104; LEFT: 352px; POSITION: absolute; TOP: 120px" runat="server">姓名:</asp:Label>
<asp:TextBox id="txtname" style="Z-INDEX: 105; LEFT: 432px; POSITION: absolute; TOP: 120px" runat="server"></asp:TextBox>
<asp:Label id="Label3" style="Z-INDEX: 106; LEFT: 352px; POSITION: absolute; TOP: 176px" runat="server">性别</asp:Label>
<asp:RadioButton id="rbtnnan" style="Z-INDEX: 107; LEFT: 440px; POSITION: absolute; TOP: 176px" runat="server"
Text="男" GroupName="sex"></asp:RadioButton>
<asp:RadioButton id="rbtnnv" style="Z-INDEX: 108; LEFT: 544px; POSITION: absolute; TOP: 176px" runat="server"
Text="女" GroupName="sex"></asp:RadioButton>
<asp:Button id="btnadd" style="Z-INDEX: 109; LEFT: 368px; POSITION: absolute; TOP: 232px" runat="server"
Text="添加"></asp:Button>
<asp:CustomValidator id="CustomValidator1" style="Z-INDEX: 110; LEFT: 624px; POSITION: absolute; TOP: 64px"
runat="server" ErrorMessage="*">该用户存在</asp:CustomValidator>
<asp:RequiredFieldValidator id="RequiredFieldValidator1" style="Z-INDEX: 111; LEFT: 640px; POSITION: absolute; TOP: 120px"
runat="server" ErrorMessage="*" ControlToValidate="txtname"></asp:RequiredFieldValidator>
</form>
</body>
</HTML>
private void InitializeComponent()
{
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.btnadd.Click += new System.EventHandler(this.btnadd_Click);
this.CustomValidator1.ServerValidate += new System.Web.UI.WebControls.ServerValidateEventHandler(this.CustomValidator1_ServerValidate);
this.Load += new System.EventHandler(this.Page_Load);
}
中
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
删掉即可
D:\Inetpub\wwwroot\WebApplication2\17+有错误\personOperate.cs(68): 声明了变量“e”,但从未使用过
这个警告信息没关系
D:\Inetpub\wwwroot\WebApplication2\17+有错误\adonet.aspx.cs(51): “WebApplication2._17.adonet”并不包含对“DataGrid1_SelectedIndexChanged”的定义把
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1.SelectedIndexChanged);代码删掉,因为你的程序没有DataGrid1_SelectedIndexChanged()方法,但却注册了!
--------------------------------------------------------------------------
(2)
D:\Inetpub\wwwroot\WebApplication2\17+有错误\personOperate.cs(68): 声明了变量“e”,但从未使用过
写成catch(Exception e)
{
throw e;
return false;
}或
catch()
{
return false;
}
“/WebApplication2”应用程序中的服务器错误。
--------------------------------------------------------------------------------编译错误
说明: 在编译向该请求提供服务所需资源的过程中出现错误。请检查下列特定错误详细信息并适当地修改源代码。 编译器错误信息: CS0117: “ASP.adonet_aspx”并不包含对“DataGrid1_SelectedIndexChanged”的定义源错误: 行 11: namespace ASP {
行 12: using System;
行 13: using System.Collections;
行 14: using System.Collections.Specialized;
行 15: using System.Configuration;
源文件: http://localhost/WebApplication2/17+有错误/adonet.aspx 行: 13