Gridviews

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaveRook
    New Member
    • Jul 2007
    • 147

    Gridviews

    I am now frustrated! :(

    I want to edit a gridview and all I need to do is update a value from the field Title to the Title field in the database.

    So, as you know, when you click on edit you have the options update and cancel and the fields turn into textboxes, but how do I know what the text boxes are called as all I do is specify <asp:boundfie ld paramaters (which doesn't us an ID) > as a paramenter. The problem is, I don't know what the textbox is called! I am also not using a asp:gridview DataSourceID - I am doing this code behind

    My code

    html
    Code:
    <asp:GridView ID="grdViewClients" runat="server" CellPadding="3" CellSpacing="1" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" OnRowEditing="grdViewClients_RowEdit" OnRowUpdating="grdViewClients_RowUpdating" OnRowCancelingEdit="grdViewClients_RowCancelEdit">
        
         <AlternatingRowStyle BackColor="White" />
            <RowStyle BackColor="#DAFCD2" />
            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#4F6F18" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#2461BF" />
    
            <Columns>
                <asp:CommandField HeaderText="Edit" ShowEditButton="True" ShowHeader="True" />
                <asp:BoundField DataField ="Title" HeaderText="Title" />           
            </Columns>
    
          </asp:GridView>
    and code behind (only showing the Updating sub)
    Code:
     protected void grdViewClients_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
    
            string strConn = ConfigurationManager.ConnectionStrings["kestrel"].ConnectionString;
            SqlConnection Conn = new SqlConnection(strConn);
    
            SqlCommand SqlUpdate = new SqlCommand("UPDATE Clients SET Title=@Title WHERE ID = '"+Request.QueryString["id"]+"'", Conn);
    
            SqlUpdate.CommandType = CommandType.Text;
            
            //TextBox txtTitle = (TextBox)grdViewClients.Rows[e.RowIndex].Cells[0].FindControl("Title");
    
            TextBox txtTitle = (TextBox)grdViewClients.Rows[0].Cells[0].FindControl("Title");
            
            
            SqlUpdate.Parameters.Add("@Title", SqlDbType.NVarChar).Value = txtTitle.Text;
    
            if (Conn.State == ConnectionState.Closed)
            {
                Conn.Open();
            }
                SqlUpdate.ExecuteNonQuery();
            
            if (Conn.State == ConnectionState.Open)
            {
                Conn.Close();
            }
        }
    The error is
    Exception Details: System.NullRefe renceException: Object reference not set to an instance of an object.

    Help please
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    #2
    This might be of help

    Comment

    • DaveRook
      New Member
      • Jul 2007
      • 147

      #3
      Hi

      This doesn't answer the question.

      In grid view, we display an item like
      <asp:BoundFie ld DataField ="Title" HeaderText="Tit le" />.

      I have also used
      <asp:CommandFie ld HeaderText="Edi t" ShowEditButton= "True" ShowHeader="Tru e" />

      When rendered in a browser, clicking the edit tag changes the table (which showed the Title field) into textboxes.

      How do I find out what the textboxes names are?

      Dave

      Comment

      • semomaniz
        Recognized Expert New Member
        • Oct 2007
        • 210

        #4
        Code:
          void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
          {
        
            // Iterate through the NewValues collection and HTML encode all 
            // user-provided values before updating the data source.
            foreach (DictionaryEntry entry in e.NewValues)
            {
        
              e.NewValues[entry.Key] = Server.HtmlEncode(entry.Value.ToString());
        
            }
        
          }
        The first example on the link should have given you an idea. Hope this is what you are looking for

        Comment

        • DaveRook
          New Member
          • Jul 2007
          • 147

          #5
          Semomaniz,

          Thank you for being patient with me.

          I understand that the foreach loop looks through each item/field for new values (I assume compared to a cache of the actual database).

          Within the foreach loop you suggested, I have added
          Code:
          Response.Redirect("?err=anything");
          and it doesn't forward.

          Also, with this suggestion is puts the first column content into column 2 (the first cell being the edit column, the second being the Title column). This means when I change the Title field from 'Mr' to 'ABC' and click update, regardless of what I write in the text box it adds the word 'edit' to Title.

          Dave

          Comment

          • DaveRook
            New Member
            • Jul 2007
            • 147

            #6
            ARRRGGGHHHH

            Totally miss-read the MSDN link and page! Sorry. I'm now with it and can start working on it! Sorry

            (if only their was a delete option to protect my pride....)

            Comment

            • semomaniz
              Recognized Expert New Member
              • Oct 2007
              • 210

              #7
              I am glad its working for you

              Comment

              Working...