Click here to Skip to main content
15,885,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ASP design page:

XML
<asp:GridView ID="GridView2" runat="server" AlternatingRowStyle-BackColor="#C2D69B"
                            HeaderStyle-BackColor="green" AutoGenerateEditButton="True"
                            onrowcancelingedit="GridView2_RowCancelingEdit"
                            onrowediting="GridView2_RowEditing" onrowupdating="GridView2_RowUpdating">
                            <HeaderStyle BackColor="Green" />
                            <AlternatingRowStyle BackColor="#C2D69B" />

                        </asp:GridView>




table:

SQL
SELECT TOP 1000 [BookingRefNo]
      ,[DateIn]
      ,[JobCode]
      ,[RequestedBy]
      ,[TargetDept]
      ,[DescriptionofJob]
      ,[NeedBy]
      ,[JobLocation]
      ,[ContactPerson]
      ,[ReqPriority]
      ,[AssignedPriority]
      ,[STATUS]
  FROM [FIMSONE_P2].[dbo].[TSafetyMaster]



code page:
C#
 protected void Page_Load(object sender, EventArgs e)
   {

      BindGridViewPageLoad();


   }

   private void BindGridViewPageLoad()
   {
       SqlConnection con1 = new SqlConnection(constr);
       con1.Open();
       SqlCommand cmd = new SqlCommand("select * from TSafetyMaster", con1);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       da.Fill(ds);
       GridView2.DataSource = ds.Tables[0];
       GridView2.DataBind();


   }

protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
   {
       GridView2.EditIndex = e.NewEditIndex;
       GridView2.DataBind();
   }

   protected void GridView2_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
   {
       GridView2.EditIndex = -1;
       GridView2.DataBind();
   }

   protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {


       GridViewRow row = (GridViewRow)GridView2.Rows[e.RowIndex];
       int BookingRefNo=int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());
       //Label lblBookingRefNo = (Label)row.FindControl("BookingRefNo");
       TextBox txtAssignedPriority = (TextBox)row.FindControl("AssignedPriority");
       TextBox txtStatus = (TextBox)row.FindControl("Status");
       SqlConnection con1 = new SqlConnection(constr);
       SqlCommand cmd = new SqlCommand("Update TSafetyMaster set AssignedPriority=@AssignedPriority,Status=@Status where BookingRefNo=@BookingRefNo", con1);
       cmd.Parameters.Add("@BookingRefNo", SqlDbType.Int).Value = BookingRefNo;
       cmd.Parameters.Add("@AssignedPriority", SqlDbType.NVarChar).Value = txtAssignedPriority;
       cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = txtStatus;
       cmd.ExecuteNonQuery();
       GridView2.EditIndex=-1;
       GridView2.DataBind();



   }
Posted
Updated 14-May-14 19:58pm
v4
Comments
DamithSL 15-May-14 1:25am    
what is the error? you need to set BookingRefNo parameter and value
safihur Rahuman 15-May-14 1:34am    
txtAssignedpriority,txtstatus its take null value
DamithSL 15-May-14 1:38am    
update the question with full gridview code
safihur Rahuman 15-May-14 1:40am    
ok
safihur Rahuman 15-May-14 1:47am    
hi frd now i am update my code pls check ...

1 solution

you need to set Data Key Names property as BookingRefNo. then only you can get the Data Key value
ASP.NET
<asp:gridview id="GridView2" runat="server" datakeynames="BookingRefNo" xmlns:asp="#unknown"></asp:gridview>

now below line you will have BookingRefNo
C#
int BookingRefNo=int.Parse(GridView2.DataKeys[e.RowIndex].Value.ToString());

when you get values, try like below
C#
var DateIn =GridView2.Rows[e.RowIndex].Cells[1].Text;
var JobCode =GridView2.Rows[e.RowIndex].Cells[2].Text;
var RequestedBy=GridView2.Rows[e.RowIndex].Cells[3].Text;

PageLoad code Updated
C#
protected void Page_Load(object sender, EventArgs e)
   {
      if(!IsPostBack()) { 
          BindGridViewPageLoad();
      }
   }
 
Share this answer
 
v4
Comments
safihur Rahuman 15-May-14 2:11am    
hello frd now i am add datakeyname its work but another two column assidnedpriority,status getting null value.
cmd.Parameters.Add("@AssignedPriority", SqlDbType.NVarChar).Value = txtAssignedPriority;
cmd.Parameters.Add("@Status", SqlDbType.NVarChar).Value = txtStatus;
DamithSL 15-May-14 2:14am    
check my update, try to get the cell value of the cell you want, like var DateIn =GridView2.Rows[e.RowIndex].Cells[1]Value.ToString();
safihur Rahuman 15-May-14 2:20am    
show error in value.tostring();
DamithSL 15-May-14 2:27am    
debug and check the values you get as each cell
safihur Rahuman 15-May-14 2:30am    
var AssignedPriority=GridView2.Rows[e.RowIndex].Cells[10]value.ToString();
var Status = GridView2.Rows[e.RowIndex].Cells[11]value.ToString();

error in var :type or namespace could not be found .....

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900