KRayudu V

KRayudu V

  • NA
  • 155
  • 205.6k

ASP.Net MVP(Grid Problem while Updating).can any one tel me that how to write code fore this

Aug 28 2012 4:23 AM
Here is my project Solution
StoredProcedure:
ALTER proc [dbo].[proc_del]
(@UserName varchar(50),@ResultMessage varchar(50) output)
as
begin
delete from reg_plannIT where UserName=@UserName
set @ResultMessage='Sucess'
end

DataObjects:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RegFields
{
  public class Fields
  {
  public string EmployeeName { get; set; }
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string UserName { get; set; }
  public string Password { get; set; }
  public string ConfirmPassword { get; set; }
  public string EmailId { get; set; }
  public string PhoneNo { get; set; }
  public string Address { get; set; }
  }
}

DAOLayer:

 public string UpGridDB(Fields fs)
  {
  try
  {
  string retval = "";
  SqlParameter[] para = new SqlParameter[3];
  para[0] = new SqlParameter("@UserName", fs.UserName);
  para[1] = new SqlParameter("@EmployeeName", fs.EmployeeName);
  para[2] = new SqlParameter("@ResultMessage", SqlDbType.VarChar, 500);
  SqlHelper.ExecuteNonQuery(connection, CommandType.StoredProcedure, "up_proc", para);
  retval = para[2].Value.ToString();
  return retval;
  }
  catch (Exception ex)
  {
  throw ex;
  }
  }
Controllers:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RegFields;
using DataBase;

namespace Reg
{
 public  class GridUpdateGrid
  {
  private Fields fsctrl = new Fields();

  public Fields fieldsCtrl
  {
  get
  {
  return fsctrl;
  }
  set
  {
  value = fsctrl;
  }
  }
  private Data dctrl = new Data();

  public string UpGridCtrl()
  {
  return dctrl.UpdateData(this.fieldsCtrl);
  }
  }
}


