Programming for Everybody: Visual Basic.net: Insert RadioButton values into database (with source code)

Visual Basic.net: Insert RadioButton values into database (with source code)





Imports System.Data.SqlClient


Public Class Form3


    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim conn As New SqlConnection("Data Source=.;Initial Catalog=users;Integrated Security=True")

        Dim name1 As String = TextBox1.Text

        Dim sport1 As String = String.Empty

        Dim hobby1 As String = String.Empty

        If f1.Checked Then

            sport1 = f1.Text

        ElseIf h1.Checked Then

            sport1 = h1.Text

        ElseIf V1.Checked Then

            sport1 = V1.Text

        End If

        If r1.Checked Then

            hobby1 = r1.Text

        ElseIf fi1.Checked Then

            hobby1 = fi1.Text

        ElseIf p1.Checked Then

            hobby1 = p1.Text

        End If

        Dim strsql As String

        strsql = " insert into table3(name,sport,hobby)Values(@name,@sport,@hobby)"

        Dim cmd1 As New SqlCommand(strsql, conn)

        cmd1.Parameters.AddWithValue("@name", name1)

        cmd1.Parameters.AddWithValue("@sport", sport1)

        cmd1.Parameters.AddWithValue("@hobby", hobby1)


        conn.Open()

        cmd1.ExecuteNonQuery()

        conn.Close()

        MessageBox.Show("Data Inserted")

    End Sub



No comments:

Post a Comment