Solved

ASP.NET inserting checkbox value into MS SQL database table

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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

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.…
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…

734 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