1

Trying to update the first name of the student there is a textbox "FirstNameTextbox" information was loaded to it from the DB, when I change the information in the textbox and try to write the changes it read only the original data.So if it loaded "Craig" as the first name from the DB, i would edit and put "Chris" in the textbox, what happens is that Craig is written to the DB and not "Chris"

int stuID = getSqlStuID(IDNUMLabel.Text);
SqlConnection conn = new SqlConnection(GetConnectionString());
string sqlUpdateStudent = "Update tblStudent set fname = @fname where stuID = @stuID";
SqlCommand cmd = new SqlCommand(sqlUpdateStudent, conn);

conn.Open();

cmd.Parameters.AddWithValue("@stuID", stuID);
cmd.Parameters.AddWithValue("@fname", FirstNameTextbox.Text);

cmd.ExecuteNonQuery();

ErrorMessage.Text = "Success";


protected void Page_Load(object sender, EventArgs e)
{
    if (Session["User"] != null)
    {
        IDNUMLabel.Text = Session["User"].ToString();
        getStuData(Session["User"].ToString());
    }
    else
    {
        Response.Redirect("../Login/Login.aspx");
    }
}

private void getStuData(string id)
{
    SqlConnection conn = new SqlConnection(GetConnectionString());

    string sql = "Select fname, sname From tblStudent Where idnumber = '" + id + "' ";

    SqlCommand cmd = new SqlCommand(sql, conn);

    try
    {
        conn.Open();

        SqlDataReader selectedRecord = cmd.ExecuteReader();

        cmd.CommandType = CommandType.Text;

        while (selectedRecord.Read())
        {
            FirstNameTextbox.Text = selectedRecord["fname"].ToString();
            LastNameTextbox.Text = selectedRecord["sname"].ToString();
        }

        selectedRecord.Close();
    }
    catch (System.Data.SqlClient.SqlException ex)
    {

        //id = 0;
        //string msg = "Error reading Student ID";
        //msg += ex.Message;
        //throw new Exception(msg);
    }
    catch (Exception ex)
    {

    }
    finally
    {
        conn.Close();
    }
}
4
  • set a breakpoint at cmd.Parameters.AddWithValue("@fname", FirstNameTextbox.Text);. What is the value of stuID? Is there such an entry in your db? Check the value of @fname as well. Commented Mar 15, 2017 at 15:40
  • 1
    Looks like clasic 'load the info in all page loads' issue. Please, edit your question and add the loading into the control code, and explain in what event and conditions is executed Commented Mar 15, 2017 at 15:44
  • Take a look, added it not too long ago Commented Mar 15, 2017 at 16:33
  • @hellogoodnight, stuID would be the primary key in the db Commented Mar 15, 2017 at 16:34

1 Answer 1

0

At what point do you make the actual update? After a button was pressed, after the value was entered on the textbox...? You're missing the method in which the code that handles the update is placed... Maybe this could help: How to display data from database into textbox, and update it

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks Danilo, this is exactly the type of solution i was looking for. #Respect

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.