-3

I am trying to implement a button_click event in C# such that if the button is pressed, the gridview is filled with the query results but am getting the above error.

This is the code:

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";
            }
        }
    }
4

3 回答 3

1

我猜这是代码中的错误:

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

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

于 2012-09-27T07:40:27.423 回答
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";
        }
于 2012-09-27T07:42:25.290 回答
0

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

在绑定之前尝试创建一个新的DataTable并将其添加到(此处示例)。DataSetGridView

而且,当您调用该Load()方法时,使条件if(!IsPostBack)如下:

protected void Page_Load(object sender, EventArgs e)
{
    if(!IsPostBack)
    {
        Load();
    }
}
于 2012-10-17T05:03:13.663 回答