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:boundfield 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
Expand|Select|Wrap|Line Numbers
- <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>
Expand|Select|Wrap|Line Numbers
- 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();
- }
- }
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Help please