System.IndexOutOfRangeException: 无法找到表 0 我用的Datalist 在编辑的时候出现这个错误. 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 第一次加载的时候 Datalis的内容可以显示出来. 可点了dalalist的上按钮进入 edit 进行update delete等操作时就出这个问题了. protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态 public static string x; public static string mySql = "select * from goods order by id asc"; public static DataSet ds; protected static string cb; protected static string cba; WmsClass gl = new WmsClass(); protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindDataList(0);} } private void BindDataList(int currentpage) { pds.AllowPaging = true; pds.PageSize = 10; pds.CurrentPageIndex = currentpage; ds= gl.GetDataSet(mySql); pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中 DataList.DataSource = pds;//绑定Datalist DataList.DataBind(); } protected void DataList_UpdateCommand(object source, DataListCommandEventArgs e) { string c1= ((TextBox)e.Item.FindControl("TB_code")).Text.Trim(); string c2= ((TextBox)e.Item.FindControl("TB_name")).Text.Trim(); string c3= ((TextBox)e.Item.FindControl("TB_price")).Text.Trim(); string c4= ((TextBox)e.Item.FindControl("TB_stocknum")).Text.Trim(); string c5= ((TextBox)e.Item.FindControl("TB_sellnum")).Text.Trim(); string c6= ((TextBox)e.Item.FindControl("TB_place")).Text.Trim(); string c7= ((TextBox)e.Item.FindControl("TB_supplier")).Text.Trim(); string c8= ((TextBox)e.Item.FindControl("TB_class")).Text.Trim(); x = ((Label)e.Item.FindControl("LB_id1")).Text.Trim(); mySql = "update goods set code='" + c1 + "',name='" + c2 + "',price='" + c3 + "',stocknum='" + c4 + "',sellnum='" + c5 + "',place='" + c6 + "',supplier='" + c7 + "',class='" + c8 + "' where id='" + x + "'"; gl.ExecuteSql(mySql); DataList.EditItemIndex = -1; BindDataList(0); } pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中看网上. 说的是 dataset ds 里面没有值.怎么破? 我认为在这一步应该没有取出数据 gl.ExecuteSql(mySql); 你可以进入这个类方法里 WmsClass gl = new WmsClass(); 查看下更新操作的最终SQL语句和更新操作的数据 public class WmsClass { public WmsClass() { } public SqlConnection con; bool myDBConSelfOpen = false; //自动打开连接 private void OpenSql() { if (con == null) { con = new SqlConnection(@"Data Source=GENGHI;Initial Catalog=WMS;Integrated Security=True"); con.Open(); myDBConSelfOpen = true; } } //自动关闭连接 private void CloseSql() { if (myDBConSelfOpen == true) { con.Close(); con.Dispose(); con = null; myDBConSelfOpen = false; } } public SqlDataReader myDataReader(string mySql) { SqlDataReader dr; try { OpenSql(); SqlCommand cmd = new SqlCommand(mySql, con); dr = cmd.ExecuteReader(); } catch (Exception ex) { throw ex; } return dr; } //获取第一行记录 public DataRow GetDataRow(string strQuery) { DataRow myRow = null; try { DataTable myTable = GetDataTable(strQuery); if (myTable.Rows.Count > 0) { myRow = myTable.Rows[0]; } } catch (Exception ex) { throw ex; } return myRow; } //获取多条记录 public DataSet GetDataSet(string strQuery) { //声明数据集 myDataSe DataSet myDataSet = new DataSet(); try { OpenSql(); //声明适配器 myDataAdapter用于连接 DataSet 与数据库 SqlDataAdapter myDataAdapter = new SqlDataAdapter(strQuery, con); //将数据装填到 DataSet中 myDataAdapter.Fill(myDataSet); } catch (Exception ex) { throw ex; } CloseSql(); //返回数据集 myDataSet return myDataSet; } //获取多条记录 public DataTable GetDataTable(string strQuery) { //声明数据集 myDataSe DataTable myTable = new DataTable(); try { myTable = GetDataSet(strQuery).Tables[0]; } catch (Exception ex) { throw ex; } //返回数据集 myDataSet return myTable; } /// <summary> /// 执行操作数据库的命令(增、删、改)含参数 /// </summary> /// <param name="strQuery">操作数据库的SQL语句</param> public bool ExecuteSql(string strQuery) { bool flag = true; //打开数据库 OpenSql(); SqlTransaction myTra = con.BeginTransaction(); //创建操作数据库的命令 SqlCommand myCommand = new SqlCommand(strQuery, con, myTra); try { //执行命令 myCommand.ExecuteNonQuery(); myTra.Commit(); } catch (Exception ex) { flag = false; myTra.Rollback(); throw ex; } CloseSql(); return flag; } } 我已经被搞的伤了. 麻烦你帮我看下好吗? wmsclass的代码已贴到8楼了. 视频解析,播放 C# gridview 从数据库中得到数据后改变时间的输出格式 如何对多线程进行传递参数 实例化的两个疑问 已经实例化了,为什么还会出现:非静态的字段、方法或属性“”要求对象引用~ 自定义控件中的日历控件和页面中的日历控件怎么关联? 找了好几天,依然没找到好的解决方案,请大牛帮忙~ C#制作学生信息管理系统——连接数据库问题求教!各位英雄指点一下吧 多线程操作窗体控件 c#怎样操作音频和视频 TCPClient 连接POP3邮箱的进行验证用户名和密码的时候反应太慢! 求教,checkedListbox控件
第一次加载的时候 Datalis的内容可以显示出来. 可点了dalalist的上按钮进入 edit 进行update delete等操作时就出这个问题了.
protected static PagedDataSource pds = new PagedDataSource();//创建一个分页数据源的对象且一定要声明为静态
public static string x;
public static string mySql = "select * from goods order by id asc";
public static DataSet ds;
protected static string cb;
protected static string cba;
WmsClass gl = new WmsClass();
protected void Page_Load(object sender, EventArgs e)
{ if (!IsPostBack)
{ BindDataList(0);}
}
private void BindDataList(int currentpage)
{
pds.AllowPaging = true;
pds.PageSize = 10;
pds.CurrentPageIndex = currentpage; ds= gl.GetDataSet(mySql);
pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中
DataList.DataSource = pds;//绑定Datalist
DataList.DataBind();
}
protected void DataList_UpdateCommand(object source, DataListCommandEventArgs e)
{
string c1= ((TextBox)e.Item.FindControl("TB_code")).Text.Trim();
string c2= ((TextBox)e.Item.FindControl("TB_name")).Text.Trim();
string c3= ((TextBox)e.Item.FindControl("TB_price")).Text.Trim();
string c4= ((TextBox)e.Item.FindControl("TB_stocknum")).Text.Trim();
string c5= ((TextBox)e.Item.FindControl("TB_sellnum")).Text.Trim();
string c6= ((TextBox)e.Item.FindControl("TB_place")).Text.Trim();
string c7= ((TextBox)e.Item.FindControl("TB_supplier")).Text.Trim();
string c8= ((TextBox)e.Item.FindControl("TB_class")).Text.Trim();
x = ((Label)e.Item.FindControl("LB_id1")).Text.Trim(); mySql = "update goods set code='" + c1 + "',name='" + c2 + "',price='" + c3 + "',stocknum='" + c4 + "',sellnum='" + c5 + "',place='" + c6 + "',supplier='" + c7 + "',class='" + c8 + "' where id='" + x + "'";
gl.ExecuteSql(mySql);
DataList.EditItemIndex = -1;
BindDataList(0);
}
pds.DataSource = ds.Tables[0].DefaultView;//把数据集中的数据放入分页数据源中
看网上. 说的是 dataset ds 里面没有值.怎么破?
gl.ExecuteSql(mySql);
你可以进入这个类方法里 WmsClass gl = new WmsClass(); 查看下更新操作的最终SQL语句和更新操作的数据
public class WmsClass
{
public WmsClass()
{
} public SqlConnection con; bool myDBConSelfOpen = false;
//自动打开连接
private void OpenSql()
{
if (con == null)
{
con = new SqlConnection(@"Data Source=GENGHI;Initial Catalog=WMS;Integrated Security=True");
con.Open();
myDBConSelfOpen = true;
}
}
//自动关闭连接
private void CloseSql()
{
if (myDBConSelfOpen == true)
{
con.Close();
con.Dispose();
con = null; myDBConSelfOpen = false;
}
} public SqlDataReader myDataReader(string mySql)
{
SqlDataReader dr;
try
{
OpenSql();
SqlCommand cmd = new SqlCommand(mySql, con);
dr = cmd.ExecuteReader();
}
catch (Exception ex)
{
throw ex;
} return dr;
} //获取第一行记录
public DataRow GetDataRow(string strQuery)
{
DataRow myRow = null;
try
{
DataTable myTable = GetDataTable(strQuery);
if (myTable.Rows.Count > 0)
{
myRow = myTable.Rows[0];
}
}
catch (Exception ex)
{
throw ex;
}
return myRow;
} //获取多条记录
public DataSet GetDataSet(string strQuery)
{
//声明数据集 myDataSe
DataSet myDataSet = new DataSet(); try
{
OpenSql();
//声明适配器 myDataAdapter用于连接 DataSet 与数据库
SqlDataAdapter myDataAdapter = new SqlDataAdapter(strQuery, con); //将数据装填到 DataSet中
myDataAdapter.Fill(myDataSet);
}
catch (Exception ex)
{
throw ex;
}
CloseSql(); //返回数据集 myDataSet
return myDataSet;
} //获取多条记录
public DataTable GetDataTable(string strQuery)
{
//声明数据集 myDataSe
DataTable myTable = new DataTable(); try
{
myTable = GetDataSet(strQuery).Tables[0];
}
catch (Exception ex)
{
throw ex;
}
//返回数据集 myDataSet
return myTable;
} /// <summary>
/// 执行操作数据库的命令(增、删、改)含参数
/// </summary>
/// <param name="strQuery">操作数据库的SQL语句</param>
public bool ExecuteSql(string strQuery)
{
bool flag = true; //打开数据库
OpenSql();
SqlTransaction myTra = con.BeginTransaction();
//创建操作数据库的命令
SqlCommand myCommand = new SqlCommand(strQuery, con, myTra);
try
{
//执行命令
myCommand.ExecuteNonQuery();
myTra.Commit();
}
catch (Exception ex)
{
flag = false;
myTra.Rollback();
throw ex;
}
CloseSql(); return flag;
} }
我已经被搞的伤了. 麻烦你帮我看下好吗? wmsclass的代码已贴到8楼了.