Link to home
Start Free TrialLog in
Avatar of jaytechnology
jaytechnologyFlag for Afghanistan

asked on

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

Open in new window

Avatar of Kevin Cross
Kevin Cross
Flag of United States of America image

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.
Hi,
Use following
myCommand.Parameters.AddWithValue("@po", SqlDbType.Bit).Value = po.Checked

notice that you are using po.Text  

Thanks
ASKER CERTIFIED SOLUTION
Avatar of prajapati84
prajapati84
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of jaytechnology

ASKER

Thank you.  Had the int field.