protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string strupdate = "";
strupdate += "学号 ='"+((TextBox)(e.Item.Cells[1].Controls[0])).Text+"'";
strupdate += ",姓名 ='"+((TextBox)(e.Item.Cells[2].Controls[0])).Text+"'";
strupdate += ",语文 ='"+((TextBox)(e.Item.Cells[3].Controls[0])).Text+"'";
strupdate += ",数学 ='"+((TextBox)(e.Item.Cells[4].Controls[0])).Text+"'";
strupdate += ",英语 ='"+((TextBox)(e.Item.Cells[5].Controls[0])).Text+"'";
strupdate += ",政治 ='"+((TextBox)(e.Item.Cells[6].Controls[0])).Text+"'";
string updata = "UPDATE grades SET " + strupdate + " where 学号='" +((TextBox)e.Item.Cells[1].Controls[0]).Text+"'";
SqlCommand Mycommand = new SqlCommand(updata,myConnection);
Mycommand.Connection.Open();
try
{
Mycommand.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
}
catch
{
Response.Write("未能更新字段,请确保正确填了字段");
}
BindGrid();着里是绑定数据
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindGrid();
}小弟不才,刚接触着方面的东西~~~希望各位帮我看哈~~~
页面运行时数据能够正常显示,但更新的时候就会出错~~
显示“ConnectionString 属性尚未初始化。” 指向这句Mycommand.Connection.Open();
{
if (!IsPostBack)
{
BindGrid();
}
}
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = (int)e.Item.ItemIndex;
BindGrid();
}
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
string strupdate = "";
strupdate += "学号 ='"+((TextBox)(e.Item.Cells[1].Controls[0])).Text+"'";
strupdate += ",姓名 ='"+((TextBox)(e.Item.Cells[2].Controls[0])).Text+"'";
strupdate += ",语文 ='"+((TextBox)(e.Item.Cells[3].Controls[0])).Text+"'";
strupdate += ",数学 ='"+((TextBox)(e.Item.Cells[4].Controls[0])).Text+"'";
strupdate += ",英语 ='"+((TextBox)(e.Item.Cells[5].Controls[0])).Text+"'";
strupdate += ",政治 ='"+((TextBox)(e.Item.Cells[6].Controls[0])).Text+"'";
string updata = "UPDATE grades SET " + strupdate + " where 学号='" +((TextBox)e.Item.Cells[1].Controls[0]).Text+"'";
SqlCommand Mycommand = new SqlCommand(updata,myConnection);
Mycommand.Connection.Open();
try
{
Mycommand.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
}
catch
{
Response.Write("未能更新字段,请确保正确填了字段");
}
BindGrid();着里是绑定数据
}
protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
{
DataGrid1.CurrentPageIndex = e.NewPageIndex;
BindGrid();
}
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
BindGrid();
}小弟不才,刚接触着方面的东西~~~希望各位帮我看哈~~~
页面运行时数据能够正常显示,但更新的时候就会出错~~
显示“ConnectionString 属性尚未初始化。” 指向这句Mycommand.Connection.Open();
解决方案 »
- datalist 编辑 时出现错误!
- 请问大家vista下开发asp.net的话visual studio和sql到底要多少的版本?
- 如何後臺指定GridView的ReadOnly屬性???在綫等待..............
- popupwin消息控件
- MSDN上关于三层结构里的自定义实体的问题
- .NET下GDI+的一些常用应用(水印,文字,圆角处理)
- IHttpHandler问题,高分求教,各位高手、大大请进!
- 关于 未将对象引用设置到对象的实例----高手指点
- .net 调用存储过程返回多个值
- 求救,太奇怪了
- 我这样从数据数中取出数据,可是不会传递数据值,请大家帮忙看看,谢谢
- 求助asp.net利用dw排板的问题
应该要有server=.;database=数据库名;uid=sa;pwd=密码;之类的连接字符串。
SqlCommand cmd = new SqlCommand(updata, myConnectionn);
try
{
conn.Open();
cmd.ExcuteNonQuery();
DataGrid1.EditItemIndex = -1;}
catch()
{}
public partial class _Default : System.Web.UI.Page
{
SqlConnection myConnection = new SqlConnection();
private void BindGrid()
{
string strconn = "server=localhost;uid=sa;pwd=19870109;database=students";
myConnection.ConnectionString = strconn;
string strsql = "select 学号,姓名,语文,数学,英语,政治 from grades";
SqlDataAdapter da = new SqlDataAdapter(strsql,myConnection);
DataSet ds = new DataSet();
da.Fill(ds,"scores");
ds.Tables["scores"].DefaultView.Sort="语文";
DataGrid1.DataSource = ds.Tables["scores"].DefaultView;
DataGrid1.DataBind();
只是在BindGrid() 给myConnection 定义了 把这个拿到外面。public partial class _Default : System.Web.UI.Page
{
SqlConnection myConnection = new SqlConnection();
string strconn = "server=localhost;uid=sa;pwd=19870109;database=students";
myConnection.ConnectionString = strconn;
private void BindGrid()
{
...
....
你在更新的时候没有CONNECTION对象必须指定Mycommand的CONNECTION STRING。。然后在打开连接。
你把SqlConnection myConnection = new SqlConnection();改为
SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;pwd=19870109;database=students"),后面的一些语句省了,试一下
SqlConnection myConnection = new SqlConnection("server=localhost;uid=sa;pwd=19870109;database=students"),我就这样改了下~~再次感谢~~