Link to home
Start Free TrialLog in
Avatar of yaney00074
yaney00074

asked on

ASP.NET inserting checkbox value into MS SQL database table

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
Avatar of pradeepsudharsan
pradeepsudharsan

 
        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()
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()
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
ASKER CERTIFIED SOLUTION
Avatar of pradeepsudharsan
pradeepsudharsan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial