Updating GridView using a data access object

Discussion in 'ASP.NET / ASP.NET Core' started by aaron, Aug 5, 2009.

Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.
  1. I hope that I can explain what I am trying to do:


    I am using asp.net 2008 with c#.
    I have made a simple page with a few text boxes that a use can fill and submit. It then saves to a SQL Database. From there the user can edit the information. I got the edit to work, meaning that when the user clicks the edit button the fields become editable, but from there I am not sure what to do to update the database with the new information that the user enters. Here is what I have so far.
    This is from my data access class:

    Code:
    public int UpdateCustomer(Customer customer)
    {
    int results = 0;
    
    using (SqlConnection connection = new SqlConnection(this._connectionString))
    {
        using (SqlCommand command = new SqlCommand())
        {
            command.Connection = connection;
            command.CommandType = CommandType.StoredProcedure;
            command.CommandText = "dbo.UpdateCustomer";
            command.Parameters.AddWithValue("@id", customer.Id);
            command.Parameters.AddWithValue("@name", customer.Name);
                command.Parameters.AddWithValue("@phone", customer.PhoneNumber);
            command.Parameters.AddWithValue("@image", customer.Image);
    
            connection.Open();
            results = command.ExecuteNonQuery();
            connection.Close();
        }
    
    }
    return results;
    }
    This is from the code behind on the page:

    Code:
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        MyData data = new MyData(connectionString);
    
        Customer customer = new Customer();
        string id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value).ToString();
        customer.Id = id;
        customer.Name = ;
        customer.PhoneNumber = ;
        customer.Image = ;
        data.UpdateCustomer(customer);
    
        GridView1.EditIndex = -1;
        BindGrid();
    }
    I have left some things out, I am just not sure what I need to do in the code behind to finish this off.



    thanks in advance


    Aaron
     
  2. If you want a zero code solution and your requirements are fairly simple you might be able to use a SqlDataSource and assign an UpdateCommand, SelectCommand etc. because this method does all of the donkey work in an automated way that requires virtually no code behind and absolutely no data access classes.

    If you need something with finer control then you're probably following the right path by implementing your own DAL.
     
  3. Yeah with this I needed a little more control. So I am still looking for help with the Update part of it.
     
  4. I figured it out

    Here is what I did:

    Code:
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
            {
                MyData data = new MyData(connectionString);
                GridViewRow row = GridView1.Rows[e.RowIndex];
                Customer customer = new Customer();
    
                string id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value).ToString();
                TextBox nameTB = ((TextBox)(row.Cells[1].Controls[0]));
                TextBox phoneTB = ((TextBox)(row.Cells[2].Controls[0]));
                TextBox imageTB = ((TextBox)(row.Cells[3].Controls[0]));
    
                customer.Id = id;
                customer.Name = nameTB.Text.ToString();
                customer.PhoneNumber = phoneTB.Text.ToString();
                customer.Image = imageTB.Text.ToString();
                data.UpdateCustomer(customer);
    
                GridView1.EditIndex = -1;
                BindGrid();
            }
     
Thread Status:
Threads that have been inactive for 5 years or longer are closed to further replies. Please start a new thread.

Share This Page