what is the correct format for calling a db in access, with a 'folder with spaces'? - vb.net

Public connstring As String = "Provider = Microsoft.ACE.Oledb.12.0; Data Source = C:\Users\blablabla\Document\Visual Studio 2013\Project\rentalsystem\rental_db.accdb"
i placed this line of code under a module so i wont have to call it everytime. and this is the code that the code above is connected to...
Public Sub loadLVusers()
LVusers.Items.Clear()
Dim sqlcode As String = "select id_code, user_lastname, user_firstname, user_midname, user_username, user_password, user_email, user_privilege from tblUsers"
Dim sqlcomd As New OleDb.OleDbCommand(sqlcode)
sqlcomd.Connection = New OleDb.OleDbConnection(connstring)
sqlcomd.Connection.Open()
Dim DA As New OleDb.OleDbDataAdapter(sqlcomd)
Dim DS As New DataSet
DA.Fill(DS, "Pi")
If DS.Tables("Pi").Rows.Count > 0 Then
Dim Ic(100) As String
For r = 0 To DS.Tables("Pi").Rows.Count - 1
For c = 0 To DS.Tables("Pi").Columns.Count - 1
Ic(c) = DS.Tables("Pi").Rows(r)(c).ToString
Next
Dim LVI As New ListViewItem(Ic)
LVusers.Items.Add(LVI)
Next
End If
End Sub
now when the form/window that it is attached to loads, the form/window does not open. and then it highlights
sqlcomd.Connection = New OleDb.OleDbConnection(connstring)
so im guessing that has something to do with the file path format

Related

Problem with my usercontrol listview fill data from msaccess

I created listview ms activex by vb.net to my access database
i add that code in vb.net to fill my listview
Public Sub FillListview(ByVal CurrntDb As String)
Dim conn As OleDbConnection
Dim cmd As OleDbCommand
Dim da As OleDbDataAdapter
Dim ds As DataSet
Dim itemcoll(100) As String
ListView1.View = View.Details
ListView1.GridLines = True
ListView1.Items.Clear()
conn = New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source =" & CurrntDb)
Dim strQ As String = String.Empty
strQ = "SELECT Accounts.ID AS [م], Customers.Customer AS [العميل], Accounts.Debit AS [مدين], Accounts.Credit AS [دائن], Accounts.Dates AS [التاريخ], Accounts.Notes AS [البيان] FROM Accounts INNER JOIN Customers ON Accounts.Customer_ID = Customers.Customer_ID;"
cmd = New OleDbCommand(strQ, conn)
da = New OleDbDataAdapter(cmd)
ds = New DataSet
conn.Open()
da.Fill(ds)
Dim i As Integer = 0
Dim j As Integer = 0
' adding the columns in ListView
For i = 0 To ds.Tables(0).Columns.Count - 1
ListView1.Columns.Add(ds.Tables(0).Columns(i).ColumnName.ToString())
Next
'Now adding the Items in Listview
For i = 0 To ds.Tables(0).Rows.Count - 1
For j = 0 To ds.Tables(0).Columns.Count - 1
itemcoll(j) = ds.Tables(0).Rows(i)(j).ToString()
Next
Dim lvi As New ListViewItem(itemcoll)
ListView1.Items.Add(lvi)
ListView1.Refresh()
Next
conn.Close()
End Sub
and regestir it in my access database
and call it in access code by that code
Dim xlist As New AForTestListviewBySedo.UserControl1
Call xlist.FillListview("C:\Users\Elsayed\Desktop\New folder\mydb.accdb")
but they give me nothing not error but nothing
I tested that code in usercontrol by add button in it
button works good
What can I do ?
the solution is with my code in access to call record
i need to set object at first
the correct code is
Option Explicit
Option Compare Database
Public listv As MsAccessListviewACX1_00.UCBySedo
Public Sub LoadListview()
Set listv = Me.UCBySedo9.Object
listv.FillListview ("C:\Users\Elsayed\Desktop\New folder\mydb.accdb")
End Sub
Private Sub Command115_Click()
Call LoadListview
End Sub

How to put data of MS excel of one column inside array in vb.net

