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;
using System.Data.SqlClient;namespace my
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
BindDataGrid();
}
} private void BindDataGrid()
{
SqlConnection sqlConnection1 = new SqlConnection("Data source = larry;Integrated security = false;user id = sa;database = Northwind;");
string sqlQuery = "SELECT EmployeeID,FirstName,LastName,Title,Extension FROM Employees";
sqlConnection1.Open();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlQuery,sqlConnection1);
DataSet dataSet = new DataSet();
sqlDataAdapter.Fill(dataSet,"Employees");
DataGrid1.DataSource = dataSet.Tables["Employees"].DefaultView;
DataGrid1.DataKeyField = "EmployeeID";
DataGrid1.DataBind();
sqlConnection1.Close(); } #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.SelectedIndex = -1;
BindDataGrid();
} private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string key = DataGrid1.DataKeys[DataGrid1.SelectedIndex].ToString();
string sqlQuery = "SELECT Notes FROM EmployeeID = @EmployeeID"; SqlConnection sqlConnection1 = new SqlConnection("Data source = larry;Integrated security = false;user id = sa;database = Northwind;");
sqlConnection1.Open();
SqlCommand sqlCommand = new SqlCommand(sqlQuery,sqlConnection1);
sqlCommand.Parameters.Add("@EmployeeID",key);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if(sqlDataReader.Read())
{
Label1.Text =sqlDataReader["Notes"].ToString();
Panel1.Visible = true; }
sqlDataReader.Close();
sqlConnection1.Close();
} private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindDataGrid();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string key = DataGrid1.DataKeys[DataGrid1.EditItemIndex].ToString();
string sqlQuery = "UPDATA Employees SET FirstName = @FirstName,LastName = @LastName,Title = @Title,Extension = @Extension WHERE EmployeeID = @EmployeeID";
SqlConnection sqlConnection1 = new SqlConnection("Data source = larry;Integrated security = false;user id = sa;database = Northwind;");
sqlConnection1.Open();
SqlCommand sqlCommand = new SqlCommand(sqlQuery,sqlConnection1);
sqlCommand.Parameters.Add("@EmployeeID",key);
sqlCommand.Parameters.Add("@FristName",((TextBox)(e.Item.Cells[1].Controls[0])).Text);
sqlCommand.Parameters.Add("@LastName",((TextBox)(e.Item.Cells[2].Controls[0])).Text);
sqlCommand.Parameters.Add("@Title",((TextBox)(e.Item .Cells[3].Controls[0])).Text);
sqlCommand.Parameters.Add("@Extension",((TextBox)(e.Item.Cells[4].Controls[0])).Text); sqlCommand.ExecuteNonQuery();
sqlConnection1.Close(); DataGrid1.EditItemIndex = -1;
BindDataGrid(); }
}
}
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;
using System.Data.SqlClient;namespace my
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Panel Panel1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
if(!Page.IsPostBack)
{
BindDataGrid();
}
} private void BindDataGrid()
{
SqlConnection sqlConnection1 = new SqlConnection("Data source = larry;Integrated security = false;user id = sa;database = Northwind;");
string sqlQuery = "SELECT EmployeeID,FirstName,LastName,Title,Extension FROM Employees";
sqlConnection1.Open();
SqlDataAdapter sqlDataAdapter = new SqlDataAdapter(sqlQuery,sqlConnection1);
DataSet dataSet = new DataSet();
sqlDataAdapter.Fill(dataSet,"Employees");
DataGrid1.DataSource = dataSet.Tables["Employees"].DefaultView;
DataGrid1.DataKeyField = "EmployeeID";
DataGrid1.DataBind();
sqlConnection1.Close(); } #region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.PageIndexChanged += new System.Web.UI.WebControls.DataGridPageChangedEventHandler(this.DataGrid1_PageIndexChanged);
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.SelectedIndexChanged += new System.EventHandler(this.DataGrid1_SelectedIndexChanged);
this.Load += new System.EventHandler(this.Page_Load); }
#endregion private void DataGrid1_PageIndexChanged(object source, System.Web.UI.WebControls.DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
DataGrid1.SelectedIndex = -1;
BindDataGrid();
} private void DataGrid1_SelectedIndexChanged(object sender, System.EventArgs e)
{
string key = DataGrid1.DataKeys[DataGrid1.SelectedIndex].ToString();
string sqlQuery = "SELECT Notes FROM EmployeeID = @EmployeeID"; SqlConnection sqlConnection1 = new SqlConnection("Data source = larry;Integrated security = false;user id = sa;database = Northwind;");
sqlConnection1.Open();
SqlCommand sqlCommand = new SqlCommand(sqlQuery,sqlConnection1);
sqlCommand.Parameters.Add("@EmployeeID",key);
SqlDataReader sqlDataReader = sqlCommand.ExecuteReader(); if(sqlDataReader.Read())
{
Label1.Text =sqlDataReader["Notes"].ToString();
Panel1.Visible = true; }
sqlDataReader.Close();
sqlConnection1.Close();
} private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindDataGrid();
} private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindDataGrid();
} private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string key = DataGrid1.DataKeys[DataGrid1.EditItemIndex].ToString();
string sqlQuery = "UPDATA Employees SET FirstName = @FirstName,LastName = @LastName,Title = @Title,Extension = @Extension WHERE EmployeeID = @EmployeeID";
SqlConnection sqlConnection1 = new SqlConnection("Data source = larry;Integrated security = false;user id = sa;database = Northwind;");
sqlConnection1.Open();
SqlCommand sqlCommand = new SqlCommand(sqlQuery,sqlConnection1);
sqlCommand.Parameters.Add("@EmployeeID",key);
sqlCommand.Parameters.Add("@FristName",((TextBox)(e.Item.Cells[1].Controls[0])).Text);
sqlCommand.Parameters.Add("@LastName",((TextBox)(e.Item.Cells[2].Controls[0])).Text);
sqlCommand.Parameters.Add("@Title",((TextBox)(e.Item .Cells[3].Controls[0])).Text);
sqlCommand.Parameters.Add("@Extension",((TextBox)(e.Item.Cells[4].Controls[0])).Text); sqlCommand.ExecuteNonQuery();
sqlConnection1.Close(); DataGrid1.EditItemIndex = -1;
BindDataGrid(); }
}
}
解决方案 »
- linq项目三层???
- 如何将DataSet写回数据库?
- ADODB RECORDSET 图片的问题
- C#2005中怎么加上快捷键
- 最原始的问题,高手帮忙解决一下吧——c#写的控制台怎么样让按回车键程序也不关闭啊,任意键也不关闭,只能关闭按钮关闭,解决马上结贴
- 本机可以编译执行,拷到别的机器上就出错,无法运行。application has generated an exception that cannot be handled
- 用C#调用PowerPoint2013出错
- 怎样获得操作系统的语言版本﹖
- 如何使用CIMV2命名空间呀?
- VS 2019 离线下载地址和正版 Key 注册码,拿走不谢
- 求教:如何与.Net系统dll文件一样包含注释文字?!
- 谁帮我翻译一段E文。。。
Server Error in '/my' Application.
--------------------------------------------------------------------------------第 1 行: '=' 附近有语法错误。
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.SqlClient.SqlException: 第 1 行: '=' 附近有语法错误。Source Error:
Line 91: SqlCommand sqlCommand = new SqlCommand(sqlQuery,sqlConnection1);
Line 92: sqlCommand.Parameters.Add("@EmployeeID",key);
Line 93: SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
Line 94:
Line 95: if(sqlDataReader.Read())
Source File: c:\inetpub\wwwroot\my\webform1.aspx.cs Line: 93 Stack Trace:
[SqlException: 第 1 行: '=' 附近有语法错误。]
System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream) +723
System.Data.SqlClient.SqlCommand.ExecuteReader() +42
my.WebForm1.DataGrid1_SelectedIndexChanged(Object sender, EventArgs e) in c:\inetpub\wwwroot\my\webform1.aspx.cs:93
System.Web.UI.WebControls.BaseDataList.OnSelectedIndexChanged(EventArgs e)
System.Web.UI.WebControls.DataGrid.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.DataGridItem.OnBubbleEvent(Object source, EventArgs e)
System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args)
System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e)
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain() +1277
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573 请一定帮忙!!!!!!!!!!!111