Bineesh  Viswanath

Bineesh Viswanath

  • NA
  • 1k
  • 760.3k

GridView Edit Function in ASP.NET

Sep 2 2013 1:10 AM
Sir, I am in a GridView update functio.

As I not implemented textbox  to the columns in GridView, am a bit confused of update function.

My columns in GridView are BoudedFields. Here my GridView:-


<asp:GridView ID="gvCustomers" runat="server" AllowPaging="True"
                    AutoGenerateColumns="False" CellPadding="4" CellSpacing="2"
                    GridLines="None" DataKeyNames="customerId"
                
                    Width="100%" onrowcancelingedit="gvCustomers_RowCancelingEdit"
                    onrowdeleting="gvCustomers_RowDeleting" EnableModelValidation="True"
                    onrowupdating="gvCustomers_RowUpdating"
                    
                    style="font-weight: 700">
                    <RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
                    <Columns>
                        <asp:BoundField DataField="customerId" HeaderText="id" />
                        <asp:BoundField DataField="userName" HeaderText="User Name" />
                        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                        <asp:BoundField DataField="customerCode" HeaderText="Code" />
                       <asp:BoundField DataField="mobileNo" HeaderText="Phone" />
                        <asp:BoundField DataField="joinDate" HeaderText="Join Date" />
                        <asp:CommandField ShowDeleteButton="True" />
                        <asp:CommandField ShowEditButton="true" />
                    </Columns>
                    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#ADCEFD" ForeColor="White" />
                    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                    <HeaderStyle BackColor="#ADCEFD" Font-Bold="True" ForeColor="White"
                        HorizontalAlign="Left" />
                    <EditRowStyle BackColor="#2461BF" />
                    <AlternatingRowStyle BackColor="White" />
                </asp:GridView>


I did the coding in SQL SP and Customer SP class. I need your help in the coding in .aspx.cs file, i.e Gridview_RowUpdating Event.

1) SQL SP:-

ALTER PROCEDURE customerEdit
     @customerId numeric(18,0) ,
@customerCode int,
@firstName varchar(50),
@lastName varchar(50),
@Gender varchar(50),
@Address varchar(50),
@City varchar(50),
@State varchar(50),
@Country varchar(50),
@pinCode bigint,
@mobileNo bigint,
@Email varchar(50),
@joinDate smalldatetime,
@userName varchar(50),
@Password varchar(50)
AS
    update  tbl_Customer set customerCode=@customerCode,firstName=@firstName,lastName=@lastName,Gender=@Gender,
    Address=@Address,City=@City,State=@State,
    Country=@Country,pinCode=@pinCode,mobileNo=@mobileNo,Email=@Email,joinDate=@joinDate,userName=@userName,
    Password=@Password where customerId=@customerId
    RETURN


2) Finally, CustomerSP class:-


public void customerEdit(CInfo InfoCustomer)
        {
            try
            {
                if (sqlCon.State == ConnectionState.Closed)
                {
                    sqlCon.Open();
                }
                SqlCommand c = new SqlCommand("customerEdit", sqlCon);
                c.CommandType = CommandType.StoredProcedure;
                c.Parameters.Add("customerId", SqlDbType.Decimal).Value = InfoCustomer.customerId;
                c.Parameters.Add("customerCode", SqlDbType.Int).Value = InfoCustomer.customerCode;
                c.Parameters.Add("firstName", SqlDbType.VarChar).Value = InfoCustomer.firstName;
                c.Parameters.Add("lastName", SqlDbType.VarChar).Value = InfoCustomer.lastName;
                c.Parameters.Add("Gender", SqlDbType.VarChar).Value = InfoCustomer.Gender;
                c.Parameters.Add("Address", SqlDbType.VarChar).Value = InfoCustomer.Address;
                c.Parameters.Add("City", SqlDbType.VarChar).Value = InfoCustomer.City;
                c.Parameters.Add("State", SqlDbType.VarChar).Value = InfoCustomer.State;
                c.Parameters.Add("Country", SqlDbType.VarChar).Value = InfoCustomer.County;
                c.Parameters.Add("pinCode", SqlDbType.BigInt).Value = InfoCustomer.pinCode;
                c.Parameters.Add("mobileNo", SqlDbType.BigInt).Value = InfoCustomer.mobileNo;
                c.Parameters.Add("Email", SqlDbType.VarChar).Value = InfoCustomer.Email;
                c.Parameters.Add("joinDate", SqlDbType.SmallDateTime).Value = InfoCustomer.joinDate;
                c.Parameters.Add("userName", SqlDbType.VarChar).Value = InfoCustomer.userName;
                c.Parameters.Add("Password", SqlDbType.VarChar).Value = InfoCustomer.Password;
                int inCount = c.ExecuteNonQuery();
                if (inCount > 0)
                {
                    HttpContext.Current.Response.Write("<script>alert('Updated Successfully');</script>");
                }
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                sqlCon.Close();
            }

please provide me the information that how to do updation in this GridView.

Answers (7)