Option Explicit On
Option Strict On
Imports System.Text
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing.Drawing2D
Public Class frmAcademic
Dim Conn As SqlConnection
Dim com As SqlCommand
Dim tr As SqlTransaction
Dim sb As StringBuilder
Dim dr As SqlDataReader
Private Sub frmAcademic_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim strConn As String
strConn = DBConnString.strConn
Conn = New SqlConnection()
With Conn
If .State = ConnectionState.Open Then .Close()
.ConnectionString = strConn
.Open()
End With
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
If MessageBox.Show("Do you want to save", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
tr = Conn.BeginTransaction()
Try
sb = New StringBuilder()
sb.Append("INSERT INTO Academic (AcademicID,AcademicSaveDate,AcademicYear,SemesterNo,StartDate,EndDate)")
sb.Append(" VALUES (@AcademicID,@AcademicSaveDate,@AcademicYear,@SemesterNo,@StartDate,@EndDate)")
Dim sqlAdd As String
sqlAdd = sb.ToString()
With com
.CommandText = sqlAdd
.CommandType = CommandType.Text
.Connection = Conn
.Transaction = tr
.Parameters.Clear()
.Parameters.Add("@AcademicID", SqlDbType.NVarChar).Value = txtAcademicID.Text.Trim()
.Parameters.Add("@AcademicSaveDate", SqlDbType.DateTime).Value = dtpSaveDate.Value
.Parameters.Add("@AcademicYear", SqlDbType.NVarChar).Value = txtAcademicYear.Text.Trim()
.Parameters.Add("@SemesterNo", SqlDbType.NVarChar).Value = txtSemester.Text.Trim()
.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = dtpStartDate.Value
.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = dtpEndDate.Value
.ExecuteNonQuery()
End With
tr.Commit()
MessageBox.Show("Save Sucess !!! ", "Confirm", MessageBoxButtons.OK, MessageBoxIcon.Information)
Catch ex As Exception
MessageBox.Show("Erorr " & ex.Message, "Alert ", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
tr.Rollback()
End Try
End If
End Sub
End Class
.....
Dim sqlAdd As String
sqlAdd = sb.ToString()
com = New SqlCommand
With com
.CommandText = sqlAdd
.CommandType = CommandType.Text
.Connection = Conn
......