I have data on my MS.Excel spreadsheet which contain different column (Sn , Amount and tech id). I am trying to put all the data of tech id on tech id in array like :-
mydata = [43219 , 43220 , 43221 , 43222 ,43223 ,43224 , 43225 ]
My code of only one main processing function:-
Importing :-
Imports System.IO
Imports System.Data.OleDb
main processing function:-
Dim conString1 As String
Dim Mydata(200) As Integer
Dim connection As OleDbConnection
Dim adapter As OleDbDataAdapter
Private Sub LoadData(conStr As String)
con = New OleDbConnection(conStr)
Dim query As String = "SELECT * FROM [Sheet0$]"
adapter = New oleDbDataAdapter(query, connection)
'Putting data indide array
'For intCount = 0 To lengthofcolumn
'Mydata(intCount) = ?
'Next intCount
Debug.Print(adapter)
End Sub
Calling :-
conString1 = String.Format("Provider = Microsoft.Jet.OLEDB.4.0;Data Source = '{0}'; Extended Properties = Excel 8.0", 'F:\MicroTest\data\log.xlsx)')
LoadData(conString1)
I am a student , I am learning so please help ,I did't find this solution , Mostly I found solution of viewing excel data in datagrid
My test data was in B2:B8.
You will need to add the Reference: Microsoft Excel 14.0 Object Library
Dim oExcel As New Microsoft.Office.Interop.Excel.Application
oExcel.Workbooks.Open("C:\TEMP\test_data.xlsx")
Dim oSheet As Microsoft.Office.Interop.Excel.Worksheet = oExcel.Sheets(1)
' I would use list instead of an array.
Dim oTest As New List(Of String)
For Each oValue As String In oSheet.Range("B2:B8").Value2
oTest.Add(oValue)
Next
' Using an array
Dim oData(200) As Integer
Dim iCounter As Integer = 0
For Each oValue As String In oSheet.Range("B2:B8").Value2
oData(iCounter) = CType(oValue, Integer)
iCounter += 1
Next
oExcel.Quit()
I think your approach is good, accessing the file with OleDB and not openning an instance of Excel.
I used a DataReader and DataTable to collect and hold the data in memory.
The Using...End Using blocks ensure your objects that have a Dispose method are closed and disposed properly even if there is an error.
Private Sub LoadData()
Dim dt As New DataTable()
Dim conStr As String = "Your connection string"
Using con As New OleDbConnection(conStr)
Dim query As String = "SELECT * FROM [Sheet1$]"
Using cmd As New OleDbCommand(query, con)
con.Open()
Using dr As OleDbDataReader = cmd.ExecuteReader()
dt.Load(dr)
End Using
End Using
End Using
'The number of rows in the DataTable less the first 2 rows which are title and blank
'and subtract 1 because vb.net arrays are defined array(upper bound)
Dim arraySize As Integer = dt.Rows.Count - 3
Dim myData(arraySize) As Integer
Dim arrayIndex As Integer = 0
'Putting data indide array
For rowIndex As Integer = 2 To dt.Rows.Count - 1
myData(arrayIndex) = CInt(dt.Rows(rowIndex)(3)) '3 is the index of the TechID column
arrayIndex += 1
Next
'Checking the array - delete in final version
'I used i as a variable name because this is a very tiny
'loop and will be deleted eventually. Otherwise, I would
'have used a more descriptive name.
For Each i As Integer In myData
Debug.Print(i.ToString)
Next
End Sub

Find String with row, column number from Datatable in VB.Net

Im reading excel sheet having two section as input and output section and import that to two Datagridview one for input and another one for Output section in VB.net 2008 Windows application.
If i have 10 rows and 10 columns for input section, then in my 11th row i have a text like 'End of Input Data' like the same i have in 11th column.
So by checking this,if i get the row and column number of this string i can import the data in to two data grid views. i can import only these row and column data in input gridview.
Below is the code for reading excel sheet. I don't know how to find string in Datatable. Or is there any other way to do that?
Private Sub ImpGrid_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ImpGrid.Click
Dim xlApp As Excel.Application
Dim xlWorkBook As Excel.Workbook
Dim conStr As String, sheetName As String
Dim filePath As String = "C:\SIG.XLS"
Dim extension As String = ".xls"
conStr = String.Empty
conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & filePath & ";Extended Properties=""Excel 8.0;HDR=No;IMEX=1"""
Using con As New OleDbConnection(conStr)
Using cmd As New OleDbCommand()
Using oda As New OleDbDataAdapter()
Dim dt As New DataTable()
' cmd.CommandText = (Convert.ToString("SELECT * From [") & sheetName) + "]"
cmd.CommandText = "select * from [Sheet1$]"
cmd.Connection = con
con.Open()
oda.SelectCommand = cmd
oda.Fill(dt)
con.Close()
'Populate DataGridView.
Loggridview.DataSource = dt
End Using
End Using
End Using
End Sub
Here's a function that will returns a list of hits. Each result is a Tuple with Item1 being the rowId and Item2 being the columnId
Public Structure SearchHit
Public ReadOnly RowIndex As Integer
Public ReadOnly CellIndex As Integer
Public Sub New(rowIndex As Integer, cellIndex As Integer)
Me.RowIndex = rowIndex
Me.CellIndex = cellIndex
End Sub
End Structure
Public Function FindData(ByVal value As String, table As DataTable) As List(Of SearchHit)
Dim results As New List(Of SearchHit)
For rowId = 0 To table.Rows.Count - 1
Dim row = table.Rows(rowId)
For colId = 0 To table.Columns.Count - 1
Dim cellValue = row(colId)
If value.Equals(cellValue) Then
results.Add(New SearchHit(rowId, colId))
End If
Next
Next
Return results
End Function

