菜鸟发问,高手请进(关于datagrid和数据库)
小弟要做毕业论文.从网上拷贝回来这一段代码.
可是delete和update功能都不能实现.说是脚本发生错误,各位达人能帮我看看吗?
......
private void dgShow_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)//不能正常使用
{
if(dgShow.Items.Count==1)//如果该页只有一行,往前删除后往前翻一页.
{
if(dgShow.CurrentPageIndex!=0)
dgShow.CurrentPageIndex = dgShow.CurrentPageIndex-1;
}
string strSql = "delete from BaseInfo where name="+e.Item.Cells[0].Text+"";
ExecuteSql(strSql);
BindData();
}
private void dgShow_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)//不能正常使用
{
string strname = e.Item.Cells[0].Text;//处于非编辑状态
string strteam = ((TextBox)(e.Item.Cells[1].Controls[0])).Text;//处于编辑状态
string strposition =((TextBox)(e.Item.Cells[2].Controls[0])).Text;
string strborn = ((TextBox)(e.Item.Cells[3].Controls[0])).Text;
string strheight =((TextBox)(e.Item.Cells[4].Controls[0])).Text;
string strweight =((TextBox)(e.Item.Cells[5].Controls[0])).Text;
string strfrom =((TextBox)(e.Item.Cells[6].Controls[0])).Text;
string strSql = "update BaseInfo set team='"+strteam+"'";
strSql+=",positon="+strposition+",born='"+strborn+"',height='"+strheight+"' ,weight='"+strweight+"',[from]='"+strfrom+"'where name="+strname+"";
ExecuteSql(strSql);
dgShow.EditItemIndex = -1;
BindData();
}
......
问题点数:20、回复次数:4Top
1 楼leostone()回复于 2006-05-04 15:59:55 得分 0
private void ExecuteSql(string strSql)
{
try
{
string strconn = System.Configuration.ConfigurationSettings.AppSettings["DSN"];//从Web.config中读取
SqlConnection conn =new SqlConnection(strconn);
SqlCommand com = new SqlCommand(strSql,conn);
conn.Open();
com.ExecuteNonQuery();
conn.Close();
}
catch(Exception e)
{
Response.Write("<script language = 'javascript'>alert('"+e.Message+"');</script>") ;
}
}Top
2 楼califord(远方)回复于 2006-05-04 16:00:47 得分 20
string strSql = "delete from BaseInfo where name="+e.Item.Cells[0].Text+"";????
改成
string strSql = "delete from BaseInfo where name='"+e.Item.Cells[0].Text+"'";
试试
下面一样的道理Top
3 楼leostone()回复于 2006-05-04 16:16:28 得分 0
谢谢califord(远方) 大哥....delete功能可以了.
我改成了
string strSql = "update BaseInfo set team='"+strteam+"'";
strSql +=",positon='"+strposition+"',born='"+strborn+"',
height='"+strheight+"' ,weight='"+strweight+"',[from]='"+strfrom+"'where name='"+strname+"'";
update还是不行哦...Top
4 楼leostone()回复于 2006-05-04 17:27:55 得分 0
搞掂了......
string strname = e.Item.Cells[0].Text;//处于非编辑状态
和strSql += ",positon=' "+strposition+ "',born=' "+strborn+ "',
height=' "+strheight+ "' ,weight=' "+strweight+ "',[from]=' "+strfrom+ "'where name=' "+strname+ "' ";
冲突了
哈哈...Top




