Welcome to ADO.NET Access 2003—your ultimate hub for VB.NET and ADO.NET programming excellence. Discover in-depth tutorials, practical code samples, and expert troubleshooting guides covering a broad range of topics—from building robust WinForms applications and seamless MS Access integration to working with SQL Server, MySQL, and advanced tools like WebView2 and Crystal Reports. Whether you're a beginner or a seasoned developer, our step-by-step articles are designed to empower you to optimize.

Looking for MS Access Developer❓❓

Application developer

Post Page Advertisement [Top]

CRUD Operations in VB.NET 

with SQL Server

VB.NET SQL Server Crystal Reports
In this guide, you'll learn how to perform basic CRUD operations — Create, Read, Update, and Delete — using VB.NET and SQL Server. We'll also cover how to generate reports using Crystal Reports.

Our sample project is called StudentManager. It handles a student database with fields like StudentID, FullName, BirthDate, and Major.

🔧 Database Table Structure

  • Table Name: Students
  • Fields: StudentID (int), FullName (varchar), BirthDate (date), Major (varchar)

💡 Project Setup

  • Create a new VB.NET Windows Forms Application named StudentManager.
  • Add a DataGridView and textboxes for input.
  • Add buttons for: Add, Update, Delete, Search, Print, and Exit.

📦 Naming Conventions

  • txtName, txtMajor, dtpBirthDate, dgvStudents
  • btnAdd, btnUpdate, btnDelete, btnSearch, btnReport

🧑‍💻 CRUD Code Snippets (PrismJS Ready)

🍀SQL Declaration

  Imports MySql.Data.MySqlClient

🧼SQL Server ConnectionString in VB.NET
Dim connString As String = "Server=evry1falls;Port=3307;Database=ProductDB;User Id=root;Password=AhmedSamir1_;"
	Dim conn As New MySqlConnection(connString)

🟢 INSERT Code

Sub CreateData()
    Try
        conn.Open()
        Dim query As String = "INSERT INTO Products (ProductName, Price) VALUES (@name, @price)"
        Dim cmd As New MySqlCommand(query, conn)
        cmd.Parameters.AddWithValue("@name", "Sample Product")
        cmd.Parameters.AddWithValue("@price", 100.0)
        cmd.ExecuteNonQuery()
        conn.Close()
        MessageBox.Show("Data inserted successfully!")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

🔵 SELECT / READ Code

Sub ReadData()
    Try
        conn.Open()
        Dim query As String = "SELECT * FROM Products"
        Dim cmd As New MySqlCommand(query, conn)
        Dim reader As MySqlDataReader = cmd.ExecuteReader()
        While reader.Read()
            Console.WriteLine(reader("ProductName") & " - $" & reader("Price"))
        End While
        conn.Close()
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

🟠 Modify/UPDATE Code

Sub UpdateData()
    Try
        conn.Open()
        Dim query As String = "UPDATE Products SET Price=@price WHERE ProductName=@name"
        Dim cmd As New MySqlCommand(query, conn)
        cmd.Parameters.AddWithValue("@name", "Sample Product")
        cmd.Parameters.AddWithValue("@price", 120.0)
        cmd.ExecuteNonQuery()
        conn.Close()
        MessageBox.Show("Data updated successfully!")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

🔴 DELETE Code

Sub DeleteData()
    Try
        conn.Open()
        Dim query As String = "DELETE FROM Products WHERE ProductName=@name"
        Dim cmd As New MySqlCommand(query, conn)
        cmd.Parameters.AddWithValue("@name", "Sample Product")
        cmd.ExecuteNonQuery()
        conn.Close()
        MessageBox.Show("Data deleted successfully!")
    Catch ex As Exception
        MessageBox.Show(ex.Message)
    End Try
End Sub

Crystal Reports Code in VB.NET

Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared

Dim rpt As New ReportDocument()
rpt.Load(Application.StartupPath & "\StudentReport.rpt")

' Set DB login info (if needed)
rpt.SetDatabaseLogon("username", "password")

' Assign to CrystalReportViewer
CrystalReportViewer1.ReportSource = rpt
CrystalReportViewer1.Refresh()

📄 Crystal Reports Integration

After creating a Crystal Report, you can use the following placeholder to display it in a viewer.

Note: You'll need to add a .rpt file to your project and configure the database connection using the ReportDocument object.

🎓 Conclusion

With this structure, you can easily develop and manage VB.NET applications connected to SQL Server and include reporting features. Stay tuned for the full implementation of each code block!

 Here are some online Visual Basic lessons and courses:

Bottom Ad [Post Page]