Datatable columns and rows are in a wrong order

I'm trying to place some information from a spreadsheet using EPPLUS into a databale which will be exported to sql server afterwards.
The Problem I am having is that when I create the datatable columns, and then start inserting the rows, the end result shows a misalignment between row information and column headings.
Here is the code:
Sub ConvertPackage2DT()
Dim FILENAME As String = "C:\Data.xlsx"
Dim flInfo As New FileInfo(FILENAME)
_package = New ExcelPackage(flInfo) 'load the excel file
Dim strSheetName As String = "Radiology"
Dim Conn As SqlConnection = New SqlConnection("Data Source=dwsqlserver1\instance3;Initial Catalog=LocalDatasets_Dev;Integrated Security=True") 'connection to server end
Dim DTExport As New DataTable
If Not IsNothing(_package) Then
Dim _workbook As ExcelWorkbook = _package.Workbook
Dim _sheets As ExcelWorksheets = _workbook.Worksheets
Dim sh As ExcelWorksheet = _sheets.Item(strSheetName)
If _package.Workbook.Worksheets.Count <> 0 Then
For Each _sheet In _sheets
Dim SHEET_NAME As String = _sheet.Name
Dim tbl As New DataTable
Dim hasHeader = True ' adjust accordingly '
For ColNum = 1 To sh.Dimension.End.Column
tbl.Columns.Add(sh.Cells(1, ColNum).Value)
Next
System.Diagnostics.Debug.Print(tbl.Columns.Count)
For rowNum = 2 To sh.Dimension.End.Row
Dim v(sh.Dimension.End.Column - 1) As Object
For X = 0 To sh.Dimension.End.Column - 2
v(X) = sh.Cells(rowNum, X + 1).Value
Next
tbl.Rows.Add(v)
'tbl.Rows.Add(wsRow.Select(Function(cell) cell.Text).ToArray)
Next
Conn.Open()
Using bulkcopy As SqlBulkCopy = New SqlBulkCopy(Conn)
bulkcopy.DestinationTableName = "[LocalDatasets_Dev].[dbo].[" & GetNewTableName(6) & "]"
bulkcopy.WriteToServer(tbl)
End Using
Conn.Close()
MsgBox("Complete")
Exit Sub
Next _sheet
End If
End If
End Sub
End Module
Can anyone help? Thank you kindly

vb.net use string as a previously created object instance