DataConnection:
 public static string connection
  {
  get
  {
  return ConfigurationManager.AppSettings.Get("ravi");
  }
DataLayer:
public string UpdateData(Fields fsdata)
  {
  return  bh.UpGridDB(fsdata);
  }
UI

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ExampleToGridAll.aspx.cs"
  Inherits="ExampleToGridAll" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
  <title></title>
</head>
<body>
  <form id="form1" runat="server">
  <div>
  <asp:GridView ID="GridView1" runat="server" Height="265px" Width="365px" AutoGenerateColumns="False"
  ShowFooter="true" DataKeyNames="EmployeeName" CellPadding="3" PageSize="5" CellSpacing="2"
  BorderWidth="1px" AllowPaging="true"
  onpageindexchanging="GridView1_PageIndexChanging"
  onrowcancelingedit="GridView1_RowCancelingEdit"
  onrowediting="GridView1_RowEditing" onrowupdating="GridView1_RowUpdating">
  <Columns>
  <%--  <asp:BoundField DataField="EmployeeName" HeaderText="EmployeeName" SortExpression="EmployeeName" />
  <asp:BoundField DataField="LastName" HeaderText="LastName" SortExpression="LastName" />
  <asp:BoundField DataField="FirstName" HeaderText="FirstName" SortExpression="FirstName" />
  <asp:BoundField DataField="UserName" HeaderText="UserName" SortExpression="UserName" />
  <asp:BoundField DataField="Password" HeaderText="Password" SortExpression="Password" />
  <asp:BoundField DataField="ConfirmPassword" HeaderText="ConfirmPassword" SortExpression="ConfirmPassword" />
  <asp:BoundField DataField="EmailId" HeaderText="EmailId" SortExpression="EmailId"/>
  <asp:BoundField DataField="PhoneNo" HeaderText="PhoneNo" SortExpression="PhoneNo" />
  <asp:BoundField DataField="Address" HeaderText="Address" SortExpression="Address" />--%>

  <%-- <asp:CommandField ShowEditButton="true" />
  <asp:CommandField ShowSelectButton="true" />--%>
  <asp:TemplateField HeaderText="EmployeeName" SortExpression="EmployeeName">
  <EditItemTemplate>
  <asp:TextBox ID="txtEName" runat="server" Width="100px" Text='<%# Bind("EmployeeName") %>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtEName1" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemTemplate>
  <asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeName") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField HeaderText="FirstName" SortExpression="FirstName">
  <EditItemTemplate>
  <asp:TextBox ID="txtFName" runat="server" Width="100px" Text='<%# Bind("FirstName") %>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtFName2" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemTemplate>
  <asp:Label ID="Label2" runat="server" Text='<%# Bind("FirstName") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField HeaderText="LastName" SortExpression="LastName">
  <EditItemTemplate>
  <asp:TextBox ID="txtLName" runat="server" Width="100px" Text='<%# Bind("LastName") %>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtLName3" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemTemplate>
  <asp:Label ID="Label3" runat="server" Text='<%# Bind("LastName") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField HeaderText="UserName" SortExpression="UserName">
  <EditItemTemplate>
  <asp:TextBox ID="txtUName" runat="server" Width="100px" Text='<%# Bind("UserName") %>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtUName4" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemTemplate>
  <asp:Label ID="Label4" runat="server" Text='<%# Bind("UserName") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField HeaderText="Password" SortExpression="Password">
  <EditItemTemplate>
  <asp:TextBox ID="txtpwd" runat="server" Width="100px" Text='<%# Bind("Password") %>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtpwd5" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemTemplate>
  <asp:Label ID="Label5" runat="server" Text='<%# Bind("Password") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField HeaderText="ConfirmPassword" SortExpression="ConfirmPassword">
  <EditItemTemplate>
  <asp:TextBox ID="txtCPWD" runat="server" Width="100px" Text='<%# Bind("ConfirmPassword") %>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtCPwd6" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemTemplate>
  <asp:Label ID="Label6" runat="server" Text='<%# Bind("ConfirmPassword") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
 
  <asp:TemplateField HeaderText="EmailId" SortExpression="EmailId">
  <EditItemTemplate>
  <asp:TextBox ID="txtEmailId" runat="server" Width="100px" Text='<%# Bind("EmailId") %>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtEmailId7" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemTemplate>
  <asp:Label ID="Label7" runat="server" Text='<%# Bind("EmailId") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>
 
  <asp:TemplateField HeaderText="PhoneNo" SortExpression="PhoneNo">
  <EditItemTemplate>
  <asp:TextBox ID="txtPhoneNo" runat="server" Width="100px" Text='<%# Bind("PhoneNo") %>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtPhoneNo8" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemTemplate>
  <asp:Label ID="Label8" runat="server" Text='<%# Bind("PhoneNo") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField HeaderText="Address" SortExpression="Address">
  <EditItemTemplate>
  <asp:TextBox ID="txtEAddress" runat="server" Width="100px" Text='<%# Bind("Address") %>'></asp:TextBox>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:TextBox ID="txtEAddress9" Width="100px" runat="server"></asp:TextBox>
  </FooterTemplate>
  <ItemTemplate>
  <asp:Label ID="Label9" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
  </ItemTemplate>
  </asp:TemplateField>

  <asp:TemplateField HeaderText="Edit" ShowHeader="false">
  <EditItemTemplate>
  <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="true" CommandName="Update" Text="Update"></asp:LinkButton>
  <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="true" CommandName="Cancle" Text="Cancle"></asp:LinkButton>
  </EditItemTemplate>
  <FooterTemplate>
  <asp:LinkButton ID="LinkButton3" runat="server" CausesValidation="false" CommandName="AddNew" Text="Add New"></asp:LinkButton>
  </FooterTemplate>
  <ItemTemplate>
  <asp:LinkButton ID="LinkButton4" runat="server" CausesValidation="false" CommandName="Edit" Text="Edit"></asp:LinkButton>
  </ItemTemplate>
  </asp:TemplateField>
  <asp:CommandField HeaderText="Delete" ShowDeleteButton="true" ShowHeader="true" />
  <asp:CommandField HeaderText="Select" ShowSelectButton="true" ShowHeader="true" />


  </Columns>
  </asp:GridView>
  </div>
  </form>
</body>
</html>

CodeBehind:
 protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  {
  TextBox t1;
  TextBox t2;
  t1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtUName");
  t2 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtEName");
   // I tried in both the ways but im unabel to get,so could any one forrward the code for this
  //t1 = (TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0];
  //t2 = (TextBox)GridView1.Rows[e.RowIndex].Cells[0].Controls[0];
  guc.fieldsCtrl.UserName = t1.Text;
  guc.fieldsCtrl.EmployeeName = t2.Text;

  guc.UpGridCtrl();
  GetData();
  Response.Write("Sucess");
  }