Updating SQL server int field with gridview checkbox value
I am using VS 2010 .net4. I have a grid view on my page that has a check box. Once the user checks it, then submits the data,the check box clears.
Help please.
Thankyou
Dim myCommand As New SqlCommand("Insert into projectsHeader (ProjectName, StatusDate, StartDate, EndDate, PercentComplete, ProjectSponsor, ProjectManager, IRDirector, Status, ProjectOpen) Values (@pn, @sd, @std, @ed, @pc, @ps, @pm, @ird, @st, @po)", Conn) 'find gridview variables Dim pn As TextBox = CType(gvProjects.FooterRow.FindControl("tbProjectName"), TextBox) Dim sd As TextBox = CType(gvProjects.FooterRow.FindControl("tbStatusDate"), TextBox) Dim std As TextBox = CType(gvProjects.FooterRow.FindControl("tbStartDate"), TextBox) Dim ed As TextBox = CType(gvProjects.FooterRow.FindControl("tbEndDate"), TextBox) Dim pc As TextBox = CType(gvProjects.FooterRow.FindControl("tbPercent"), TextBox) Dim ps As DropDownList = CType(gvProjects.FooterRow.FindControl("ddSponsor"), DropDownList) Dim pm As DropDownList = CType(gvProjects.FooterRow.FindControl("ddProjectManager"), DropDownList) Dim ird As DropDownList = CType(gvProjects.FooterRow.FindControl("ddDirector"), DropDownList) Dim st As DropDownList = CType(gvProjects.FooterRow.FindControl("ddStatus"), DropDownList) Dim po As CheckBox = DirectCast(gvProjects.FooterRow.FindControl("cbopen"), CheckBox) 'set footer row for insert myCommand.Parameters.AddWithValue("@pn", SqlDbType.VarChar).Value = pn.Text myCommand.Parameters.AddWithValue("@sd", SqlDbType.Date).Value = sd.Text myCommand.Parameters.AddWithValue("@std", SqlDbType.Date).Value = std.Text myCommand.Parameters.AddWithValue("@ed", SqlDbType.Date).Value = ed.Text myCommand.Parameters.AddWithValue("@pc", SqlDbType.Decimal).Value = pc.Text myCommand.Parameters.AddWithValue("@ps", SqlDbType.VarChar).Value = ps.Text myCommand.Parameters.AddWithValue("@pm", SqlDbType.VarChar).Value = pm.Text myCommand.Parameters.AddWithValue("@ird", SqlDbType.VarChar).Value = ird.Text myCommand.Parameters.AddWithValue("@st", SqlDbType.VarChar).Value = st.Text myCommand.Parameters.AddWithValue("@po", SqlDbType.Bit).Value = po.Text 'open connection Conn.Open() myCommand.ExecuteNonQuery() 'close connection Conn.Close() 'refresh the page Response.Redirect("NewProjectsDataEntry.aspx") End Sub
For a CheckBox, I believe you want the Checked property and not Text. What is likely happening is you are passing a string that doesn't evaluate to a boolean|bit and so is leaving the field as FALSE and as such the text box reverts to UnChecked.
Imran Javed Zia
Hi,
Use following
myCommand.Parameters.AddWithValue("@po", SqlDbType.Bit).Value = po.Checked