Not sure exactly what I need to do to make this work, so my description may be lacking at first. Essentially I am writing a program launcher that recreates itself each time on load. It pulls the data regarding the tabs and buttons from an SQLite database and builds itself dynamically at run time. I get my problem when I pass the tab name through to the function that creates the buttons. I need the name to pull the right set of buttons from the database and I then tried to use the name to place the buttons on the right tab when I create them, but the debugger calls it a null reference because it doesn't point correctly to the tabpage that I'm trying to make it point to (at least that is what I'm guessing). Any ideas on how to make this work right?
Private Sub CreateTabs()
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;"
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "SELECT title FROM tabs"
Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
Dim Tabs(25) As String
Dim c As Integer = 1
While SQLreader.Read()
Tabs(c) = SQLreader(0)
c = c + 1
End While
SQLcommand.Dispose()
SQLconnect.Close()
For i = 1 To UBound(Tabs)
If Tabs(i) <> "" Then
Launcher.TabPages.Add(Tabs(i))
CreateButtons(Tabs(i))
End If
Next
End Sub
Private Sub CreateButtons(ByVal tab)
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;"
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "SELECT id,name,path FROM buttons WHERE tab = '" & tab & "'"
Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
While SQLreader.Read()
For i = 1 To 9
Dim NewButton(i) As Button
If Not SQLreader(2) Is System.DBNull.Value Then
Dim myIcon As System.Drawing.Icon = Icon.ExtractAssociatedIcon(SQLreader(2))
End If
Dim toolTip1 As ToolTip = New System.Windows.Forms.ToolTip(Me.components)
Me.Controls(tab).tabpages.add(NewButton(i)) '<--this causes my problem
'NewButton(i).Width = 32
'NewButton(i).Height = 32
'NewButton(i).Text = i
'NewButton(i).Image = myIcon.ToBitmap
'If Not SQLreader(1) Is System.DBNull.Value Then
'toolTip1.SetToolTip(NewButton(i), SQLreader(1))
'toolTip1.Active = True
'End If
Next
End While
SQLcommand.Dispose()
SQLconnect.Close()
End Sub
I think you've got two problems here:
You're trying to find the tab control by the tab name. You should instead find the tab control itself (or pass it in) and find the name of the tab inside that control. Otherwise you're trying to add a button to a collection of tab pages, instead of to a specific tab.
You're never actually creating a new button. You're creating an array of buttons for every iteration of the loop, but not a new actual Button object. Admittedly that doesn't jibe with your suspicion that it's due to tab pages, but it's certainly a problem...
I suspect you want this as your loop (where tabControl is the TabControl passed in):
While SQLreader.Read()
For i = 1 To 9
If Not SQLreader(2) Is System.DBNull.Value Then
Dim myIcon As Icon = Icon.ExtractAssociatedIcon(SQLreader(2))
End If
Dim toolTip1 As ToolTip = New ToolTip(Me.components)
Dim NewButton As Button = New Button
NewButton.Width = 32
NewButton.Height = 32
NewButton.Text = i
NewButton.Image = myIcon.ToBitmap
tabControl.TabPages(tab).Controls.Add(NewButton)
If Not SQLreader(1) Is System.DBNull.Value Then
toolTip1.SetToolTip(NewButton, SQLreader(1))
toolTip1.Active = True
End If
Next
End While
Hopefully that's right - my VB.NET isn't great...
I'm surprised your original code compiled though - do you have Option Strict on?
You should also use a Using statement for your command and connection so they get disposed even when an exception occurs.
Thanks for the help, but I found my answer. Apparently I just needed to reference the index of the tab like so:
Private Sub CreateTabs()
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;"
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "SELECT title FROM tabs"
Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
Dim Tabs(25) As String
Dim c As Integer = 1
While SQLreader.Read()
Tabs(c) = SQLreader(0)
c = c + 1
End While
SQLcommand.Dispose()
SQLconnect.Close()
For i = 1 To UBound(Tabs)
If Tabs(i) <> "" Then
Launcher.TabPages.Add(Tabs(i))
CreateButtons(Tabs(i), i - 1)
End If
Next
End Sub
Private Sub CreateButtons(ByVal tab, ByVal TabIndex)
Dim SQLconnect As New SQLite.SQLiteConnection()
Dim SQLcommand As SQLiteCommand
SQLconnect.ConnectionString = "Data Source=" & sPath & "\dock.db;"
SQLconnect.Open()
SQLcommand = SQLconnect.CreateCommand
SQLcommand.CommandText = "SELECT id,name,path FROM buttons WHERE tab = '" & tab & "'"
Dim SQLreader As SQLiteDataReader = SQLcommand.ExecuteReader()
While SQLreader.Read()
For i = 1 To 9
Dim NewButton As New Button
Launcher.TabPages.Item(TabIndex).Controls.add(NewButton)
NewButton.Width = 32
NewButton.Height = 32
NewButton.Location = New Point(10 + (SQLreader(0) * 32) + 10, 10)
NewButton.Text = SQLreader(0)
If Not SQLreader(2) Is System.DBNull.Value Then
Dim toolTip1 As ToolTip = New System.Windows.Forms.ToolTip(Me.components)
Dim myIcon As System.Drawing.Icon = Icon.ExtractAssociatedIcon(SQLreader(2))
NewButton.Image = myIcon.ToBitmap
toolTip1.SetToolTip(NewButton, SQLreader(1))
toolTip1.Active = True
End If
Next
End While
SQLcommand.Dispose()
SQLconnect.Close()
End Sub