code for export,import,save,update,delete ?



code for export,import,save,update,delete ?..

Answer / sree11

Imports System.Data.SqlClient
Imports System
Imports System.IO
Imports System.Windows.Forms


Public Class Department
Dim qstring As String
Dim dataadapter As SqlDataAdapter
Dim deptid As Integer

Private Sub Clear_all()
txt_Deptname.Clear()
btn_Save.Text = "&Save"
txt_Excelsheet.Text = "Sheet1"
txt_Filename.Clear()
chk_Header.Checked = True
Errp.Clear()
clear_Toolstrip()
End Sub
Private Sub Filldgv_view()
Dim dtset As New DataSet

Dim counter As Integer
dataadapter = New SqlDataAdapter("select
dept_id,dept_name from dept_master", Con) 'adopting the
requirred fields from the tabel")
dataadapter.Fill(dtset, "Table") 'filling the
dataset with the adopted values
dgv_View.DataSource = dtset.Tables
("Table") 'assigning the values from dataset to the
datagrid view
dgv_View.AlternatingRowsDefaultCellStyle.BackColor
= Color.Bisque 'assigning the alternating color to the
datagrid rows
For counter = 1 To dtset.Tables
("Table").Columns.Count
dgv_View.Columns(counter).ReadOnly = True
Next
dgv_View.Columns(0).Width = 50
dgv_View.Columns(1).Visible = False
dgv_View.Columns(2).HeaderText = "Department Name"
clear_Toolstrip()
End Sub
Private Sub clear_Toolstrip()
stl_Text.Text = ""
stl_Image.Image = Nothing
End Sub

Public Sub Getdeptdetails(ByVal deptname As String)
If deptname <> "" And btn_Save.Text = "&Save" Then
qstring = "Select dept_id,dept_name from
dept_master where dept_name= '" & Trim(deptname) & "'"
Dim dtset As New DataSet
dataadapter = New SqlDataAdapter(qstring, Con)
dataadapter.Fill(dtset, "Dept")
If dtset.Tables("dept").Rows.Count <> 0 Then
deptid = CInt(dtset.Tables("dept").Rows
(0).Item(0).ToString)
txt_Deptname.Text = dtset.Tables
("dept").Rows(0).Item(1).ToString
btn_Save.Text = "&Update"
Else
btn_Save.Text = "&Save"
End If
End If
End Sub
Private Sub save_Update(ByVal Caller As String)
If txt_Deptname.Text <> "" Then
Dim QSTR As String
Dim rstring As String
QSTR = "Select count(*) from dept_master where
dept_name = '" & Trim(txt_Deptname.Text) & "'"
If (Errp.GetError(txt_Deptname) = "") And
reccount(QSTR) = 0 Then
If Caller = 1 Then
qstring = "insert into dept_master
(dept_name) values('" & Trim(txt_Deptname.Text) & "')"
rstring = "Department Successfully
Inserted."
Else
qstring = "Update dept_master set
dept_name='" & Trim(txt_Deptname.Text) & "' where dept_id
= " & deptid
rstring = "Department Succesfully
Updated."
End If

Dim cmd As SqlCommand
cmd = New SqlCommand(qstring, Con)
Try
If cmd.ExecuteNonQuery = 1 Then
Clear_all()
Filldgv_view()
stl_Text.Text = rstring
stl_Text.ForeColor = Color.Green
'stl_Image.Image =
My.Resources.Green

End If
Catch ex As Exception
stl_Text.Text = "Some Problem Occured
In process. Restart the application."
stl_Text.ForeColor = Color.Yellow
' stl_Image.Image = My.Resources.yellow
End Try
End If
End If

End Sub

'*******************************************************
************************************************
'******************************************* End Of
Functions Section ************************
'******************************************* Starting
of Event Handling Section ************************
'*******************************************************
************************************************
'calls when the keyis pressed in the excel sheet name
text box to allow required characters
Private Sub txt_Excelsheet_KeyPress(ByVal sender As
Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
Handles txt_Excelsheet.KeyPress
clear_Toolstrip()
e.KeyChar = AllowAlphabetsNumeric
(e.KeyChar) 'allowing only number values
End Sub
'calls whether the mouse leaves the excel sheet text
box and checking the values
Private Sub txt_Excelsheet_Validating(ByVal sender As
Object, ByVal e As System.ComponentModel.CancelEventArgs)
Handles txt_Excelsheet.Validating
If txt_Excelsheet.Text = "" Then
Errp.SetError(txt_Excelsheet, "excel sheet name
should not be blank") ''assigning the error provider to
department name text box
Else
Errp.SetError(txt_Excelsheet, "")
End If
End Sub
'calls whether the mouse leaves the departmentname text
box and checking the values
Private Sub txt_Deptname_Leave(ByVal sender As Object,
ByVal e As System.EventArgs) Handles txt_Deptname.Leave
Getdeptdetails(txt_Deptname.Text)
End Sub
'calls the function whether the value inserted is
validating or not
Private Sub txt_Deptname_Validating(ByVal sender As
Object, ByVal e As System.ComponentModel.CancelEventArgs)
Handles txt_Deptname.Validating
If txt_Deptname.Text = "" Then
Errp.SetError(txt_Deptname, "Department name
should not be Null") 'assigning the error provider to
department name text box
Else
Errp.SetError(txt_Deptname, "") ''assigning
the error provider to department name text box
End If
End Sub
'saves the new record or update the existed record to
be done
Private Sub btn_Save_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btn_Save.Click
If btn_Save.Text = "&Save" Then
save_Update(1)
Else
save_Update(2)
End If

End Sub


'clearing the textboxes values
Private Sub btn_Clear_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btn_Clear.Click
Clear_all()
End Sub
'call the function to close the form

Private Sub btn_Cancel_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btn_Cancel.Click
Me.Close() 'closing the form
End Sub
'calls the function to browse the file
Private Sub btn_Browse_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btn_Browse.Click
If txt_Excelsheet.Text <> "" Then
Dim myStream As IO.Stream
Dim myfile As String
stl_Text.Text = ""
dlg_open.Filter = "Excel files (*.xls)
|*.xls|All files (*.*)|*.*" 'filtering the excel files
dlg_open.FilterIndex = 2
dlg_open.RestoreDirectory = True
'if the file is selected then the if condition
is true
If dlg_open.ShowDialog() =
System.Windows.Forms.DialogResult.OK Then
Try
myStream = dlg_open.OpenFile() 'open's
the file and stores the values to the mystream reader
myfile = dlg_open.FileName() 'saves the
file name into myfile string
txt_Filename.Text = myfile.ToString
If Not (myStream Is Nothing) Then
myStream.Close() 'closing the file
End If
Catch ex As Exception

stl_Text.Text = "The Selected file is
Open.Please close it and try again"
stl_Text.ForeColor = Color.Red
'stl_Image.Image = My.Resources.Red
txt_Filename.Clear()
End Try


End If
End If

End Sub
'calls whether the import button was clicked to import
the rows from the excel sheet
Private Sub btn_Import_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
btn_Import.Click
If txt_Filename.Text = "" Then 'checking whether
the file name null of not if null exit the xub

stl_Text.Text = "file name should not be null"
stl_Text.ForeColor = Color.Red
' stl_Image.Image = My.Resources.Red
Exit Sub
End If
If txt_Excelsheet.Text = "" Then 'checking
whether the excel sheet name is null of not if null exit
the xub

stl_Text.Text = "sheet column should not be
null"
stl_Text.ForeColor = Color.Red
'stl_Image.Image = My.Resources.Red
Exit Sub
End If
If txt_Filename.Text <> "" And txt_Excelsheet.Text
<> "" Then
clear_Toolstrip()
Importexcel()
End If


End Sub

'calls when the import button clicked to import the
rows in the excel sheet of selected filname and sheet name
Private moApp As Excel.Application
Private Sub Importexcel()
Dim dtset As DataSet
Dim hstat As Boolean
Dim Deptname As String
If chk_Header.Checked = True Then
hstat = True
Else
hstat = False
End If

dtset = Importdtsetfromexel(txt_Filename.Text,
txt_Excelsheet.Text, hstat, Me)
ppg_ImportExcel.Maximum = dtset.Tables
(0).Rows.Count + 20
ppg_ImportExcel.Value = 10
Dim fail(dtset.Tables(0).Rows.Count - 1) As Integer
Dim failcounter As Integer = 0
Dim succounter As Integer = 0
Dim counter As Integer = 0

For counter = 0 To dtset.Tables(0).Rows.Count - 1
'Check deptname validation
Deptname = dtset.Tables(0).Rows(counter).Item
(0).ToString
hstat = True

If Deptname <> "" And hstat = True And
Deptname.Length <= 50 Then
If reccount("Select * from dept_master
where dept_name='" & Deptname & "'") <> 0 Then
hstat = False
End If
Else
hstat = False
End If
If hstat = True Then
qstring = "insert into dept_master
(dept_name) values('" & Deptname & "')"
Dim cmd As SqlCommand
cmd = New SqlCommand(qstring, Con)
Try
cmd.ExecuteNonQuery()
succounter += 1
Catch ex As Exception
fail(failcounter) = counter
failcounter += 1
End Try
Else
fail(failcounter) = counter
failcounter += 1
End If
ppg_ImportExcel.Value = ppg_ImportExcel.Value +
1
Next

Try
'<INITIALIZE EXCEL>
moApp = DirectCast(GetObject
(, "Excel.Application"), Excel.Application)
Catch ex As Exception
'<CREATE EXCEL SINCE NOT RUNNING>
If TypeName(moApp) = "Nothing" Then
moApp = DirectCast(CreateObject
("Excel.Application"), Excel.Application)
Else
MessageBox.Show(ex.Message, "Export
Contacts", _
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation)
End If
End Try
Dim oWB As Excel.Workbook = moApp.Workbooks.Add
Dim oSht As Excel.Worksheet = DirectCast(oWB.Sheets
("Sheet1"), Excel.Worksheet)
oSht.Cells(1, 1) = "Department Name"
If failcounter > 0 Then
For counter = 0 To failcounter - 1
oSht.Cells(counter + 2, 1) = dtset.Tables
(0).Rows(fail(counter)).Item(0)
Next
End If
Dim directory, fname As String
Directory = System.IO.Path.GetDirectoryName
(txt_Filename.Text)
fname = System.IO.Path.GetFileNameWithoutExtension
(txt_Filename.Text)
fname = Directory & "\Fail" & fname & ".xls"

Try
File.Delete(fname)
oWB.Close(True, fname)
oSht = Nothing
oWB = Nothing

Catch ex As Exception
End Try
ppg_ImportExcel.Value = ppg_ImportExcel.Value + 10
Filldgv_view()
If failcounter = 0 Then
stl_Text.Text = succounter.ToString & " Records
inserted successfully"
stl_Text.ForeColor = Color.Green
'stl_Image.Image = My.Resources.Green
ElseIf succounter = 0 Then
stl_Text.Text = " Fail to import records"
stl_Text.ForeColor = Color.Red
'tl_Image.Image = My.Resources.Red
Else
stl_Text.Text = succounter.ToString & " Records
inserted and " & failcounter.ToString & " Records failed to
insert"
stl_Text.ForeColor = Color.Yellow
'stl_Image.Image = My.Resources.yellow
End If
ppg_ImportExcel.Value = 0
End Sub

Private Sub lnk_Selectall_Leave(ByVal sender As Object,
ByVal e As System.EventArgs) Handles lnk_Selectall.Leave
clear_Toolstrip()
End Sub

'calls when lnk_selectall linklabel pressed
'selets all the rows in datagridview
Private Sub lnk_Selectall_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
lnk_Selectall.LinkClicked
clear_Toolstrip()
Dim Counter As Integer
For Counter = 0 To dgv_View.RowCount - 1
dgv_View.Item(0, Counter).Value = True
Next
End Sub

Private Sub lnk_Deselectall_Leave(ByVal sender As
Object, ByVal e As System.EventArgs) Handles
lnk_Deselectall.Leave
clear_Toolstrip()
End Sub
'calls when lnk_Deselectall linklabel pressed
'deselets all the rows in datagridview
Private Sub lnk_Deselectall_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
lnk_Deselectall.LinkClicked
clear_Toolstrip()
Dim Counter As Integer
For Counter = 0 To dgv_View.RowCount - 1
dgv_View.Item(0, Counter).Value = False
Next
End Sub

'calls when lnk_pritn linklabel pressed
'prints all the rows in datagridview
Private Sub lnk_Print_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
lnk_Print.LinkClicked
'PrintDocument1.Print()
clear_Toolstrip()
PrintDGV.Print_DataGridView(dgv_View)
End Sub
'which loads the datagridview with items in the table.

Private Sub Department_Load(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
MyBase.Load
clear_Toolstrip()
Filldgv_view()
End Sub

'calls when context menu strip item edit was selected
Private Sub EditToolStripMenuItem_Click(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
EditToolStripMenuItem.Click
Getdeptdetails(dgv_View.CurrentRow.Cells
(2).Value.ToString)
End Sub

Private Sub lnk_Delete_Leave(ByVal sender As Object,
ByVal e As System.EventArgs) Handles lnk_Delete.Leave
clear_Toolstrip()
End Sub
'assigning the values of textboxes to varialbles

'calls when context menu strip item edit was selected

Private Sub lnk_Delete_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
lnk_Delete.LinkClicked
clear_Toolstrip()
deletefromdb_datagridview(Me,
dgv_View, "dept_master", "dept_id", 1)
End Sub
'call the sub when delete in the context menu item was
clicked
Private Sub DeleteToolStripMenuItem_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
DeleteToolStripMenuItem.Click
dgv_View.CurrentRow.Cells(0).Value = True
deletefromdb_datagridview(Me,
dgv_View, "dept_master", "dept_id", 1)
End Sub

Private Sub lnk_Export_Leave(ByVal sender As Object,
ByVal e As System.EventArgs) Handles lnk_Export.Leave
clear_Toolstrip()
End Sub
'calls when the export link clicked
Private Sub lnk_Export_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
lnk_Export.LinkClicked
clear_Toolstrip()
dlg_Save.FileName = ""
dlg_Save.Filter = "Excel files (*.xls)
|*.xls" 'filtering the files for only getting excel files
dlg_Save.ShowDialog() 'shows the save file dialog
Dim fn As String
fn = dlg_Save.FileName
If fn <> "" Then
exporttoexcel_fromdgv(Me, dgv_View,
fn, "Department Report")
End If
End Sub

'selecting row for context menu strip items
Private Sub DataGridView1_MouseUp(ByVal sender As
Object, ByVal e As System.Windows.Forms.MouseEventArgs)
Handles dgv_View.MouseUp
Dim hit As
System.Windows.Forms.DataGridView.HitTestInfo
hit = dgv_View.HitTest(e.X, e.Y)
If hit.Type = DataGridViewHitTestType.Cell Or
hit.Type = DataGridViewHitTestType.RowHeader Then
ctm_Dept.Items(0).Enabled = True
ctm_Dept.Items(1).Enabled = True
dgv_View.Rows(hit.RowIndex).Selected = True
dgv_View.CurrentCell = dgv_View.Rows
(hit.RowIndex).Cells(0)
Else
ctm_Dept.Items(0).Enabled = False
ctm_Dept.Items(1).Enabled = False
End If

End Sub

Private Sub RefreshToolStripMenuItem_Click(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
RefreshToolStripMenuItem.Click
Filldgv_view()
End Sub

Private Sub btn_Save_Leave(ByVal sender As Object,
ByVal e As System.EventArgs) Handles btn_Save.Leave
clear_Toolstrip()
End Sub

Private Sub lnk_Refresh_LinkClicked(ByVal sender As
System.Object, ByVal e As
System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles
lnk_Refresh.LinkClicked
Filldgv_view()

End Sub

Private Sub Departments_Enter(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Departments.Enter
Clear_all()
End Sub

Private Sub Departments_Leave(ByVal sender As Object,
ByVal e As System.EventArgs) Handles Departments.Leave
Filldgv_view()
End Sub

Private Sub txt_Excelsheet_TextChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
txt_Excelsheet.TextChanged

End Sub


Private Sub GroupBox1_Enter(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
GroupBox1.Enter
clear_Toolstrip()
End Sub

Private Sub GroupBox2_Enter(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
GroupBox2.Enter
Clear_all()
End Sub
End Class

Is This Answer Correct ?    0 Yes 1 No

Post New Answer

More VB.NET Interview Questions

Define naming convention?

0 Answers  


What are the advantages of an assembly?

0 Answers  


Where is the Version Information Stored on a assembly ? Write the Namespace to load assemblies at runtime Can you allow a class to be inherited but prevent the method from being overridden ? What happens in memory when you box and unbox a value type ? Write a progtam to convert decimal to byte without using library function.

2 Answers   HCL,


What is the Difference between Dataset and Datareader?

7 Answers  


Explain jit?

0 Answers  






What is the difference between manifest and metadata?

0 Answers  


Which dll is used for microsoft .net run time?

0 Answers  


What are the objects in asp.net?

0 Answers  


Write a program for calculator in VB.NET

6 Answers   CMC, College School Exams Tests, IBM, NIIT, Practical Viva Questions, Subex,


if user enters 1 or 2 or any other number in digit format in next textbox it should show the answer in word reading like if enters 12 answer should be twelve

4 Answers  


What are the shadow variables?

0 Answers  


Explain clr?

0 Answers  


Categories