Solved

ASP.NET inserting checkbox value into MS SQL database table

Posted on 2006-07-23
4
2,268 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

911 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

22 Experts available now in Live!

Get 1:1 Help Now