Solved

ASP.NET inserting checkbox value into MS SQL database table

Posted on 2006-07-23
4
2,266 Views
Last Modified: 2011-10-03
Hi Experts,

I have an ASP.NET/VB.NET Web Form containing an <asp:checkbox runat="server" id="chkNewsletter" /> control.

I would like to submit a 'Y' or 'N' value to the "Newsletter" bit field in a SQL database table.

Whenever the checkbox is checked and the form is submitted, I would like the value of the database field to be set to 'Y' when checked or 'N' when unchecked.

Any help would be appreciated.

Thanks
0
Comment
Question by:yaney00074
  • 3
4 Comments
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 17165301
 
        Dim c As Char
        If (chkNewsletter.Checked = True) Then
            c = "Y"
        Else
            c = "N"
        End If
        Dim param As SqlClient.SqlParameter
        param = New SqlParameter("input", SqlDbType.Char)
        param.Value = c
        Dim query As String
        query = "Insert into mytable(Newsletter) values('" + c + "')"
        Dim cmd As New SqlCommand
        cmd.Connection = SqlConnection1
        cmd.CommandText = query
        cmd.ExecuteNonQuery()
0
 
LVL 1

Expert Comment

by:gfuture4me
ID: 17165978
Hope a small code than above /////

If (chkNewsLetter.Checked= True)
         c="Y"
Else
         c= "N"

dim sqlcon as new sqlconnection(sqlstring)
dim sqlcom as new sqlcommand(sqlcon,"Insert into table(NewsLetter) values('"& c &"')")
sqlcom.connection.open()
sqlcom.executeScalar();
sqlcom.connection.close()
0
 
LVL 7

Expert Comment

by:pradeepsudharsan
ID: 17166216
Try
            If (SqlConnection1.State <> ConnectionState.Open) Then
                SqlConnection1.Open()
            End If
            Dim c As Char
            If (chkNewsletter.Checked = True) Then
                c = "Y"
            Else
                c = "N"
            End If
            Dim param As New SqlClient.SqlParameter("input", SqlDbType.Char)
            param.Value = c
            Dim cmd As New SqlCommand("Insert into mytable(Newsletter) values('" + c + "')", SqlConnection1)
            cmd.ExecuteNonQuery()
        Catch ex As Exception
            Response.Write("Error while inserting data.")
        Finally
            SqlConnection1.Close()
        End Try
0
 
LVL 7

Accepted Solution

by:
pradeepsudharsan earned 500 total points
ID: 17166254
Simple Code
-----------------------      
Dim SqlConn As New SqlConnection("Assign connection String here")
        SqlConn.Open()
        Dim c As Char = "Y"
        If (chkNewsletter.Checked <> True) Then
            c = "N"
        End If
        Dim cmd As New SqlCommand("Insert into mytable(Newsletter) values('" + c + "')", SqlConn)
        cmd.ExecuteNonQuery()
        SqlConn.Close()
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now