Question

I'm trying to do an update using CommandBuilder. The code works perfectly when the the code fetching the data is retrieved by a button command, but when I fetch the data from the page_load, update fails.

The program simply fetches data from a database then uses the sqlCommandBuilder to make updates on a specific table.

I can't figure out what is going on.

Here is is the Code that fails.

  private Users users;

protected void Page_Load(object sender, EventArgs e)
{
    users = (Users)Session["Users"];

    string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    SqlConnection connection = new SqlConnection(connectionString);
    string selectQuery = "Select * from Candidate where Candidate_ID = " + users.Candidate_ID;
    SqlDataAdapter dataAdapter = new SqlDataAdapter(selectQuery, connection);

    DataSet dataSet = new DataSet();
    dataAdapter.Fill(dataSet, "Candidates");
    ViewState["DATASET"] = dataSet;
    ViewState["SELECT_QUERY"] = selectQuery;

    if (dataSet.Tables["Candidates"].Rows.Count > 0)
    {
        DataRow dataRow = dataSet.Tables["Candidates"].Rows[0];


        txtLastName.Text = dataRow["LastName"].ToString();
        txtCity.Text = dataRow["City"].ToString();
        ddlGender.SelectedValue = dataRow["Gender"].ToString();
        lblStatus.Text = "";
    }
    else
    {
        lblStatus.ForeColor = System.Drawing.Color.Red;
        lblStatus.Text = "No record with ID = " + txtCandidateID.Text;
    }


}


protected void btnUpdate_Click(object sender, EventArgs e)
{
    string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    SqlConnection con = new SqlConnection(connectionString);

    SqlDataAdapter dataAdapter = new SqlDataAdapter();
    dataAdapter.SelectCommand = new SqlCommand((string)ViewState["SELECT_QUERY"], con);
    SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);

    DataSet ds = (DataSet)ViewState["DATASET"];

    DataRow dr = ds.Tables["Candidates"].Rows[0];

    dr["LastName"] = txtLastName.Text;
    dr["Gender"] = ddlGender.SelectedValue;
    dr["City"] = txtCity.Text;

    int rowsUpdated = dataAdapter.Update(ds, "Candidates");
    if (rowsUpdated == 0)
    {
        lblStatus.ForeColor = System.Drawing.Color.Red;
        lblStatus.Text = "No rows updated";
    }
    else
    {
        lblStatus.ForeColor = System.Drawing.Color.Green;
        lblStatus.Text = rowsUpdated.ToString() + " row(s) updated";
    }
}

Here is the code that works perfectly.

 protected void Page_Load(object sender, EventArgs e)
{


}


protected void btnUpdate_Click(object sender, EventArgs e)
{
    string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    SqlConnection con = new SqlConnection(connectionString);

    SqlDataAdapter dataAdapter = new SqlDataAdapter();
    dataAdapter.SelectCommand = new SqlCommand((string)ViewState["SELECT_QUERY"], con);
    SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);

    DataSet ds = (DataSet)ViewState["DATASET"];

    DataRow dr = ds.Tables["Candidates"].Rows[0];

    dr["LastName"] = txtLastName.Text;
    dr["Gender"] = ddlGender.SelectedValue;
    dr["City"] = txtCity.Text;

    int rowsUpdated = dataAdapter.Update(ds, "Candidates");
    if (rowsUpdated == 0)
    {
        lblStatus.ForeColor = System.Drawing.Color.Red;
        lblStatus.Text = "No rows updated";
    }
    else
    {
        lblStatus.ForeColor = System.Drawing.Color.Green;
        lblStatus.Text = rowsUpdated.ToString() + " row(s) updated";
    }
}




protected void btnFetchData_Click(object sender, EventArgs e)
{
    users = (Users)Session["Users"];

    string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    SqlConnection connection = new SqlConnection(connectionString);
    string selectQuery = "Select * from Candidate where Candidate_ID = " + users.Candidate_ID;
    SqlDataAdapter dataAdapter = new SqlDataAdapter(selectQuery, connection);

    DataSet dataSet = new DataSet();
    dataAdapter.Fill(dataSet, "Candidates");
    ViewState["DATASET"] = dataSet;
    ViewState["SELECT_QUERY"] = selectQuery;

    if (dataSet.Tables["Candidates"].Rows.Count > 0)
    {
        DataRow dataRow = dataSet.Tables["Candidates"].Rows[0];


        txtLastName.Text = dataRow["LastName"].ToString();
        txtCity.Text = dataRow["City"].ToString();
        ddlGender.SelectedValue = dataRow["Gender"].ToString();
        lblStatus.Text = "";
    }
    else
    {
        lblStatus.ForeColor = System.Drawing.Color.Red;
        lblStatus.Text = "No record with ID = " + txtCandidateID.Text;
    }
}
Was it helpful?

Solution

Did you miss a check on Page.IsPostBack?

Remember that in ASP.NET when you click a server button the code in the Page_Load is called before the code in the button click event.

Without

protected void Page_Load(object sender, EventArgs e)
{ 
     if(!Page.IsPostBack)
     {
         // load data
     }

}

the code that reloads the DATASET is executed again and, when you receive the control in the button click event, your changes are lost.

OTHER TIPS

fisrt of all make a debug of your source code.

Create breakpoint on each line and check which values are stored in varibales.

Also make it for debug purpouse in a try catch block.

When you can understand where the error has been made you may check of to fix issue.

Which type of error you receive??

Another stuff you assume that user is always in session.Take care that aspnet use a custom method to recycle session and you can have session empty because iis application pool is restarted of has reached is "limit"

is a good practice check if session(value) is null before make the cast

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top