我有两个asp c#页面,第一页使用insert to sql,另一个将更新

时间:2015-04-30 02:55:41

标签: c# asp.net sqlconnection

我有两个asp c#页面,它们链接在一起,它由两个相互连接的表单组成。

示例我已完成第一页所以当我点击下一页(继续到下一页)时,我在第一页中输入的那些值将自动插入到sql数据库中,当我回答第二页时并提交它我需要更新数据库。

问题是第一页的insert语句运行并进入数据库,而第二页的update语句没有进入数据库。

任何想法如何解决这些问题?

来自第一页的

代码

    protected void Button1_Click(object sender, EventArgs e)
    {
        string selectedvalue = RadioButtonList2.SelectedValue.ToString();
        string selectedvalue2 = RadioButtonList4.SelectedValue.ToString();
        string selectedvalue3 = RadioButtonList6.SelectedValue.ToString();
        string selectedvalue4 = RadioButtonList8.SelectedValue.ToString();
        string selectedvalue5 = RadioButtonList10.SelectedValue.ToString();
        string selectedvalue6 = RadioButtonList11.SelectedValue.ToString();
       bool risk;
     SqlCommand cmd = new SqlCommand();
     SqlConnection sqlcon = new SqlConnection("Data Source=DBASE;Initial Catalog=TumorRegistry;User ID=sa");
        try
            {
                if (RadioButtonList1.SelectedItem.Value == "rbfalse")
                {
                    if (bool.TryParse("False" , out risk))
                    {
                        risk = false;
                    }

                }
                if (RadioButtonList1.SelectedItem.Value == "rbtrue")
                {
                    if (bool.TryParse(RadioButtonList1.SelectedItem.Value , out risk))
                    {
                        risk = true;
                    }
                }
                if(RadioButtonList3.SelectedItem.Value=="rb3false")
                {
                    if (bool.TryParse(RadioButtonList3.SelectedItem.Value , out risk))
                    {
                        risk = false;
                    }
                }
                if (RadioButtonList3.SelectedItem.Value == "rb3true")
                {
                    if (bool.TryParse(RadioButtonList3.SelectedItem.Value , out risk))
                    {
                        risk = true;
                    }
                }
                if (RadioButtonList5.SelectedItem.Value == "rb4false")
                {
                    if (bool.TryParse(RadioButtonList5.SelectedItem.Value , out risk))
                    {
                        risk = false;
                    }
                }
                if (RadioButtonList5.SelectedItem.Value == "rb4true")
                {
                    if (bool.TryParse(RadioButtonList5.SelectedItem.Value , out risk))
                    {
                        risk = true;
                    }
                }
                if (RadioButtonList7.SelectedItem.Value == "rbfalse5")
                {
                    if (bool.TryParse(RadioButtonList7.SelectedItem.Value , out risk))
                    {
                        risk = false;
                    }
                }
                if (RadioButtonList7.SelectedItem.Value == "rbtrue5")
                {
                    if (bool.TryParse(RadioButtonList7.SelectedItem.Value , out risk))
                    {
                        risk = true;
                    }
                }
                if (RadioButtonList9.SelectedItem.Value == "rbfalse6")
                {
                    if (bool.TryParse(RadioButtonList9.SelectedItem.Value , out risk))
                    {
                        risk = false;
                    }
                }
                if (RadioButtonList9.SelectedItem.Value == "rbtrue6")
                {
                    if (bool.TryParse(RadioButtonList9.SelectedItem.Value , out risk))
                    {
                        risk = true;
                    }
                }
                if (selectedvalue.ToString() == "rbfalse")
                {

                    selectedvalue = null;
                }
                if (selectedvalue2.ToString() == "rb3false")
                {
                    selectedvalue2 = null;
                }
                if (selectedvalue3.ToString() == "rb4false")
                {
                    selectedvalue3 = null;
                }
                if (selectedvalue4.ToString() == "rbfalse5")
                {
                    selectedvalue4 = null;
                }
                if (selectedvalue5.ToString() == "rbfalse6")
                {
                    selectedvalue6 = null;
                }














        Session["rdl1"] = RadioButtonList1.SelectedIndex;
                Response.Redirect("WebForm1.aspx");
                Session["rdl2"] = RadioButtonList2.Text;
                Response.Redirect("WebForm1.aspx");
                Session["rdl3"] = RadioButtonList3.Text;
                Response.Redirect("WebForm1.aspx");
                Session["rdl4"] = RadioButtonList4.Text;
                Response.Redirect("WebForm1.aspx");
                Session["rdl5"] = RadioButtonList5.Text;
                Response.Redirect("WebForm1.aspx");
                Session["rdl6"] = RadioButtonList6.Text;
                Response.Redirect("WebForm1.aspx");
                Session["rdl7"] = RadioButtonList7.Text;
                Response.Redirect("WebForm1.aspx");
                Session["rdl8"] = RadioButtonList8.Text;
                Response.Redirect("WebForm1.aspx");
                Session["rdl9"] = RadioButtonList9.Text;
                Response.Redirect("WebForm1.aspx");
                Session["rdl10"] = RadioButtonList10.Text;
                Response.Redirect("WebForm1.aspx");
                Session["rdl11"] = RadioButtonList11.Text;
                Response.Redirect("WebForm1.aspx");
               } 

              catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
    } 

