Solved

ASP.NET inserting checkbox value into MS SQL database table

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

636 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