VB.NET APP, SHOWING HOW TO ADD, EDIT, DELETE AND DISPLAY CRYSTAL REPORT
DOWNLOAD : ADD; UPDATE; DELETE; CRYSTAL REPORT
Imports System
Imports System.Data
Imports System.Data.SqlClient
Public Class Form1
Dim myconn As SqlConnection
Dim mycmd As SqlCommand
Dim mydr As SqlDataReader
Dim dr As SqlDataReader
Dim adp As SqlDataAdapter
Dim dat As New DataSet
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dview()
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Public Sub dview()
Try
myconn = New SqlConnection(My.Settings.Setting)
myconn.Open()
mycmd = New SqlCommand("select * from usersched order by idd desc", myconn)
mydr = mycmd.ExecuteReader
Dim dtable As New DataTable
dtable.Load(mydr)
DataGridView1.DataSource = dtable
DataGridView1.Refresh()
myconn.Close()
mycmd.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, "datagridview")
myconn.Close()
mycmd.Dispose()
End Try
End Sub
Public Sub viewselected()
If DataGridView1.SelectedCells.Count > 0 Then
TextBox1.Text = DataGridView1.SelectedRows(0).Cells(0).Value
TextBox2.Text = DataGridView1.SelectedRows(0).Cells(1).Value
TextBox3.Text = DataGridView1.SelectedRows(0).Cells(2).Value
TextBox4.Text = DataGridView1.SelectedRows(0).Cells(3).Value
TextBox5.Text = DataGridView1.SelectedRows(0).Cells(4).Value
End If
End Sub
Private Sub DataGridView1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGridView1.Click
viewselected()
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xcounter As Integer = 0
Try
myconn = New SqlConnection(My.Settings.Setting)
myconn.Open()
mycmd = New SqlCommand("insert into usersched (sdate, suser, stime, sbooth) values ('" & TextBox2.Text & "', '" & TextBox3.Text & "', '" & TextBox4.Text & "', '" & TextBox5.Text & "')", myconn)
xcounter = mycmd.ExecuteNonQuery
MessageBox.Show("(" & xcounter & ") added")
myconn.Close()
mycmd.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, "Add failed")
myconn.Close()
mycmd.Dispose()
End Try
TextBox1.Text = ""
TextBox2.Text = ""
TextBox3.Text = ""
TextBox4.Text = ""
TextBox5.Text = ""
dview()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim xcounter As Integer = 0
myconn = New SqlConnection(My.Settings.Setting)
myconn.Open()
mycmd = New SqlCommand("update usersched set sdate='" & TextBox2.Text & "', suser= '" & TextBox3.Text & "', stime= '" & TextBox4.Text & "', sbooth ='" & TextBox5.Text & "' where idd = '" & TextBox1.Text & "'", myconn)
xcounter = mycmd.ExecuteNonQuery
MessageBox.Show("(" & xcounter & ") updated")
myconn.Close()
mycmd.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, "update failed")
myconn.Close()
mycmd.Dispose()
End Try
dview()
End Sub
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
If MessageBox.Show("are you sure to delete idd : " & DataGridView1.SelectedRows(0).Cells(0).Value, "Delete confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation) = Windows.Forms.DialogResult.Yes Then
Try
Dim xcounter As Integer = 0
myconn = New SqlConnection(My.Settings.Setting)
myconn.Open()
mycmd = New SqlCommand("delete from usersched where idd = '" & TextBox1.Text & "'", myconn)
xcounter = mycmd.ExecuteNonQuery
MessageBox.Show("(" & xcounter & ") removed")
myconn.Close()
mycmd.Dispose()
Catch ex As Exception
MessageBox.Show(ex.Message, "delete failed")
End Try
dview()
Else
End If
End Sub
Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
Try
myconn = New SqlConnection(My.Settings.Setting)
myconn.Open()
mycmd = New SqlCommand("select * from usersched where idd like '" & TextBox6.Text & "%'", myconn)
dr = mycmd.ExecuteReader
Dim dtable As New DataTable
dtable.Load(dr)
DataGridView1.DataSource = dtable
DataGridView1.Refresh()
Catch ex As Exception
End Try
End Sub
Public Sub isearch()
Dim i As Integer = 0
Dim icount As Integer = 0
Dim ifound As Boolean = False
Try
icount = DataGridView1.RowCount
DataGridView1.MultiSelect = False
While i < icount And ifound = False
DataGridView1.Rows(i).Selected = True
If DataGridView1.SelectedRows(0).Cells(0).Value = TextBox7.Text Then
ifound = True
DataGridView1.CurrentCell = DataGridView1(0, i)
viewselected()
Else
i = i + 1
End If
End While
Catch ex As Exception
MessageBox.Show(ex.Message, "search")
End Try
If ifound = False Then MessageBox.Show("no record found")
End Sub
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
isearch()
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
Try
Dim rpt As New CrystalReport1
'Dim frm_rpt As New creport
dat.Clear()
myconn = New SqlConnection(My.Settings.Setting)
myconn.Open()
mycmd = New SqlCommand("select * from usersched", myconn)
adp = New SqlDataAdapter(mycmd)
adp.Fill(dat, "usersched")
rpt.SetDataSource(dat)
creport.CrystalReportViewer1.ReportSource = rpt
creport.CrystalReportViewer1.Dock = DockStyle.Fill
Dim frm As New Form()
With frm
.Controls.Add(creport.CrystalReportViewer1)
.WindowState = FormWindowState.Maximized
.ShowDialog()
End With
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
'creport.Show()
End Sub
End Class
No comments:
Post a Comment