将视图状态数据保存到数据库错误

时间:2013-06-20 02:28:15

标签: asp.net

放入Response.Redirect

时出现问题

例如,数据仅记录第一个键,对于所有其余数据,DB中没有记录。 其次,我使用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");
    }
  }
}

2 个答案:

答案 0 :(得分: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);
    }
    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();

}

答案 1 :(得分: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");
}

在这个on Button2_Click for for循环中,第一次它将重定向页面所以现在你的方法将是这样的

替换此方法

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

}