第二页代码

                 protected void Button2_Click(object sender, EventArgs e)
    {
        string selectedvalue = " ";
        string selectedvalue2 = " ";
        string selectedvalue3 = " ";
        string selectedvalue4 = " ";
        string selectedvalue5 = " ";
        string selectedvalue6 = " ";
        string rbl1 = " ";
        string rbl2 = " ";
        string rbl3 = " ";
        string rbl4 = " ";
        string rbl5 = " ";
        //bool risk;
        try
        {

            // session gained from previous page 

            if (Session["rdl1"] != null)
            {
                rbl1 = Session["rdl1"].ToString();
            }
            if (Session["rdl2"] != null)
            {
                selectedvalue = Session["rdl2"].ToString();
            }
            if (Session["rdl3"] != null)
            {
                rbl2 = Session["rdl3"].ToString();
            }
            if (Session["rdl4"] != null)
            {
                selectedvalue2 = Session["rdl4"].ToString();
            }
            if (Session["rdl5"] != null)
            {
                rbl3 = Session["rdl5"].ToString();
            }
            if (Session["rdl6"] != null)
            {
                selectedvalue3 = Session["rdl6"].ToString();
            }
            if (Session["rdl7"] != null)
            {
                rbl4 = Session["rdl7"].ToString();
            }
            if (Session["rdl8"] != null)
            {
                selectedvalue4 = Session["rdl8"].ToString();
            }
            if (Session["rdl9"] != null)
            {
                rbl5 = Session["rdl9"].ToString();
            }
            if (Session["rdl10"] != null)
            {
                selectedvalue5 = Session["rdl10"].ToString();
            }
            if (Session["rdl11"] != null)
            {
                selectedvalue6 = Session["rdl11"].ToString();
            }

            SqlConnection sqlcon = new SqlConnection("Data Source=DBASE;Initial Catalog=TumorRegistry;User ID=sa");
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = sqlcon;

            string SqlInsert = "Insert  into tbTRcBase (HPN,ISH, Asthma, DM, OtherCo, HPNTreatment, ISHTreatment,AsthmaTreatment, DMTreatment, OtherCoTreatment,SecondHandSmoke) values ('" + rbl1.ToString() + "','" + rbl2.ToString() + "','" + rbl3.ToString() + "','" + rbl4.ToString() + "','" + rbl5.ToString() + "','" + selectedvalue.ToString() + "','" + selectedvalue2.ToString() + "','" + selectedvalue3.ToString() + "','" + selectedvalue4.ToString() + "','" + selectedvalue5.ToString() + "','" + selectedvalue6.ToString() + "')"; // this line inserts the vlaues of the first page when the button of submit in 2nd page is clicked
            SqlDataAdapter DA = new SqlDataAdapter(SqlInsert, sqlcon);

            DataTable dt = new DataTable();
            DA.Fill(dt);
            if (Radbl3.SelectedItem.Value != null && radbl4.SelectedItem.Value != null && CheckBoxList1.SelectedItem.Value != null && radbl5.SelectedItem != null && CheckBoxList2.SelectedItem.Value != null && radbl1.SelectedItem.Value != null)
            {
                sqlcon = new SqlConnection("Data Source=DBASE;Initial Catalog=TumorRegistry;User ID=sa");


                string SqlUpdate = "Update  tbTRcBase SET Smoker=Radbl3.SelectedItem.Text , StopSmoking = radbl4.SelectedItem.Text , Occupation = CheckBoxList1.SelectedItem.Text , CancerFamilyHistory = radbl5.SelectedItem.Text , FamilyWithCancer=CheckBoxList2.SelectedItem.Text , ParentWithCancer = radbl1.SelectedItem.Text WHERE Smoker=null, StopSmoking = null , Occupation = null, CancerFamilyHistory = null, FamilyWithCancer= null, ParentWithCancer = null "; // you see im having trouble with this line since this is the update the values are not inserted into the database 
                SqlDataAdapter DA1 = new SqlDataAdapter(SqlUpdate, sqlcon);
                DataTable dt1 = new DataTable();
                DA1.Update(dt1);

            } 


        }

             catch (Exception ex)
        {
            Response.Write(ex.Message);
        }

    } 

1 个答案:

答案 0 :(得分:0)

codes from the first page:

       Session["rdl1"] = RadioButtonList1.SelectedIndex;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl2"] = RadioButtonList2.Text;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl3"] = RadioButtonList3.Text;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl4"] = RadioButtonList4.Text;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl5"] = RadioButtonList5.Text;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl6"] = RadioButtonList6.Text;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl7"] = RadioButtonList7.Text;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl8"] = RadioButtonList8.Text;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl9"] = RadioButtonList9.Text;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl10"] = RadioButtonList10.Text;
                    Response.Redirect("WebForm1.aspx");
                    Session["rdl11"] = RadioButtonList11.Text;
                    Response.Redirect("WebForm1.aspx");

This should be 

    **   Session["rdl1"] = RadioButtonList1.SelectedIndex;
              Session["rdl2"] = RadioButtonList2.Text;
             Session["rdl3"] = RadioButtonList3.Text;

                    Session["rdl4"] = RadioButtonList4.Text;

                    Session["rdl5"] = RadioButtonList5.Text;

                    Session["rdl6"] = RadioButtonList6.Text;

                    Session["rdl7"] = RadioButtonList7.Text;

                    Session["rdl8"] = RadioButtonList8.Text;

                    Session["rdl9"] = RadioButtonList9.Text;

                    Session["rdl10"] = RadioButtonList10.Text;

                    Session["rdl11"] = RadioButtonList11.Text;
Response.Redirect("WebForm1.aspx");
           **

As if first redirect happened then it will not save the other data in session.

尝试一下,并知道它是否能解决问题。

相关问题