2012-09-27 50 views
-3

我想在C#中實現一個button_click事件,如果按下按鈕,gridview會填充查詢結果,但會出現上述錯誤。System.IndexOutOfRangeException:無法找到表0

這是代碼:

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"; 
      } 
     } 
    } 
+2

顯示UR值編碼.... – DevT

+1

胡亂猜測,你可能會有諸如'dataset.Tables [0] '指定爲'DataSource',但似乎沒有可用的結果,因此您最好檢查查詢並記住發佈出血代碼 – V4Vendetta

+0

您是否檢查返回的數據是否具有數據可數> 0?沒有看到您的代碼很難說... –

回答

1

我猜這裏是在代碼中的錯誤:

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

ds將不包含表,因此不包含Table[0]。如果你可以將你的代碼分解成錯誤發生的幾行,這將會很有幫助。

+0

在Page_Load中發生錯誤。我如何修改它以獲取空的數據源? –

+0

在訪問'Tables [0]'之前檢查'Tables'''Count'屬性。 –

0

請確保您在使用表格或行之前檢查索引。我已經修改了你的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"; 
     } 
+0

我認爲問題在於我的網頁加載。每一個工作正常,如果我刪除頁面上的數據加載..我可以修改它,使它帶來一個空網格? –

+0

您是否嘗試過我粘貼的代碼。似乎在你Page_Load你只調用加載方法。 – Shailesh

+0

很明顯,當我們首次創建一個新的'DataSet'時,可能沒有表, – manas

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(); 
    } 
}