locked
Not sure where I went wrong. RRS feed

  • Question

  • User966545989 posted


    I fixed up my to do exercise and its not working. Can someone point out where so I can think about what I need to do to fix it.

    What its not doing is: its not loading its customers to the DD=dropdown in the binddata/load button method. I am not sure what went wrong.

    public partial class MainForm : System.Web.UI.Page
    {
    /// <summary>
    /// TODO: Look up IsPostBack, find out why this code is using it, what happens if we remove the IF STATEMENT? Explain your thoughts.
    /// </summary>
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindData();
    }
    }
    
    /// <summary>
    /// TODO: Explain why there is a -1? Why is this the same name method as the one below? What is the purpose of doing this?
    /// Answer 1:-1 means that it selects the last ID on the list.
    /// Answer 2: So that it can Target the Method BindData with the -1.
    /// </summary>
    private void BindData()
    {
    BindData(-1);
    }
    
    private void BindData(int iCustID)
    {
    // CHALLENGE TODO: Reference the Web.config connection string
    // Look inside of the Web.Config and see the string there, we want to use that value instead of
    // the below line. The answer is further in the notes or found on Google easily!
    SqlConnection connString = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString);
    connString.Open();
    
    //TODO: Create the spGetCustomer stored procedure
    // NOTE: The stored proc will return all customer if there are no parameters passed
    // NOTE2: The stored proc will return a single row if a customerID is passed as a parameter (assuming the customer exists)
    SqlDataAdapter daCustomers = new SqlDataAdapter("spGetCustomer", connString);
    daCustomers.SelectCommand.CommandType = CommandType.StoredProcedure;
    
    if (iCustID != -1)
    {
    //An actual customer ID has been passed to BindData()
    //The stored proc now returns information about a specific customer
    daCustomers.SelectCommand.Parameters.Add(new SqlParameter("@CustomerID", iCustID));
    DataSet dsCustomers = new DataSet();
    daCustomers.Fill(dsCustomers);
    
    SetEnabled(true);
    FillFields(dsCustomers);
    }
    else
    {
    // If this code is reached, we do not have a parameter for CustomerID, therefore we return all Customers here!
    DataSet dsCustomers = new DataSet();
    daCustomers.Fill(dsCustomers);
    // I use a column called "CustFullName"
    // This column doesn't exist in the table, how can we make it though?
    // Check out the SQL file in the root directory of this RAR file
    ddCustomers.DataTextField = "CustFullName";
    ddCustomers.DataValueField = "CustomerID";
    ddCustomers.DataSource = dsCustomers.Tables[0];
    ddCustomers.DataBind();
    
    ListItem li = new ListItem("-- Please Select --", "-1");
    ddCustomers.Items.Insert(0, li);
    
    SetEnabled(false);
    }
    
    connString.Close();
    }
    
    /// <summary>
    /// TODO: Explain this method, what is it doing, in what order? Why?
    /// This Method First Clears the textboxes and then adds the names of the selected Person's name in the DropDown to the textboxes text with Name city and prov.
    /// </summary>
    /// <param name="ds"></param>
    private void FillFields(DataSet ds)
    {
    ClearFields();
    
    txtFName.Text = ds.Tables[0].Rows[0]["FirstName"].ToString();
    txtLName.Text = ds.Tables[0].Rows[0]["LastName"].ToString();
    txtCity.Text = ds.Tables[0].Rows[0]["City"].ToString();
    txtProv.Text = ds.Tables[0].Rows[0]["Prov"].ToString();
    }
    
    /// <summary>
    /// Clears all the fields on the web form
    /// </summary>
    private void ClearFields()
    {
    txtFName.Text = "";
    txtLName.Text = "";
    txtCity.Text = "";
    txtProv.Text = "";
    }
    
    /// <summary>
    /// TODO: Explain what this event does exactly.
    /// This is Suppose to load the Customer Info into the DropDown
    /// </summary>
    protected void btnLoadCustomer_Click(object sender, EventArgs e)
    {
    BindData(Convert.ToInt16(ddCustomers.SelectedValue));
    }
    
    /// <summary>
    /// TODO: Explain what this method is doing.
    /// This Method enables the textboxes for use as well as the dropdown and the load button.
    /// </summary>
    /// <param name="bEnabled">TODO: Exaplin what does this variable does?</param>
    private void SetEnabled(bool bEnabled)
    {
    txtFName.Enabled = bEnabled;
    txtLName.Enabled = bEnabled;
    txtCity.Enabled = bEnabled;
    txtProv.Enabled = bEnabled;
    
    ddCustomers.Enabled = !bEnabled;
    btnLoadCustomer.Enabled = !bEnabled;
    }
    
    /// <summary>
    /// TODO: Explain the logic behind this method, what changes depending on the button click?
    /// if you click the add button it changes the add to save so when you click the save it saves it to the database and returns the save to add but really seems to be unneeded code if you ask me.
    /// </summary>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
    if (btnAdd.Text == "Add New Customer")
    {
    ClearFields();
    btnAdd.Text = "Save";
    ddCustomers.SelectedIndex = -1;
    SetEnabled(true);
    btnEdit.Visible = false;
    btnCancel.Visible = true;
    }
    else
    {
    SaveNewCustomer();
    btnAdd.Text = "Add New Customer";
    ddCustomers.SelectedIndex = -1;
    SetEnabled(false);
    btnEdit.Visible = true;
    btnCancel.Visible = false;
    }
    }
    
    private void SaveNewCustomer()
    {
    SqlConnection connString = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString);
    string spName = "spAddCustomer";
    connString.Open();
    SqlDataAdapter daExecute = new SqlDataAdapter(spName, connString);
    daExecute.SelectCommand.CommandType = CommandType.StoredProcedure;
    daExecute.SelectCommand.Parameters.Add(new SqlParameter("@FirstName", txtFName.Text));
    daExecute.SelectCommand.Parameters.Add(new SqlParameter("@Last", txtLName.Text));
    daExecute.SelectCommand.Parameters.Add(new SqlParameter("@City", txtCity.Text));
    daExecute.SelectCommand.Parameters.Add(new SqlParameter("@Prov", txtProv.Text));
    //TODO: Add remaining parameters, create stored procedure and finish this method.
    
    
    //Since we have just created a new customer we need to call BindData 
    //to re populate the drop down.
    BindData();
    connString.Close();
    }
    
    /// <summary>
    /// TODO: Explain the logic behind this method, what changes depending on the button click?
    /// This Method saves the new info over the old info after you click it twice. Basically it UPDATES
    /// </summary>
    protected void btnEdit_Click(object sender, EventArgs e)
    {
    if (btnEdit.Text == "Edit Customer")
    {
    btnAdd.Visible = false;
    SetEnabled(true);
    btnEdit.Text = "Save";
    btnCancel.Visible = true;
    }
    else
    {
    SaveExistingCustomer();
    btnEdit.Text = "Edit Customer";
    btnCancel.Visible = false;
    btnAdd.Visible = true;
    btnEdit.Visible = true;
    }
    
    }
    
    private void SaveExistingCustomer()
    {
    SqlConnection connString = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString);
    string spName = "spUPDATE";
    connString.Open();
    SqlDataAdapter daExecute = new SqlDataAdapter(spName, connString);
    daExecute.SelectCommand.CommandType = CommandType.StoredProcedure;
    daExecute.SelectCommand.Parameters.Add(new SqlParameter("@CustomerID", ddCustomers.SelectedValue));
    daExecute.SelectCommand.Parameters.Add(new SqlParameter("@FirstName", txtFName.Text));
    daExecute.SelectCommand.Parameters.Add(new SqlParameter("@Last", txtLName.Text));
    daExecute.SelectCommand.Parameters.Add(new SqlParameter("@City", txtCity.Text));
    daExecute.SelectCommand.Parameters.Add(new SqlParameter("@Prov", txtProv.Text));
    
    BindData();
    connString.Close();
    //TODO: Finish Me
    
    }
    
    /// <summary>
    /// TODO: Explain this method, what is it doing, in what order? Why?
    /// This method cancels both edit and add, resetting them both to there original names on the buttons.
    /// </summary>
    protected void btnCancel_Click(object sender, EventArgs e)
    {
    SetEnabled(false);
    btnAdd.Text = "Add New Customer";
    btnAdd.Visible = true;
    btnEdit.Text = "Edit Customer";
    btnEdit.Visible = true;
    ClearFields();
    btnCancel.Visible = false;
    }

    Wednesday, July 30, 2014 10:53 AM

Answers

  • User-1454326058 posted

    Hi athrun5,

    Base on your code, for the first page load, the iCustID‘s value is -1, so it will execute if code block. In the if code block, you don’t bind any data for DropDownList (ddCustomers), that’s why there isn’t any item in the DropDownList (ddCustomers).

    Please check your detail requirement (How to binding data) and modify your code.

    Thanks

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 31, 2014 2:24 AM

All replies

  • User-1454326058 posted

    Hi athrun5,

    Base on your code, for the first page load, the iCustID‘s value is -1, so it will execute if code block. In the if code block, you don’t bind any data for DropDownList (ddCustomers), that’s why there isn’t any item in the DropDownList (ddCustomers).

    Please check your detail requirement (How to binding data) and modify your code.

    Thanks

    Best Regards

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, July 31, 2014 2:24 AM
  • User753101303 posted

    Hi,

    Also see http://msdn.microsoft.com/en-us/library/y740d9d3.aspx which allows to run line by line allowing to check quite easily which logic error you may have in your code etc...

    Tuesday, August 5, 2014 4:28 PM