提示如下错误:索引超出范围。必须为非负值并小于集合大小。
参数名: index private void BindGrid()
{
//string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
//string SqlStr = "SELECT a.*,b.description FROM test01 a,xueli b where a.xueli=b.code and a.id<1000 and a.id>200";
string connStr = "Data Source='localhost';Initial Catalog='test';User Id='sa';Password='123456'";
string SqlStr = "select * from login";
DataSet ds = new DataSet();
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "login");
if (conn.State.ToString() == "Open") conn.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
{
GridViewRow row = GridView1.Rows[e.RowIndex];
//string username = ((TextBox)row.FindControl("username")).Text.ToString();
int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); string connStr = "Data Source='localhost';Initial Catalog='test';User Id='sa';Password='123456'";
string SqlStr = "delete from login where ID="+ID+"";
Response.Write(ID);
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
BindGrid();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
} }
参数名: index private void BindGrid()
{
//string connStr = ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString;
//string SqlStr = "SELECT a.*,b.description FROM test01 a,xueli b where a.xueli=b.code and a.id<1000 and a.id>200";
string connStr = "Data Source='localhost';Initial Catalog='test';User Id='sa';Password='123456'";
string SqlStr = "select * from login";
DataSet ds = new DataSet();
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlDataAdapter da = new SqlDataAdapter(SqlStr, conn);
da.Fill(ds, "login");
if (conn.State.ToString() == "Open") conn.Close();
GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataBind();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
}
}
{
GridViewRow row = GridView1.Rows[e.RowIndex];
//string username = ((TextBox)row.FindControl("username")).Text.ToString();
int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString()); string connStr = "Data Source='localhost';Initial Catalog='test';User Id='sa';Password='123456'";
string SqlStr = "delete from login where ID="+ID+"";
Response.Write(ID);
try
{
SqlConnection conn = new SqlConnection(connStr);
if (conn.State.ToString() == "Closed") conn.Open();
SqlCommand comm = new SqlCommand(SqlStr, conn);
comm.ExecuteNonQuery();
comm.Dispose();
if (conn.State.ToString() == "Open") conn.Close();
GridView1.EditIndex = -1;
BindGrid();
}
catch (Exception ex)
{
Response.Write("数据库错误,错误原因:" + ex.Message);
Response.End();
} }
就这么写??如果没值,不报索引超出范围,报什么错??
dataview = ds!=null&&ds.Tables.Count>0?ds.Tables[0].DefaultView:null;
int ID = int.Parse(GridView1.DataKeys[e.RowIndex].Values[0].ToString());
这一句的错,看属性DataKeysName有直吗?