Hello DotNetDoc,
appreciate your time and desire to help.
This is a goal of mine.
1. I got to read data from the SQL Server DB and represent the report in the Excel format.
To do it I wrote the following code:
Imports System
Imports System.Drawing
Imports System.Collections
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Data
Imports System.Data.SqlClient
Namespace SQLServertoExcel
' Summary description for Form2.
Public Class Form2
Inherits System.Windows.Forms.Form
Friend WithEvents button1 As System.Windows.Forms.Button
Private components As System.ComponentModel.Container = Nothing
Public Sub New()
' Required for Windows Form Designer support
InitializeComponent()
End Sub 'New
Protected Overloads Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub 'Dispose
' <summary>
' Required method for Designer support - do not modify
' the contents of this method with the code editor.
' </summary>
Private Sub InitializeComponent()
Me.button1 = New System.Windows.Forms.Button()
Me.SuspendLayout()
'
'button1
'
Me.button1.Location = New System.Drawing.Point(32, 72)
Me.button1.Name = "button1"
Me.button1.Size = New System.Drawing.Size(216, 112)
Me.button1.TabIndex = 0
Me.button1.Text = "Populate Excel With Customer Information From Microsoft SQL Server"
'
'Form2
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(292, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.button1})
Me.Name = "Form2"
Me.Text = "Form2"
Me.ResumeLayout(False)
End Sub 'InitializeComponent
' <summary>
' The main entry point for the application.
' </summary>
<STAThread()> _
Shared Sub Main()
Application.Run(New Form2())
End Sub 'Main
Private Function GetData() As System.Data.DataTable
Dim conn As SqlConnection
Dim strConn As String
'Set the Conn String to the NSRCG DB
'Don't forget to add TRUSTED_CONNECTION = TRUE for Development
strConn = "server=MPR23;database=NSRCG;TRUSTED_CONNECTION=TRUE"
conn = New SqlConnection(strConn)
Dim adapter As New SqlDataAdapter("select * FROM tlkpStatus", conn)
Dim myDataSet As New DataSet()
Try
adapter.Fill(myDataSet, "Status"

Catch ex As Exception
MessageBox.Show(ex.ToString())
End Try
Return myDataSet.Tables(0)
End Function 'GetData
Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles button1.Click
Dim excel As New Excel.ApplicationClass()
Dim rowIndex As Integer = 1
Dim colIndex As Integer = 0
excel.Application.Workbooks.Add(True)
Dim table As DataTable = GetData()
Dim col As DataColumn
For Each col In table.Columns
colIndex += 1
excel.Cells(1, colIndex) = col.ColumnName
Next col
Dim row As DataRow
For Each row In table.Rows
rowIndex += 1
colIndex = 0
Dim col1 As DataColumn
For Each col1 In table.Columns
colIndex += 1
excel.Cells(rowIndex, colIndex) = row(col1.ColumnName).ToString()
Next col1
Next row
excel.Visible = True
End Sub 'button1_Click
Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class 'Form2
End Namespace 'SQLServertoExcel
2. When the user updates whatever fields in that Excel document and saves Excel, Im supposed to write a code to update the DB. That's what I don't know how to do.