Search code examples
c#asp.netsqlconnection

i have two asp c# pages the first pages uses insert to sql the other will be update


I have two asp c# pages, that are linked together, it is composed of two forms connected to each other.

example i'm done with first page so when i click next page (continue to next page) those values that i have entered in the first page will be automatically be inserted to the sql database, and when i answered the 2nd page and submitted it i need to update the database.

the problem is the insert statement of the first page runs and goes into the database while the update statement of the 2nd page does not enter to the database.

any idea how to fix these?

codes from the first page

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

2nd page codes

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

    } 

Solution

  • 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.
    

    Try it, and lemme know if it resolves the issue or not.