2013-06-20 79 views
0

放在Response.Redirect的保存視圖狀態的數據到數據庫錯誤

例如,當我有一個問題,該數據只在第一個密鑰記錄,對於所有其他數據在數據庫沒有記錄。 其次,我使用Response.Redirect的目的是刷新網頁,在將數據插入數據庫後清除數據的任何想法?

友情提醒。謝謝。

protected void Page_Load(object sender, EventArgs e) 
    { 

     string stat = Request.QueryString["stat"]; 
     if (stat == "insert") 
     { 
      StatLabel.Text = "New Record have been Insert"; 
     } 

    } 





    private void BindGrid(int rowcount) 
    { 

     DataTable dt = new DataTable(); 

     DataRow dr; 

     dt.Columns.Add(new System.Data.DataColumn("Test1", typeof(String))); 

     dt.Columns.Add(new System.Data.DataColumn("Test2", typeof(String))); 

     dt.Columns.Add(new System.Data.DataColumn("Test3", typeof(String))); 



     if (ViewState["CurrentData"] != null) 
     { 

      for (int i = 0; i < rowcount + 1; i++) 
      { 

       dt = (DataTable)ViewState["CurrentData"]; 

       if (dt.Rows.Count > 0) 
       { 

        dr = dt.NewRow(); 

        dr[0] = dt.Rows[0][0].ToString(); 



       } 

      } 

      dr = dt.NewRow(); 

      dr[0] = TextBox1.Text; 

      dr[1] = TextBox2.Text; 

      dr[2] = TextBox3.Text; 

      dt.Rows.Add(dr); 



     } 

     else 
     { 

      dr = dt.NewRow(); 

      dr[0] = TextBox1.Text; 

      dr[1] = TextBox2.Text; 

      dr[2] = TextBox3.Text; 



      dt.Rows.Add(dr); 



     } 



     // If ViewState has a data then use the value as the DataSource 

     if (ViewState["CurrentData"] != null) 
     { 

      GridView1.DataSource = (DataTable)ViewState["CurrentData"]; 

      GridView1.DataBind(); 

     } 

     else 
     { 

      // Bind GridView with the initial data assocaited in the DataTable 

      GridView1.DataSource = dt; 

      GridView1.DataBind(); 



     } 

     // Store the DataTable in ViewState to retain the values 

     ViewState["CurrentData"] = dt; 



    } 


    protected void Button1_Click(object sender, EventArgs e) 
    { 
     // Check if the ViewState has a data assoiciated within it. If 
     if (ViewState["CurrentData"] != null) 
     { 
      DataTable dt = (DataTable)ViewState["CurrentData"]; 
      int count = dt.Rows.Count; 
      BindGrid(count); 
     } 
     else 
     { 
      BindGrid(1); 
     } 
     TextBox1.Text = string.Empty; 
     TextBox2.Text = string.Empty; 
     TextBox3.Text = string.Empty; 

     TextBox1.Focus(); 
     TextBox2.Focus(); 
     TextBox3.Focus(); 
    } 



    protected void Button2_Click(object sender, EventArgs e) 
    { 


     foreach (GridViewRow oItem in GridView1.Rows) 
     { 
      string str1 = oItem.Cells[0].Text; 
      string str2 = oItem.Cells[1].Text; 
      string str3 = oItem.Cells[2].Text; 
      insertData(str1, str2, str3); 
     } 
    } 

    public void insertData(string str1,string str2,string str3) 
    { 
     SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString); 
     string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')"; 
     SqlCommand cmd = new SqlCommand(sql, con); 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     cmd.Parameters.Clear(); 
     con.Close(); 
     Response.Redirect("WebForm1.aspx?stat=insert"); 
    } 
    } 
} 
+0

如何將'Page_Load'的代碼添加到'if(!IsPostBack){}'中? – zey

回答

0

當您保存到數據庫時,您在每次插入後立即重定向。因此,沒有任何後續插入被執行。我會將重定向移出循環。也許你可以傳遞查詢字符串中插入的記錄數。

保護無效Button2_Click(對象發件人,EventArgs的) {

foreach (GridViewRow oItem in GridView1.Rows) 
    { 
     string str1 = oItem.Cells[0].Text; 
     string str2 = oItem.Cells[1].Text; 
     string str3 = oItem.Cells[2].Text; 
     insertData(str1, str2, str3); 
    } 
    Response.Redirect("WebForm1.aspx?stat=insert"); 
} 

public void insertData(string str1,string str2,string str3) 
{ 
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString); 
    string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')"; 
    SqlCommand cmd = new SqlCommand(sql, con); 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    cmd.Parameters.Clear(); 
    con.Close(); 

} 
0

我看到你的代碼,我發現

protected void Button2_Click(object sender, EventArgs e) 
{ 


    foreach (GridViewRow oItem in GridView1.Rows) 
    { 
     string str1 = oItem.Cells[0].Text; 
     string str2 = oItem.Cells[1].Text; 
     string str3 = oItem.Cells[2].Text; 
     insertData(str1, str2, str3); 
    } 
} 

public void insertData(string str1,string str2,string str3) 
{ 
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString); 
    string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')"; 
    SqlCommand cmd = new SqlCommand(sql, con); 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    cmd.Parameters.Clear(); 
    con.Close(); 
    Response.Redirect("WebForm1.aspx?stat=insert"); 
} 
在此

Button2_Click在第一時間後,循環播放將重定向頁面,所以現在你的方法將是這樣的

替換此方法

protected void Button2_Click(object sender, EventArgs e) 
{ 


    foreach (GridViewRow oItem in GridView1.Rows) 
    { 
     string str1 = oItem.Cells[0].Text; 
     string str2 = oItem.Cells[1].Text; 
     string str3 = oItem.Cells[2].Text; 
     insertData(str1, str2, str3); 
    } 
    Response.Redirect("WebForm1.aspx?stat=insert"); 
} 

public void insertData(string str1,string str2,string str3) 
{ 
    SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["CIMProRPT01ConnectionString"].ConnectionString); 
    string sql = "insert into test (test1,test2,test3) values ('" + str1 + "','" + str2 + "','" + str3 + "')"; 
    SqlCommand cmd = new SqlCommand(sql, con); 
    con.Open(); 
    cmd.ExecuteNonQuery(); 
    cmd.Parameters.Clear(); 
    con.Close(); 

}