[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

Inserting a checkbox value into a SQL table via ASP.NET form.

Posted on 2006-11-01
5
Medium Priority
?
456 Views
Last Modified: 2008-01-09
Hello experts,

    Does anyone know of a way to store the value of a checkbox on an ASP page in a SQL table as either a true or false value? I know how to retrieve the info from SQL, but not insert it. Currently the method I am using sets the value to true regardless of what is checked on the form.

Thanks.

Here is the code I am using.

Imports System.Data.SqlClient
Imports GISTableAdapters
Imports System.Net.Mail

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        txtDateIn.Text = Today
        txtID.Text = User.Identity.Name


    End Sub

 Protected Sub ddlRush_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlRush.SelectedIndexChanged
        If ddlRush.SelectedIndex = 2 Then
            txtDateBy.Text = Today.AddDays(1)
        Else
            txtDateBy.Text = Today.AddDays(4)
        End If
    End Sub

    Protected Sub cmdSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSubmit.Click
        Dim NewCase As New tblRequests1TableAdapter
        Dim requestno As Integer
        Dim m As New MailMessage

        If txtDateBy.Text = "" Then
            lblErrMsg.Text = "This is a required field in the form of mm/dd/yyyy."
            lblErrMsg.Visible = True
            lblError1.Visible = True
            txtDateBy.Focus()
            Exit Sub
        Else
            lblError1.Visible = False
            lblErrMsg.Visible = False
        End If

        'Check to make sure there is a project name associated with the field less
        'than 75 characters
        If txtProjName.Text = "" Then
            lblErrMsg.Visible = True
            lblErrMsg.Text = "This is a required field."
            lblError2.Visible = True
            txtProjName.Focus()
            Exit Sub
        Else
            lblError2.Visible = False
            lblErrMsg.Visible = False
        End If

        'Check to make sure if there are hours assigned to the request
        If txtHours.Text = "" Then
            lblErrMsg.Visible = True
            lblErrMsg.Text = "You must assign hours for this request."
            txtHours.Focus()
        Else
            lblErrMsg.Visible = False
        End If

        'check to see the values of the # of copies and correct so value isnt Null
        If txtcpySite.Text = "" Then
            txtcpySite.Text = "0"
        End If

        If txtcpySoils.Text = "" Then
            txtcpySoils.Text = "0"
        End If

        If txtcpyCNDDB.Text = "" Then
            txtcpyCNDDB.Text = "0"
        End If

        If txtcpyWet.Text = "" Then
            txtcpyWet.Text = "0"
        End If

        If txtcpyPropMap.Text = "" Then
            txtcpyPropMap.Text = "0"
        End If

        If txtcpyBioConst.Text = "" Then
            txtcpyBioConst.Text = "0"
        End If

        If txtcpyBMP.Text = "" Then
            txtcpyBMP.Text = "0"
        End If

        If txtcpyFlight.Text = "" Then
            txtcpyFlight.Text = "0"
        End If

        If txtcpyHistoric.Text = "" Then
            txtcpyHistoric.Text = "0"
        End If

                requestno = Convert.ToInt32(NewCase.InsertNewRequest(txtDateIn.Text, txtDateBy.Text, _
        ddlRush.SelectedValue, ddlOffice.SelectedValue, txtProjName.Text, txtCty.Text, _
        txtDeltek.Text, txtHours.Text, ddlPM.SelectedIndex, ddlPoC.SelectedIndex, _
        ddlReqBy.SelectedIndex, txtID.Text, "New", ddlBioRes.SelectedValue.ToString, _
        ddlBioConst.SelectedValue.ToString, ddlWetlnd.SelectedValue.ToString, _
        ddlArborist.SelectedValue.ToString, ddlMonitoring.SelectedValue.ToString, _
        ddlOther.SelectedValue.ToString, ddlPCN.SelectedValue.ToString, _
        ddlPublic.SelectedValue.ToString, ddlSec7.SelectedValue.ToString, _
        ddlOnsiteAA.SelectedValue.ToString, ddlOffsiteAA.SelectedValue.ToString, _
        ddlPMP.SelectedValue.ToString, ddlCEQA.SelectedValue.ToString, _
        ddlSWPPP.SelectedValue.ToString, ddlWQC.SelectedValue.ToString, _
        ddlSAA.SelectedValue.ToString, txtcpySite.Text, txtcpySoils.Text, txtcpyCNDDB.Text, _
        txtcpyWet.Text, txtcpyPropMap.Text, txtcpyBioConst.Text, txtcpyBMP.Text, _
        txtcpyFlight.Text, txtcpyHistoric.Text, ddlprnSite.SelectedValue.ToString, _
        ddlprnSoils.SelectedValue.ToString, ddlprnCNDDB.SelectedValue.ToString, _
        ddlprnWetlnd.SelectedValue.ToString, ddlprnPropMap.SelectedValue.ToString, _
        ddlprnBioConst.SelectedValue.ToString, ddlprnBMP.SelectedValue.ToString, _
        ddlprnFlight.SelectedValue.ToString, ddlprnHistoric.SelectedValue.ToString, _
        ddltypeSite.SelectedValue.ToString, ddltypeSoils.SelectedValue.ToString, _
        ddltypeCNDDB.SelectedValue.ToString, ddltypeWetlnd.SelectedValue.ToString, _
        ddltypePropMap.SelectedValue.ToString, ddltypeBioConst.SelectedValue.ToString, _
        ddltypeBMP.SelectedValue.ToString, ddltypeFlight.SelectedValue.ToString, _
        ddltypeHistoric.SelectedValue.ToString, chkAerialField.Checked.GetType, _
        chkCAD.Checked.GetType, chkGPSBckgrnd.Checked.GetType, chkDataCD.Checked.GetType, _
        chkDigi2clnt.Checked.GetType, chkOtherfield.Checked.GetType, chkDigCad.Checked.GetType, _
        chkParcel.Checked.GetType, chkAerial.Checked.GetType, chkGPS.Checked.GetType, _
        chkOther.Checked.GetType, txtNotes.Text))        

         Label5.Text = "Request # " & requestno & " has been entered successfully. You will receive an email shortly."

        If ddlRush.SelectedIndex = 2 Then

            'Add the sender, and recepients. CC the user.
            m.From = New MailAddress("GIS@foothill.com")
            m.To.Add("jsanchez@Foothill.com")

            'Add the subject to the email so it gets by the SPAM filter.
            m.Subject = "Request # for " & txtProjName.Text & " - RUSH."
            m.Body = "The request from " & txtEmpName.Text & " is due " & txtDateBy.Text & " and is expected to take " & txtHours.Text & "."
           
             'Make sure the body of the email is HTML
            m.IsBodyHtml = True
            m.Priority = MailPriority.High

            'Send the message
            Dim smtp As New SmtpClient("192.168.254.13")
            smtp.Send(m)
            cmdSubmit.PostBackUrl = "~/Default.aspx"

        ElseIf ddlRush.SelectedIndex = 1 Then
            With m
                'Add the sender, and recepients. CC the user.
                .From = New MailAddress("GISAdmin@foothill.com")
                .To.Add("jsanchez@foothill.com")

                'Add the subject to the email so it gets by the SPAM filter.
                .Subject = "Request # has been submitted"
                .Body = "The request from " & txtEmpName.Text & " is due " & txtDateBy.Text & " and is expected to take " & txtHours.Text & "."

                'Make sure the body of the email is HTML
                .IsBodyHtml = True
                .Priority = MailPriority.Normal

                'Send the message
                Dim smtp As New SmtpClient("192.168.254.13")
                smtp.Send(m)
                cmdSubmit.PostBackUrl = "~/Default.aspx"
            End With

        End If
    End Sub

    Protected Sub ddlReqBy_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ddlReqBy.SelectedIndexChanged
        Dim email As New taPM
        txtReqEmail.Text = email.GetPM(ddlReqBy.SelectedIndex).pmEmail
        txtEmpName.Text = email.GetPM(ddlReqBy.SelectedIndex).pmName
    End Sub
End Class
0
Comment
Question by:sithman17
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Accepted Solution

by:
hfpon earned 1000 total points
ID: 17854894
Create a new column in your sql table (for example chk1column)
Change the data type to bit.


In your design view, add a sql datasource

                    <asp:SqlDataSource ID="InsertCheckBox" runat="server" ConnectionString="<%$ ConnectionStrings:ASPNETDBConnection %>"
                        InsertCommand="INSERT INTO [Table] ([chk1column], [othercolumn1], [othercolumn2]) VALUES (@chk1column, @othercolumn1, @othercolumn2)"
                        ProviderName="<%$ ConnectionStrings:ASPNETDBConnection.ProviderName %>">
                    </asp:SqlDataSource>

In your source view. Under your "submit" code add the following

        InsertCheckBox.InsertParameters.Add("chk1column", CheckBox1.Checked)
        InsertCheckBox.InsertParameters.Add("othercolumn1", textbox1.text)
        InsertCheckBox.InsertParameters.Add("othercolumn2", textbox2.text)
        InsertCheckBox.Insert()
0
 
LVL 16

Assisted Solution

by:Edwin_C
Edwin_C earned 1000 total points
ID: 17854950
Firstly SQL Server does not have the boolean data type and so you cannot save a boolean value as true/false directly.  You can use a char(1) to store 'Y' or 'N'.  Alternatively you can use a bit or tinyint type to store 1 or 0.  This is your choice.

Suppose you use a tinyint field to store the boolean value.  Then you can do the conversion in your data access layer like this.

Public Sub AddNewRecord(name as string, isYesNo as boolean)
  Dim cmd as SqlCommand
  cmd = new SqlCommand()
  cmd.Commandtext = "INSERT ........"
  cmd.Connection = myDBConnection
  cmd.Parameters.AddWithValue("name", name)
  cmd.Parameters.AddWithValue("isYesNo", iif(isYesNo, 1, 0))
  myDBConnection.Open()
  cmd.ExecuteNonQuery()
  myDBConnection.Close()
End Sub
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

656 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question