I have excel files that i want to match into one column of my database
And if the data in excel file is not match in data in database it will popup an exception error it says
"there is no row at position 0"
and i want to change this to specific row and column?
Here's my Whole Code
Imports MySql.Data
Imports MySql.Data.MySqlClient
Module ConnectionV2
Public connString As String = "Server=localhost;Database=lcs_db;Uid=root;Pwd=@v3p@$$w0rd;"
Public conn As MySqlConnection = New MySqlConnection(connString)
Public Function ExecuteSQLQuery(ByVal sql As String) As DataTable
Dim sqlDT As New DataTable
Try
Dim sqlCon As New MySqlConnection(connString)
Dim sqlDA As New MySqlDataAdapter(sql, connString)
Dim sqlCB As New MySqlCommandBuilder(sqlDA)
sqlDA.Fill(sqlDT)
Catch ex As Exception
'MsgBox("Error: " & ex.Message)
End Try
Return sqlDT
End Function
Public Function DT_Foreign_Key(ByVal select_column_name As String, ByVal table_name As String, ByVal where_column_name As String, ByVal where_value As String) As String
Dim dt_service_provider_id As DataTable = ExecuteSQLQuery("select " & select_column_name & " from " & table_name & " where " & where_column_name & " = '" & where_value & "'")
Return dt_service_provider_id.Rows(0).Item(0)
End Function
End Module
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Try
Dim filePath As String = UploadTextBox.Text
If Not String.IsNullOrEmpty(filePath) Then
If File.Exists(UploadTextBox.Text) Then
If Path.GetExtension(filePath) = ".xls" OrElse Path.GetExtension(filePath) = ".xlsx" Then
Try
If MessageBox.Show("Batch uploading process will delete all existing data. Are you sure you want to continue?", "Reminder", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
Dim stream = File.OpenRead(filePath)
Dim xdr As IExcelDataReader
If Path.GetExtension(filePath).EndsWith("xls") Then
xdr = ExcelReaderFactory.CreateBinaryReader(stream)
Else
xdr = ExcelReaderFactory.CreateOpenXmlReader(stream)
End If
stream.Close()
stream.Dispose()
xdr.IsFirstRowAsColumnNames = True
xls = xdr.AsDataSet()
xdr.Close()
xdr.Dispose()
Dim dt_count As Integer = 0
For Each dt As DataTable In xls.Tables
If Not dt Is Nothing Then
'MessageBox.Show(dt.TableName)
For Each dr As DataRow In dt.Rows
If (dt.TableName = "Service Provider Types") Then
Dim site_id As String = DT_Foreign_Key("site_id", "sites", "site_code", dr(2).ToString) 'Actually Im intentionally put error because i want to show the exception that i want
End If
Next
End If
Next
End If
Catch ex As IOException
MessageBox.Show(ex.Message & " Please make sure that the file is not curretly open.", "Upload file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End If
Else
MessageBox.Show("File does not exist.", "Upload file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Else
MessageBox.Show("Browse for the file to upload.", "Upload file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End If
Catch ex As Exception
MessageBox.Show(ex.Message, "Upload file", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
End Try
End Sub