下面是ASPX文件代码
<form id="Form1" method="post" runat="server">
<asp:datagrid id="dgly" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 88px" runat="server"
AutoGenerateColumns="False" Width="241px" PageSize="3" AllowPaging="True">
<Columns>
<asp:BoundColumn DataField="enName" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="chName"></asp:BoundColumn>
<asp:BoundColumn DataField="oicq"></asp:BoundColumn>
<asp:BoundColumn DataField="phone"></asp:BoundColumn>
<asp:BoundColumn DataField="ly"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Save" CancelText="Cannel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:datagrid></form>
<form id="Form1" method="post" runat="server">
<asp:datagrid id="dgly" style="Z-INDEX: 101; LEFT: 120px; POSITION: absolute; TOP: 88px" runat="server"
AutoGenerateColumns="False" Width="241px" PageSize="3" AllowPaging="True">
<Columns>
<asp:BoundColumn DataField="enName" ReadOnly="True"></asp:BoundColumn>
<asp:BoundColumn DataField="chName"></asp:BoundColumn>
<asp:BoundColumn DataField="oicq"></asp:BoundColumn>
<asp:BoundColumn DataField="phone"></asp:BoundColumn>
<asp:BoundColumn DataField="ly"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Save" CancelText="Cannel" EditText="Edit"></asp:EditCommandColumn>
</Columns>
<PagerStyle Mode="NumericPages"></PagerStyle>
</asp:datagrid></form>
解决方案 »
- 众神归位看帖子吧
- 博客个人用户首页地址(www.xxx.com/xxx)这种URL正则如何处理?
- oracle时间格式问题
- 未能加载文件或程序集“UrlRewritingNet.UrlRewriter”或它的某一个依赖项
- JS取后台字符串做数组。。。。
- 新建项目-aspnet应用程序时,提示错误“503:service unavaliable”?在线等待
- 为什么用IP访问web服务器会弹出登录网域的对话框,而用机器名访问就不弹出呢?
- ConnectionString 属性尚未初始化。怎么做初始化呢?
- 大送分啊,关于treeview的编程
- SQL 使用存储过程问题
- 请问哪里有OracleHelper下载? 谢谢了
- sql2000存储图片的问题,抄了网上的小代码,出现错误,帮我看看,急!~~~~~~~
public class WebForm2 : System.Web.UI.Page
{
public System.Web.UI.WebControls.DataGrid dgly;
public String strConnection = "server=(local);user id=sa;pwd=sa;database=lybbook;";
public String strSQL = "select * from liuyan";
public String lyTableName = "lyTable";
public SqlConnection objConnection;
public void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
LoadGrid();
} public void LoadGrid()
{
Connect();
SqlDataAdapter adapter = new SqlDataAdapter(strSQL,objConnection);
DataSet ds = new DataSet();
adapter.Fill(ds,lyTableName);
Disconnect(); dgly.DataSource = ds.Tables[lyTableName];
dgly.DataBind();
} public void Connect()
{
if(objConnection == null)
objConnection = new SqlConnection(strConnection); if(objConnection.State == ConnectionState.Closed)
objConnection.Open();
} public void Disconnect()
{
objConnection.Close();
} public void EditRecord(object sender, DataGridCommandEventArgs e)
{
dgly.EditItemIndex = e.Item.ItemIndex;
LoadGrid();
} public void CannelEdit(object sender, DataGridCommandEventArgs e)
{
dgly.EditItemIndex=-1;
LoadGrid();
} public void UpdateRecord(object sender, DataGridCommandEventArgs e)
{
String strConnection = "server=(local);user id=sa;pwd=sa;database=lybbook;";
SqlConnection objConnection = new SqlConnection(strConnection);
String strSQL1 = "UPDATE liuyan SET chName=@chName, oicq=@oicq, phone=@phone, ly=@ly";
SqlDataAdapter adapter1 = new SqlDataAdapter(strSQL1, objConnection);
adapter1.UpdateCommand.Parameters["chName"].Value = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
adapter1.UpdateCommand.Parameters["oicq"].Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
adapter1.UpdateCommand.Parameters["phonee"].Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
adapter1.UpdateCommand.Parameters["ly"].Value = ((TextBox)e.Item.Cells[4].Controls[0]).Text; adapter1.UpdateCommand.Connection.Open();
adapter1.UpdateCommand.ExecuteNonQuery();
adapter1.UpdateCommand.Connection.Close() ;
dgly.EditItemIndex = -1;
LoadGrid();
}
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。源错误:
行 79: String strSQL1 = "UPDATE liuyan SET chName=@chName, oicq=@oicq, phone=@phone, ly=@ly";
行 80: SqlDataAdapter adapter1 = new SqlDataAdapter(strSQL1, objConnection);
行 81: adapter1.UpdateCommand.Parameters["chName"].Value = ((TextBox)e.Item.Cells[1].Controls[0]).Text;
行 82: adapter1.UpdateCommand.Parameters["oicq"].Value = ((TextBox)e.Item.Cells[2].Controls[0]).Text;
行 83: adapter1.UpdateCommand.Parameters["phonee"].Value = ((TextBox)e.Item.Cells[3].Controls[0]).Text;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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 student
{
/// <summary>
/// WebForm28 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!IsPostBack)
{
SetBind();
SetBind2();
}
} protected void SetBind()
{ SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da=new SqlDataAdapter("select * from stu,dep where stu.studepid=dep.depid",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
} protected void SetBind2()
{ SqlConnection conn2=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da2=new SqlDataAdapter("select * from dep",conn2);
DataSet ds2=new DataSet();
da2.Fill(ds2,"table1");
this.DropDownList1.DataSource=ds2.Tables["table1"];
this.DropDownList1.DataTextField="depname";
this.DropDownList1.DataValueField="depid";
this.DropDownList1.DataBind();
this.DropDownList1.Items.Insert(0,new ListItem("请选择",""));
} protected void SetBind3()
{
string s=this.DropDownList1.SelectedValue;
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlCommand comm=new SqlCommand();
comm.Connection=conn;
if(s!="")
{
comm.CommandText="select * from stu,dep where stu.studepid=dep.depid and depid=@depid";
SqlParameter parm1=new SqlParameter("@depid",SqlDbType.Int);
parm1.Value=s;
comm.Parameters.Add(parm1);
}
else
comm.CommandText="select * from stu,dep where stu.studepid=dep.depid";
SqlDataAdapter da=new SqlDataAdapter();
da.SelectCommand=comm;
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.DropDownList1.SelectedIndexChanged += new System.EventHandler(this.DropDownList1_SelectedIndexChanged);
this.DataGrid1.ItemDataBound += new System.Web.UI.WebControls.DataGridItemEventHandler(this.DataGrid1_ItemDataBound);
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation id=JINBING;packet size=4096;user id=sa;data source=JINBING;persist se" +
"curity info=False;initial catalog=isswhu";
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da=new SqlDataAdapter("select * from dep",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
if(e.Item.ItemType==ListItemType.EditItem)
{
DropDownList ddl=(DropDownList)e.Item.FindControl("dep");
ddl.DataSource=ds.Tables["table1"];
ddl.DataTextField="depname";
ddl.DataValueField="depid";
ddl.DataBind();
ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"depid"))).Selected=true;//选择数据库内的作为默认
}
} protected void edit(object sender,DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
if(this.DropDownList1.SelectedValue=="")
SetBind();
else
SetBind3();
} protected void cancel(object sender,DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
if(this.DropDownList1.SelectedValue=="")
SetBind();
else
SetBind3();
} protected void update(object sender,DataGridCommandEventArgs e)
{
if(e.Item.ItemType==ListItemType.EditItem)//只有在编辑按下以后才能提交
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlCommand comm=new SqlCommand("update stu set stuname=@name,studepid=@depid where stuid=@id",conn);
SqlParameter parm1=new SqlParameter("@name",SqlDbType.NVarChar,50);
parm1.Value=((TextBox)e.Item.FindControl("name")).Text;
SqlParameter parm2=new SqlParameter("@depid",SqlDbType.Int);
parm2.Value=((DropDownList)e.Item.FindControl("dep")).SelectedValue;
SqlParameter parm3=new SqlParameter("@id",SqlDbType.Int);
parm3.Value=this.DataGrid1.DataKeys[e.Item.ItemIndex];
comm.Parameters.Add(parm1);
comm.Parameters.Add(parm2);
comm.Parameters.Add(parm3);
conn.Open();
comm.ExecuteNonQuery();
conn.Close();
this.DataGrid1.EditItemIndex=-1;
if(this.DropDownList1.SelectedValue=="")
SetBind();
else
SetBind3();//如果选择过滤则使用SetBind3()
}
} private void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e)
{
SetBind3();
}
}
}
应该加上@,即
adapter1.UpdateCommand.Parameters["@chName"]......
构造参数应该是select command,最好不要通过这种方法来更新数据