System.IndexOutOfRangeException:找不到表0

时间:2012-09-27 07:21:50

标签: c# asp.net

我正在尝试在C#中实现button_click事件,这样如果按下该按钮,则gridview将填充查询结果,但我收到上述错误。

这是代码:

public partial class Pages_Managingpayment : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyServer"].ConnectionString);
    protected void Page_Load(object sender, EventArgs e)
    {

        if(IsPostBack)
        {
            Load();
    }
    }
    protected void Load() {
        DataSet ds = new DataSet();
        ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
        GridView1.DataSource = ds;
        GridView1.DataBind();
        int columncount = GridView1.Rows[0].Cells.Count;
        GridView1.Rows[0].Cells.Clear();
        GridView1.Rows[0].Cells.Add(new TableCell());
        GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
        GridView1.Rows[0].Cells[0].Text = "No records on display";
    }
    protected void SearchButton_Click(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            BindEmployeeDetails();
        }
    }
    protected void BindEmployeeDetails()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("Select * from users", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        else
        {
            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No Records Found";
        }
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        BindEmployeeDetails();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string passwords = GridView1.DataKeys[e.RowIndex].Value.ToString();
        TextBox pass = (TextBox)GridView1.Rows[e.RowIndex].FindControl("Password");
        TextBox usernames = (TextBox)GridView1.Rows[e.RowIndex].FindControl("username");
        TextBox usertypes = (TextBox)GridView1.Rows[e.RowIndex].FindControl("usertype");
        con.Open();
        SqlCommand cmd = new SqlCommand("update users set User_Type='" + usertypes.Text + "',Username='" + usernames.Text + "' where password='" + passwords + "'", con);
        cmd.ExecuteNonQuery();
        con.Close();
        //.ForeColor = Color.Green;
        //lblresult.Text = username + " Details Updated successfully";
        GridView1.EditIndex = -1;
        BindEmployeeDetails();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindEmployeeDetails();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //int userid = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values["UserId"].ToString());
        string passwords = GridView1.DataKeys[e.RowIndex].Values["password"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("delete from users where password='" + passwords + "'", con);
        int result = cmd.ExecuteNonQuery();
        con.Close();
        if (result == 1)
        {
            BindEmployeeDetails();
            // lblresult.ForeColor = Color.Red;
            //lblresult.Text = username + " details deleted successfully";
        }
    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
            TextBox usertypes = (TextBox)GridView1.FooterRow.FindControl("usertype");
            TextBox usernames = (TextBox)GridView1.FooterRow.FindControl("username");
            TextBox passwords = (TextBox)GridView1.FooterRow.FindControl("password");
            con.Open();
            SqlCommand cmd =
            new SqlCommand(
            "insert into users values('" + usertypes.Text + "','" +
            usernames.Text + "','" + passwords.Text + "')", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindEmployeeDetails();
                // lblresult.ForeColor = Color.Green;
                //lblresult.Text = txtUsrname.Text + " Details inserted successfully";
            }
            else
            {
                //lblresult.ForeColor = Color.Red;
                //lblresult.Text = txtUsrname.Text + " Details not inserted";
            }
        }
    }

3 个答案:

答案 0 :(得分:1)

我猜这里是代码中的错误:

DataSet ds = new DataSet();
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

ds不会包含表格,因此不包含Table[0]。如果您可以将代码分解为发生错误的几行,那将会很有帮助。

答案 1 :(得分:0)

确保在使用table或Row之前检查索引。我修改了你的Load方法,希望你能修改其他地方来纠正错误。

    protected void Load() {
            DataSet ds = new DataSet();

            if(ds.Tables.Count == 0)
            {
             // Syntax might be wrong. I am trying to add new table if it is missing.
              ds.Table.Add(new Table());
            }

            ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
            GridView1.DataSource = ds;
            GridView1.DataBind();
            int columncount = GridView1.Rows[0].Cells.Count;
            GridView1.Rows[0].Cells.Clear();
            GridView1.Rows[0].Cells.Add(new TableCell());
            GridView1.Rows[0].Cells[0].ColumnSpan = columncount;
            GridView1.Rows[0].Cells[0].Text = "No records on display";
        }

答案 2 :(得分:0)

Load()方法中,您创建了一个新的DataSet并尝试使用代码ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());访问其不存在的表。

尝试创建新的DataTable并将其添加到DataSet,然后再绑定GridView(示例Here)。

此外,当您调用Load()方法时,请将条件if(!IsPostBack)设置为:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        Load();
    }
}