Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a web form that populates data from the database. The user can also edit the populated data and save it again. Why doesn't the new data populate the textboxes. Instead the old data is still showing?

Here is my populated code:
C#
if (!IsPostBack)
        {
            if (TextBoxINST_ID.Text.Trim().Length > 0)
            {
                SqlConnection con44 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
                con44.Open();

                SqlCommand scmd44 = new SqlCommand("Select FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC, UnderG12, Postb9, Total123b4b, THCAS, FTE40, HC50, FTE4050 from Table55 where User_ID = '" + TextBoxUser_ID.Text + "'", con44);
                SqlDataReader dr44 = scmd44.ExecuteReader();
                if (dr44.Read())
                {
                    TextBox1.Text = dr44["FT_UNDERGR"].ToString();
                    TextBox2.Text = dr44["FT_GRAD"].ToString();
                    TextBox3.Text = dr44["FTE_UNDERG"].ToString();
                    TextBox4.Text = dr44["FTE_GRAD"].ToString();
                    TextBox4.Text = dr44["NON_CREDIT"].ToString();
                    TextBox5.Text = dr44["TOTAL_FTE"].ToString();
                    TextBox6.Text = dr44["FCFTUHC"].ToString();
                    
                }
                con44.Close();
                dr44.Close();
            }
        }
    }


Here is my Save Button code:
C#
protected void ButtonSave_Click(object sender, EventArgs e)
    {
        SqlConnection con7 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
        con7.Open();

        SqlCommand cmd = new SqlCommand("Insert into Table56 (User_ID, FT_UNDERGR, DATE, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC, UnderG12, Postb9, Total123b4b, FTEYR, THCAS, FTE40, HC50, FTE4050) values (@INST_ID, @FT_UNDERGR, @DATE, @FT_GRAD, @FTE_UNDERG, @FTE_GRAD, @NON_CREDIT, @TOTAL_FTE, @FCFTUHC, @FCFTPBHC, @FCPTUHC, @FCPTPBHC, @NCHC, @UnderG12, @Postb9, @Total123b4b, @FTEYR, @THCAS, @FTE40, @HC50, @FTE4050);", con7);

        cmd.CommandType = CommandType.Text;

        cmd.Parameters.AddWithValue("@User_ID", TextBoxUser_ID.Text);
        cmd.Parameters.AddWithValue("@FT_UNDERGR", TextBox1.Text);
        cmd.Parameters.AddWithValue("@FT_GRAD", TextBox2.Text);
        cmd.Parameters.AddWithValue("@FTE_UNDERG", TextBox3.Text);
        cmd.Parameters.AddWithValue("@FTE_GRAD", TextBoxTHGDR.Text);
        cmd.Parameters.AddWithValue("@NON_CREDIT", TextBox4.Text);
        cmd.Parameters.AddWithValue("@TOTAL_FTE", TextBox5.Text);
        cmd.Parameters.AddWithValue("@FCFTUHC", TextBox6.Text);
        cmd.Parameters.AddWithValue("@FCFTPBHC", TextBoxTNFG.Text);
Posted
Updated 30-Oct-14 4:34am
v2
Comments
Richard Deeming 30-Oct-14 11:20am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

Hi,

I guess the problem here is the if clause that checks for page loaded first time or not .

(!IsPostBack)

If the code that populates data/text boxes is inside your page load event , try moving this to a separate routine and call this routine in the button click handler.

Don't check for page post back condition in the routine that populates the data.Let this check be in form load itself.
 
Share this answer
 
v4
Comments
Computer Wiz99 30-Oct-14 10:56am    
Okay, new to this. How and where will I move my code?
Mathew Soji 30-Oct-14 11:04am    
What I mean by moving is create a separate routine ,something like below .
Then call this routine at the end of save button click.

protected void PopulateTextBoxes()
{
if (TextBoxINST_ID.Text.Trim().Length > 0)
{
SqlConnection con44 = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["HotConnectionString"].ConnectionString);
con44.Open();

SqlCommand scmd44 = new SqlCommand("Select FT_UNDERGR, FT_GRAD, FTE_UNDERG, FTE_GRAD, NON_CREDIT, TOTAL_FTE, FCFTUHC, FCFTPBHC, FCPTUHC, FCPTPBHC, NCHC, UnderG12, Postb9, Total123b4b, THCAS, FTE40, HC50, FTE4050 from Table55 where User_ID = '" + TextBoxUser_ID.Text + "'", con44);
SqlDataReader dr44 = scmd44.ExecuteReader();
if (dr44.Read())
{
TextBox1.Text = dr44["FT_UNDERGR"].ToString();
TextBox2.Text = dr44["FT_GRAD"].ToString();
TextBox3.Text = dr44["FTE_UNDERG"].ToString();
TextBox4.Text = dr44["FTE_GRAD"].ToString();
TextBox4.Text = dr44["NON_CREDIT"].ToString();
TextBox5.Text = dr44["TOTAL_FTE"].ToString();
TextBox6.Text = dr44["FCFTUHC"].ToString();

}
con44.Close();
dr44.Close();
}


}
Computer Wiz99 30-Oct-14 11:28am    
Ok. I will try this. Thanks.

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