We help IT Professionals succeed at work.

Updating SQL server int field with gridview checkbox value

489 Views
Last Modified: 2012-08-13
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

Comment
Watch Question

Kevin CrossChief Technology Officer
CERTIFIED EXPERT
Most Valuable Expert 2011

Commented:
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 ZiaConsultant Software Engineer - .NET Architect
CERTIFIED EXPERT

Commented:
Hi,
Use following
myCommand.Parameters.AddWithValue("@po", SqlDbType.Bit).Value = po.Checked

notice that you are using po.Text  

Thanks
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION

Author

Commented:
Thank you.  Had the int field.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.