C# System.IndexOutOfRangeException:找不到表0

C# System.IndexOutOfRangeException:找不到表0,c#,asp.net,C#,Asp.net,我试图在C语言中实现一个按钮点击事件,这样如果按下按钮,gridview就会被查询结果填充,但会得到上面的错误 代码如下: public partial class Pages_Managingpayment : System.Web.UI.Page { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyServer"].ConnectionString); protec

我试图在C语言中实现一个按钮点击事件,这样如果按下按钮,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";
            }
        }
    }
DataSet ds = new DataSet();
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

我猜代码中有一个错误:

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";
            }
        }
    }
DataSet ds = new DataSet();
ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());

ds不包含表,因此不包含表[0]。不过,如果您可以将代码分解为出现错误的几行,这将非常有用。

请确保在使用表或行之前检查索引。我已经修改了你的加载方法,希望你可以修改其他地方来纠正错误

    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";
        }
在Load方法中,创建一个新的数据集,并尝试使用代码ds.Tables[0].Rows.Addds.Tables[0].NewRow;访问它的不存在的表

尝试创建一个新的DataTable并将其添加到DataSet,然后再将其绑定到GridView示例

而且,当您调用Load方法时,如果!iPostback如中所示:


您可能有类似dataset.Tables[0]的内容被指定为DataSource,但似乎没有可用的结果,因此您最好检查查询并记住发布代码。您是否检查返回的数据是否具有datatable count>0?很难说没有看到你的代码…我已经更新并显示了代码…你可以查看它。我想问题是我的页面负载。如果我删除页面加载上的数据,每一个都可以正常工作。我如何修改它,使其产生一个空网格?您是否尝试过我粘贴的代码。在您的页面上,似乎您正在调用only Load方法。很明显,当我们第一次创建新的数据集时,可能没有表。错误发生在页面上。如何修改它以获取空数据源?请在访问表[0]之前检查表的Count属性。