保存视图状态数据存入数据库错误 [英] Save view state data into database Error

查看:104
本文介绍了保存视图状态数据存入数据库错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题摆在时的Response.Redirect

例如,该数据仅用于第一个关键的记录,对于所有其他数据DB没有记录。
其次,我使用Response.Redirect的是刷新网页的目的,任何想法,以清除插入数据后的数据到数据库?

请告知。谢谢你。

 保护无效的Page_Load(对象发件人,EventArgs的发送)
    {        字符串STAT =的Request.QueryString [STAT];
        如果(STAT ==插入)
        {
            StatLabel.Text =新记录已插入;
        }    }    私人无效BindGrid(INT行数)
    {        DataTable的DT =新的DataTable();        DataRow的博士;        dt.Columns.Add(新System.Data.DataColumn(测试1中的typeof(字符串)));        dt.Columns.Add(新System.Data.DataColumn(Test2的typeof运算(字符串)));        dt.Columns.Add(新System.Data.DataColumn(Test3的typeof运算(字符串)));        如果(的ViewState [CURRENTDATA]!= NULL)
        {            的for(int i = 0; I<行数+ 1;我++)
            {                DT =(数据表)的ViewState [CURRENTDATA];                如果(dt.Rows.Count大于0)
                {                    博士= dt.NewRow();                    博士[0] = dt.Rows [0] [0]的ToString();                }            }            博士= dt.NewRow();            博士[0] = TextBox1.Text;            博士[1] = TextBox2.Text;            博士[2] = TextBox3.Text;            dt.Rows.Add(DR);        }        其他
        {            博士= dt.NewRow();            博士[0] = TextBox1.Text;            博士[1] = TextBox2.Text;            博士[2] = TextBox3.Text;            dt.Rows.Add(DR);        }        //如果ViewState中有一个数据,然后使用该值作为数据源        如果(的ViewState [CURRENTDATA]!= NULL)
        {            GridView1.DataSource =(数据表)的ViewState [CURRENTDATA];            GridView1.DataBind();        }        其他
        {            //绑定的GridView与DataTable中assocaited的初始数据            GridView1.DataSource = DT;            GridView1.DataBind();        }        //存放的DataTable在ViewState中保留值        的ViewState [CURRENTDATA] = DT;    }
    保护无效的button1_Click(对象发件人,EventArgs的发送)
    {
        //检查的ViewState里面出现一个取出保存数据。如果
        如果(的ViewState [CURRENTDATA]!= NULL)
        {
            DataTable的DT =(数据表)的ViewState [CURRENTDATA];
            诠释计数= dt.Rows.Count;
            BindGrid(计数);
        }
        其他
        {
            BindGrid(1);
        }
        TextBox1.Text =的String.Empty;
        TextBox2.Text =的String.Empty;
        TextBox3.Text =的String.Empty;        TextBox1.Focus();
        TextBox2.Focus();
        TextBox3.Focus();
    }    保护无效Button2_Click(对象发件人,EventArgs的发送)
    {
        的foreach(GridViewRow OITEM在GridView1.Rows)
        {
            串STR1 = oItem.Cells [0]。文本;
            字符串STR2 = oItem.Cells [1]。文本;
            字符串STR3 = oItem.Cells [2]。文本;
            insertData(STR1,STR2,STR3);
        }
    }    公共无效insertData(串STR1,字符串STR2,串STR3)
    {
        SqlConnection的CON =新SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[\"CIMProRPT01ConnectionString\"].ConnectionString);
       字符串SQL =插入测试(TEST1,TEST2,TEST3)VALUES('+ STR1 +,+ str2的+,+ STR3 +');
       CMD的SqlCommand =新的SqlCommand(SQL,CON);
       con.Open();
       cmd.ExecuteNonQuery();
       cmd.Parameters.Clear();
       con.Close();
       的Response.Redirect(?WebForm1.aspx的STAT =插入);
    }
  }
}


解决方案

当您保存到数据库中,您每次插入后立即重定向。因此,没有任何后续插入的执行。我会动议重定向圈外。也许你可以通过在查询字符串插入以及记录数。

保护无效Button2_Click(对象发件人,EventArgs的发送)
    {

 的foreach(GridViewRow OITEM在GridView1.Rows)
    {
        串STR1 = oItem.Cells [0]。文本;
        字符串STR2 = oItem.Cells [1]。文本;
        字符串STR3 = oItem.Cells [2]。文本;
        insertData(STR1,STR2,STR3);
    }
    的Response.Redirect(?WebForm1.aspx的STAT =插入);
}公共无效insertData(串STR1,字符串STR2,串STR3)
{
    SqlConnection的CON =新SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[\"CIMProRPT01ConnectionString\"].ConnectionString);
   字符串SQL =插入测试(TEST1,TEST2,TEST3)VALUES('+ STR1 +,+ str2的+,+ STR3 +');
   CMD的SqlCommand =新的SqlCommand(SQL,CON);
   con.Open();
   cmd.ExecuteNonQuery();
   cmd.Parameters.Clear();
   con.Close();}

i have a problem when put in Response.Redirect

Example, the data only record for the first key in, for all the rest data no record in DB. Secondly, the purpose i use Response.Redirect is to refresh the webpage, any idea to clear data after insert data into DB ?

kindly advise. thank you.

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

解决方案

When you are saving into the database, you redirect right after each insert. Hence, none of the subsequent inserts are executed. I would move the redirect out of the loop. Maybe you can pass the number of records inserted in the query string as well.

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

}

这篇关于保存视图状态数据存入数据库错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