Programming for Everybody: November 2020

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



How to update,delete image into a sql server database using vb.net(with Source Code+database)Part2

 


Full Code (Part1+part2)

Imports System.Data.SqlClient
Imports System.IO
Imports System.Drawing.Imaging

Public Class Form2


    Dim con1 As New SqlConnection("Server=.;Database=images;integrated security=true")

    Private Sub Form2_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        bind1()
    End Sub

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


        Dim openfiledialog1 As New OpenFileDialog


        openfiledialog1.Filter = "images|*.jpg;*.png;*.gif;*.bmp"


        If openfiledialog1.ShowDialog = Windows.Forms.DialogResult.OK Then



            PictureBox1.Image = Image.FromFile(openfiledialog1.FileName)


        End If

    End Sub

    Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click


        Dim command1 As New SqlCommand("Insert into table1(name,age,img1)values(@name,@age,@img)", con1)

        command1.Parameters.Add("@name", SqlDbType.VarChar).Value = TextBox1.Text


        command1.Parameters.Add("@age", SqlDbType.VarChar).Value = TextBox2.Text



        Dim memstr As New MemoryStream


        PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)

        command1.Parameters.Add("@img", SqlDbType.Image).Value = memstr.ToArray


        If con1.State = ConnectionState.Closed Then

            con1.Open()


        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("image inserted")

        con1.Close()

        bind1()

    End Sub


    Private Sub bind1()

        Dim command1 As New SqlCommand("Select * from table1 order by id desc", con1)


        Dim adapter As New SqlDataAdapter(command1)


        Dim table As New DataTable()

        adapter.Fill(table)

        DataGridView1.AllowUserToAddRows = False

        DataGridView1.RowTemplate.Height = 90


        Dim pic1 As New DataGridViewImageColumn

        DataGridView1.DataSource = table

        pic1 = DataGridView1.Columns(3)

        pic1.ImageLayout = DataGridViewImageCellLayout.Stretch


    End Sub



    Private Sub DataGridView1_Click(sender As Object, e As System.EventArgs) Handles DataGridView1.Click


        Label4.Text = DataGridView1.CurrentRow.Cells(0).Value

        TextBox1.Text = DataGridView1.CurrentRow.Cells(1).Value


        TextBox2.Text = DataGridView1.CurrentRow.Cells(2).Value



        Dim img As Byte()

        img = DataGridView1.CurrentRow.Cells(3).Value

        Dim memstr As New MemoryStream(img)


        PictureBox1.Image = Image.FromStream(memstr)




    End Sub

    Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click


        Dim memstr As New MemoryStream

        PictureBox1.Image.Save(memstr, PictureBox1.Image.RawFormat)


        Dim img() As Byte

        img = memstr.ToArray()

        Dim update1 As String = "Update table1 set name='" & TextBox1.Text & "',age='" & TextBox2.Text & "',img1=@img where id=" & Label4.Text

        Dim command1 As New SqlCommand(update1, con1)

        command1.Parameters.Add("@img", SqlDbType.Image).Value = img

        If con1.State = ConnectionState.Closed Then

            con1.Open()
        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("Image updated")

        con1.Close()

        bind1()

    End Sub

    Private Sub Button4_Click(sender As System.Object, e As System.EventArgs) Handles Button4.Click


        Dim delete1 As String = "Delete from table1 where id=" & Label4.Text

        Dim command1 As New SqlCommand(delete1, con1)

        If con1.State = ConnectionState.Closed Then

            con1.Open()

        End If

        command1.ExecuteNonQuery()

        MessageBox.Show("Image deleted")


        con1.Close()


        bind1()


    End Sub
End Class

Visual Basic.net: Insert data into sql Database and Solve Violation of PRIMARY KEY

 


:Source code

Imports System.Data.SqlClient


Public Class Form4

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


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


        con1.Open()

        Dim cmd2 As New SqlCommand("Select name1,username1 from table1 where username1=@username1", con1)


        cmd2.Parameters.AddWithValue("@username1", TextBox2.Text)

        Dim myreader As SqlDataReader

        myreader = cmd2.ExecuteReader()

        If (myreader.Read()) Then

            con1.Close()

            MessageBox.Show("Duplicate Username")

            con1.Close()

        Else

           con1.Close()

            Dim strsql As String


            strsql = " Insert into table1(name1,username1) Values(@name,@username)"


            Dim cmd1 As New SqlCommand(strsql, con1)


            cmd1.Parameters.AddWithValue("@name", TextBox1.Text)

            cmd1.Parameters.AddWithValue("@username", TextBox2.Text)


            con1.Open()

            cmd1.ExecuteNonQuery()

            con1.Close()

            MessageBox.Show(" insert successful")


        End If


    End Sub

End Class

Retrieve data from a database in VB.net and Solve Invalid attempt to read when no data is present


 

Imports System.Data.SqlClient

Public Class Form3

    Private Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim con1 As SqlConnection = New SqlConnection("Data Source=.;Initial Catalog=names;Integrated Security=True")
        con1.Open()
        Dim cmd1 As New SqlCommand(" Select name1,tel from table1 where id=@parm1", con1)

        cmd1.Parameters.AddWithValue("@parm1", TextBox1.Text)

        Dim myreader As SqlDataReader

        myreader = cmd1.ExecuteReader()

        If (myreader.Read()) Then
            TextBox2.Text = myreader("name1")
            TextBox3.Text = myreader("tel")
        Else
            TextBox2.Text = ""
            TextBox3.Text = ""
            MessageBox.Show("No Data Found")
        End If
       

        con1.Close()


    End Sub