Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET3.5
I m implementing a gridview on a web page.here is its code
 
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="false"
           AllowPaging="True" Height="568px" PageSize="25"
           onrowupdating="GridView2_RowUpdating" AutoGenerateEditButton="True"
           onrowediting="GridView2_RowEditing">
       <Columns>
       <asp:TemplateField HeaderText="Name">
       <ItemTemplate>
           <asp:TextBox ID="TextBox2" runat="server" Text='<%#Eval("cstnam") %>' ReadOnly="true"></asp:TextBox>
       </ItemTemplate>
       </asp:TemplateField>
 
        <asp:TemplateField HeaderText="Date">
            <ItemTemplate >
                <asp:TextBox ID="TextBox13" runat="server" Text='<%#Eval("compdat") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>
 
          <asp:TemplateField HeaderText="LogBy">
            <ItemTemplate>
                <asp:TextBox ID="TextBox14" runat="server" Text='<%#Eval("complogby") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>
 
          <asp:TemplateField HeaderText="AttendBy">
            <ItemTemplate>
                <asp:TextBox ID="TextBox15" runat="server" Text='<%#Eval("compatdby") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>
 
          <asp:TemplateField HeaderText="Status">
            <ItemTemplate>
                <asp:TextBox ID="TextBox16" runat="server" Text='<%#Eval("compsts") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>
 
         <asp:TemplateField HeaderText="AttendDate">
            <ItemTemplate>
                <asp:TextBox ID="TextBox17" runat="server" Text='<%#Eval("compatddat") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>
 

         <asp:TemplateField HeaderText="Ticket">
            <ItemTemplate>
                <asp:TextBox ID="TextBox18" runat="server" Text='<%#Eval("ticket") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>
 

         <asp:TemplateField HeaderText="RepReceived">
            <ItemTemplate>
                <asp:TextBox ID="TextBox19" runat="server" Text='<%#Eval("rptRecieved") %>'></asp:TextBox>
            </ItemTemplate>
 
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>
 

         <asp:TemplateField HeaderText="ReceivedBy">
            <ItemTemplate>
                <asp:TextBox ID="TextBox20" runat="server" Text='<%#Eval("rptRecievedBy") %>' ReadOnly="true" ></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>
 

         <asp:TemplateField HeaderText="Recieveddate">
            <ItemTemplate>
                <asp:TextBox ID="TextBox10" runat="server" Text='<%#Eval("rptRecievedDate") %>' ReadOnly="true"></asp:TextBox>
            </ItemTemplate>
            <ItemStyle Width="40px" Font-Bold="true" />
         </asp:TemplateField>
 
         <%--<asp:TemplateField HeaderText="">
            <EditItemTemplate>
               &nbsp;&nbsp;
               <asp:TextBox ID="txtrptreciev" runat="server" Text='<%#Eval("rptRecieved") %>'></asp:TextBox>
                               &nbsp;
                <asp:LinkButton ID="lnkupdt" runat="server" CommandName="Update" Visible="true"
                   Text='<%#Eval("rptRecieved") %>'>LinkButton</asp:LinkButton>
               &nbsp;
           </EditItemTemplate>
         </asp:TemplateField>--%>
         <asp:TemplateField HeaderText="">
         <EditItemTemplate>
             <asp:TextBox ID="txtrptRecieved" Text="" runat="server"></asp:TextBox>
         </EditItemTemplate>
         </asp:TemplateField>
         <asp:CommandField  HeaderText="Update" ShowEditButton="true"/>
       </Columns>
       </asp:GridView>
 

Now i want to update a single column in this gridview whose code is as follow
 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        
        GridViewRow myRow = GridView2.Rows[e.RowIndex];
 
        TextBox rptRecieved = (TextBox)myRow.FindControl("txtrptreciev");
 
        TextBox cstnam = (TextBox)myRow.FindControl("cstnam");
 
        SqlDataAdapter adp = new SqlDataAdapter("update tbcomp set rptRecieved=" + rptRecieved + " where cstnam=" + cstnam + "", con);
 
        DataSet ds = new DataSet();
 
        adp.Fill(ds);
 
        GridView2.EditIndex = -1;
    }
 
    protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView2.EditIndex = e.NewEditIndex;
    }
but its nt updating the column.Please tell me what to do
 
thanks in advance
Posted 25-Jun-12 21:09pm
Edited 25-Jun-12 21:11pm
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

SqlDataAdapter adp = new SqlDataAdapter("update tbcomp set rptRecieved=" + rptRecieved + " where cstnam=" + cstnam + "", con);
 
Probably you should wrap the string value in SQL query by ' sign. so your query should be as below to have it properly working.
 
SqlDataAdapter adp = new SqlDataAdapter("update tbcomp set rptRecieved='" + rptRecieved + "' where cstnam='" + cstnam + "'", con);
 
Thanks,
Hiren
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Hi,
 
Where you are re-binding the GRID...???
 
in Row_Updating event will fire when an Update Command is received, but it will not update by itself. you must bind the grid again.
 
Just update the datasource used of the grid, or Get the new datasource from database and bind again.
 
Happy Coding.
  Permalink  
Comments
abhilasha mourya at 26-Jun-12 3:57am
   
i m binding it on button's click.Will you please elaborate how to bind it again
abhilasha mourya at 26-Jun-12 4:07am
   
hey
 
i got what u were saying but still its not updating the value
Karthik Harve at 26-Jun-12 4:21am
   
can you show your code for updating the datasource of the grid..??
abhilasha mourya at 26-Jun-12 4:26am
   
sory
I m not updating the datasource. i m just giving value to datasource.
anyways here is code.Please suggest any solution
 
GridViewRow myRow = GridView2.Rows[e.RowIndex];
 
TextBox txtrptRecieved = (TextBox)myRow.FindControl("txtrptreciev");
 
TextBox complogby = (TextBox)myRow.FindControl("TextBox2");
 
SqlDataAdapter adp = new SqlDataAdapter("update tbcomp set rptRecieved='" + txtrptRecieved + "' where complogby='" + complogby + "'", con);
 
DataSet ds = new DataSet();
 
adp.Fill(ds);
 
GridView2.EditIndex = -1;
GridView2.DataSource=ds.Tables[0];
GridView2.DataBind();

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 480
1 Gihan Liyanage 225
2 OriginalGriff 174
3 Bhavik_Patel 170
4 PhilLenoir 155
0 Sergey Alexandrovich Kryukov 8,653
1 OriginalGriff 7,132
2 CPallini 2,598
3 Richard MacCutchan 2,060
4 Abhinav S 1,808


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 26 Jun 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100