Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why data from database is not fetching initially to update new value as per desire.
I am using session value to fetch from database as per given below.
my cs code is given below


C#
if (Page.IsValid == true)
        {
            string sc1 = "";

            if (rbStudent.Checked == true)
            {
                sc1 = "Student";
            }
            if (rbPFaculty.Checked == true)
            {
                sc1 = "Private Faculty";
            }
            if (rbSFaculty.Checked == true)
            {
                sc1 = "School Faculty";
            }


            SqlConnection CON = new SqlConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["tuitionConnectionString"].ConnectionString);
            SqlTransaction tr = null;
            CON.Open();
            tr = CON.BeginTransaction();
            ds = new DataSet();
            string sql = "select *  from registration";
            da = new SqlDataAdapter(sql, CON);
            da.Fill(ds, "registration");
            //string sa = txtSess.Text;
            //txtFname.Text = sa;
            if (Session["name"] != null)
            {
                string sa = Session["name"].ToString();

                int r = 0;
                DataRow[] rs;
                rs = ds.Tables["registration"].Select("email='" + sa + "'");
                r = rs.Length;
                if (r > 0)
                {
                    //FETCH DATA FROM DATABASE
                    DataRow dr;
                    dr = rs[0];
                    txtFname.Text = dr["fname"].ToString();
                    txtLname.Text = dr["lname"].ToString();
                    txtDob.Text = dr["dob"].ToString();
                    txtCno.Text = dr["cont"].ToString();
                    sc1 = dr["rtype"].ToString();
                    ddClass.SelectedItem.Text = dr["tclass"].ToString();
                    txtSchool.Text = dr["school"].ToString();
                    ddBoard.SelectedItem.Text = dr["board"].ToString();
                    ddCity.SelectedItem.Text = dr["city"].ToString();
                    txtPCode.Text = dr["pcode"].ToString();
                    ddState.SelectedItem.Text = dr["state"].ToString();

                    try
                    {

                        //UPDATE RECORDS in DataBase

                        com = CON.CreateCommand();
                        com.CommandText = "UPDATE registration SET fname='" + txtFname.Text + "',lname='" + txtLname.Text + "',dob='" + txtDob.Text + "',cont='" + txtCno.Text + "',rtype='" + sc1.ToString() + "',tclass='" + ddClass.SelectedItem.Text + "',school='" + txtSchool.Text + "',board='" + ddBoard.SelectedItem.Text + "',city='" + ddCity.SelectedItem.Text + "',pcode='" + txtPCode.Text + "',state='" + ddState.SelectedItem.Text + "' WHERE email='" + txtSess.Text + "'";
                        try
                        {
                            com.ExecuteNonQuery();
                            System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
                            lbl1.ForeColor = System.Drawing.Color.Yellow;
                            lbl1.BackColor = System.Drawing.Color.Blue;
                            lbl1.Text = "Your record UPDATED sucessfully";
                            ph1.Controls.Add(lbl1);
                        }
                        catch (Exception ex)
                        {
                            Response.Write(ex.Message);
                        }

                         }
                    catch (Exception ex)
                    {

                        Response.Write("" + ex);
                        tr.Rollback();
                    }

                    finally
                    {
                        CON.Close();
                    }
                }
            }
        }
Posted
Updated 20-Dec-11 20:20pm
v2
Comments
Sunasara Imdadhusen 21-Dec-11 2:24am    
added <PRE> tag for readability
Al Moje 21-Dec-11 2:31am    
Hi, use break point to debug. Check if session is not null... because session expires, and if expires value becomes null...
Janardan Pandey 21-Dec-11 2:48am    
not clear

1 solution

I'm just guessing, but do you get an exception on the first run? I'm betting you do.
Page.IsValid is only valid when the validation routine has been called first - if you try to use it before that, you will get an error and your execution will stop. So your database will not be loaded. If you are ignoring errors with a try-catch we can't see, then that could be your problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900