Empty path name is not legal in MemoryStream in vb.net - arrays

What is the solution if there is an error "Empty path name is not legal" because in the database column Filename6 is empty and also what is the solution if between the filename1 to filename6 fields are not found in the image folder?.
Thanks
jack
Private Sub GridView1_RowCellClick(sender As Object, e As RowCellClickEventArgs) Handles GridView1.RowCellClick
Dim view As GridView = TryCast(sender, GridView)
Dim SUBFOLDERP As String = view.GetFocusedRowCellValue("SUBFOLDERP").ToString
Dim Filename1 As String = view.GetFocusedRowCellValue("Filename1").ToString
Dim Filename2 As String = view.GetFocusedRowCellValue("Filename2").ToString
Dim Filename3 As String = view.GetFocusedRowCellValue("Filename3").ToString
Dim Filename4 As String = view.GetFocusedRowCellValue("Filename4").ToString
Dim Filename5 As String = view.GetFocusedRowCellValue("Filename5").ToString
Dim Filename6 As String = view.GetFocusedRowCellValue("Filename6").ToString
If e.Column.FieldName = "Code" Then
Dim Code As String = view.GetRowCellValue(e.RowHandle, e.Column).ToString
Dim filePath1 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename1, False)
Dim filePath2 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename2, False)
Dim filePath3 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename3, False)
Dim filePath4 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename4, False)
Dim filePath5 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename5, False)
Dim filePath6 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename6, False)
PictureEdit1.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath1)), True, False)
PictureEdit2.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath2)), True, False)
PictureEdit3.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath3)), True, False)
PictureEdit4.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath4)), True, False)
PictureEdit5.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath5)), True, False)
PictureEdit6.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath6)), True, False) 'error this line code
End If
End Sub

Private Sub GridView1_RowCellClick(sender As Object, e As RowCellClickEventArgs) Handles GridView1.RowCellClick
Dim view As GridView = TryCast(sender, GridView)
Dim SUBFOLDERP As String = view.GetFocusedRowCellValue("SUBFOLDERP").ToString
Dim Filename1 As String = view.GetFocusedRowCellValue("Filename1").ToString
Dim Filename2 As String = view.GetFocusedRowCellValue("Filename2").ToString
Dim Filename3 As String = view.GetFocusedRowCellValue("Filename3").ToString
Dim Filename4 As String = view.GetFocusedRowCellValue("Filename4").ToString
Dim Filename5 As String = view.GetFocusedRowCellValue("Filename5").ToString
Dim Filename6 As String = view.GetFocusedRowCellValue("Filename6").ToString
If e.Column.FieldName = "Code" Then
Dim Code As String = view.GetRowCellValue(e.RowHandle, e.Column).ToString
Dim filePath1 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename1, False)
Dim filePath2 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename2, False)
Dim filePath3 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename3, False)
Dim filePath4 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename4, False)
Dim filePath5 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename5, False)
Dim filePath6 As String = DevExpress.Utils.FilesHelper.FindingFileName(parentpathimage & "\" & SUBFOLDERP & "\", Filename6, False)
PictureEdit1.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath1)), True, False)
PictureEdit2.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath2)), True, False)
PictureEdit3.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath3)), True, False)
PictureEdit4.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath4)), True, False)
PictureEdit5.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath5)), True, False)
**If Not String.IsNullOrWhiteSpace(filePath6) Then**
PictureEdit6.Image = Image.FromStream(New MemoryStream(File.ReadAllBytes(filePath6)), True, False)
**End If**
End If
End Sub

Related

WPF - controls .IsEnabled not showing in UI second time a grid is loaded

We use ToggleEdit (to enable/disable controls) when navigating to a new page in a new tab. This time the new tab is created in an existing page. It works as it should the first time the grid is loaded, but then fails the second time. Have tried lots of different methods but still the same result
Double clicking a row in a DataGrid causes a new tab to be opened
Private Sub Alarm_Edit(sender As Object, e As RoutedEventArgs)
Try
e.Handled = True
IsNewRecord = False
Dim DGV As DGVx = PersonalSchedule_Grid.FindName("Schedule_AlarmsDGV")
If DGV.SelectedItems.Count = 1 Then
Dim row As System.Data.DataRowView = DGV.SelectedItems(0)
Alarm_ID = row("ID")
Dim vName As String = row("Subject")
Dim vTab As STC_Tabx = PersonalSchedule_Grid.FindName(TabName)
Dim TabControl As STCx = PersonalSchedule_Grid.FindName("Alarm_TabControl")
If Not vTab Is Nothing Then
vTab.Close()
End If
Dim MCFrame As New Frame
Dim MCTab As New STC_Tabx
With MCTab
.Name = TabName
.Header = " " & Left(vName, 20) & " "
.ImageSource = ReturnImageAsString("Edit.png", 16)
.CloseButtonVisibility = DevComponents.WpfEditors.eTabCloseButtonVisibility.Visible
.TabToolTip = "View or edit the " & vName & " record"
' .Content = MCFrame
.Content = AlarmGrid()
End With
RemoveHandler MCTab.Closing, AddressOf TabControl_TabClosing
AddHandler MCTab.Closing, AddressOf TabControl_TabClosing
RegisterControl(PersonalSchedule_Grid, MCTab)
TabControl.Items.Add(MCTab)
' MCFrame.NavigationService.Navigate(AlarmPage)
LoadedTabs(TabName)
MCTab.IsSelected = True
End If
Catch ex As Exception
EmailError(ex)
End Try
End Sub
... and this is added to the tab
Private Function AlarmGrid() As Grid
Try
Dim MainGrid As New Grid
MainGrid.Children.Clear()
Dim vGrid As New Grid
vGrid.ColumnDefinitions.Clear()
vGrid.RowDefinitions.Clear()
vGrid.Children.Clear()
Dim SV As New ScrollViewer
With SV
.Name = "Alarm_SV"
.Content = vGrid
.VerticalScrollBarVisibility = ScrollBarVisibility.Auto
End With '
RegisterControl(PersonalSchedule_Grid, SV)
MainGrid.Children.Add(SV)
For i As Integer = 0 To 5
Dim vRow As New RowDefinition
If i = 4 Then
vRow.Height = New GridLength(35, GridUnitType.Star)
Else
vRow.Height = New GridLength(35)
End If
vGrid.RowDefinitions.Add(vRow)
Next
For i As Integer = 0 To 1
Dim vCol As New ColumnDefinition
If i = 0 Then
vCol.Width = New GridLength(120)
ElseIf i = 1 Then
vCol.Width = New GridLength(200, GridUnitType.Star)
End If
vGrid.ColumnDefinitions.Add(vCol)
Next
'Date | Time | Subject | Details
Dim vToolBar As New ToolBar
Grid.SetColumn(vToolBar, 0)
Grid.SetColumnSpan(vToolBar, 2)
Grid.SetRow(vToolBar, 0)
vGrid.Children.Add(vToolBar)
Dim EditButton As New Button
With EditButton
.Content = ReturnToolBarImage("Edit.png")
.Name = "Alarm_EditButton"
.ToolTip = "Edit the record"
If IsNewRecord = True Then
.Visibility = Visibility.Collapsed
End If
End With
RegisterControl(PersonalSchedule_Grid, EditButton)
vToolBar.Items.Add(EditButton)
Dim EditTS As New Separator
With EditTS
.Name = "Alarm_EditTS"
If IsNewRecord = True Then
.Visibility = Visibility.Collapsed
End If
End With
RegisterControl(PersonalSchedule_Grid, EditTS)
vToolBar.Items.Add(EditTS)
Dim SaveUpdateButton As New Button
With SaveUpdateButton
.Name = "Alarm_SaveUpdateButton"
If IsNewRecord = True Then
.Content = ReturnToolBarImage("Record_Insert.png")
.ToolTip = "Save the record"
.IsEnabled = True
Else
.Content = ReturnToolBarImage("Record_Update.png")
.ToolTip = "Update the record"
.IsEnabled = False
End If
End With
RegisterControl(PersonalSchedule_Grid, SaveUpdateButton)
vToolBar.Items.Add(SaveUpdateButton)
vToolBar.Items.Add(TS_Separator)
Dim EnabledCB As New CBx
With EnabledCB
.Content = "Enabled"
.ToolTip = "The alarm is enabled"
.Name = "Alarm_EnabledCB"
.IsNewRecord = IsNewRecord
If IsNewRecord = True Then
.Visibility = Visibility.Collapsed
End If
End With
RegisterControl(PersonalSchedule_Grid, EnabledCB)
vToolBar.Items.Add(EnabledCB)
If IsNewRecord = False Then
vToolBar.Items.Add(TS_Separator)
End If
Dim DateLB As New TextLabel
With DateLB
.Text = "Date"
End With
Grid.SetColumn(DateLB, 0)
Grid.SetRow(DateLB, 1)
vGrid.Children.Add(DateLB)
Dim DateTB As New DateTBx
With DateTB
.IsNewRecord = IsNewRecord
.Value = Today
.Name = "Alarm_DateTB"
End With
RegisterControl(PersonalSchedule_Grid, DateTB)
Grid.SetColumn(DateTB, 1)
Grid.SetRow(DateTB, 1)
vGrid.Children.Add(DateTB)
Dim TimeLB As New TextLabel
With TimeLB
.Text = "Time"
End With
Grid.SetColumn(TimeLB, 0)
Grid.SetRow(TimeLB, 2)
vGrid.Children.Add(TimeLB)
Dim TimeTB As New TimeTBx
With TimeTB
.Value = Format(DateTime.Now, "HH:mm")
.IsNewRecord = IsNewRecord
.Name = "Alarm_TimeTB"
End With
RegisterControl(PersonalSchedule_Grid, TimeTB)
Grid.SetColumn(TimeTB, 1)
Grid.SetRow(TimeTB, 2)
vGrid.Children.Add(TimeTB)
Dim SubjectLB As New TextLabel
With SubjectLB
.Text = "Subject"
End With
Grid.SetColumn(SubjectLB, 0)
Grid.SetRow(SubjectLB, 3)
vGrid.Children.Add(SubjectLB)
Dim SubjectTB As New TBx
With SubjectTB
.Width = 300
.IsNewRecord = IsNewRecord
.Name = "Alarm_SubjectTB"
End With
RegisterControl(PersonalSchedule_Grid, SubjectTB)
Grid.SetColumn(SubjectTB, 1)
Grid.SetRow(SubjectTB, 3)
vGrid.Children.Add(SubjectTB)
Dim DetailsLB As New TextBlock
With DetailsLB
.Text = "Details"
.VerticalAlignment = VerticalAlignment.Top
.HorizontalAlignment = Windows.HorizontalAlignment.Left
.Margin = New Thickness(10, 10, 0, 0)
End With
Grid.SetColumn(DetailsLB, 0)
Grid.SetRow(DetailsLB, 4)
vGrid.Children.Add(DetailsLB)
Dim DetailsTB As New MultiLineLargeTBx
With DetailsTB
.Name = "Alarm_DetailsTB"
.IsNewRecord = IsNewRecord
End With
RegisterControl(PersonalSchedule_Grid, DetailsTB)
Grid.SetColumn(DetailsTB, 1)
Grid.SetRow(DetailsTB, 4)
vGrid.Children.Add(DetailsTB)
If IsNewRecord = False Then
Dim vTime As TimeSpan = Nothing
Dim vDate As Date = Nothing
Dim vSubject As String = ""
Dim vDetails As String = ""
Dim IsAlarmSet As Boolean = False
Using vService As New Service1Client
strSQL = "Select Alarm_Date, Alarm_Time, Alarm_Subject, Alarm_Content, Alarm_Set FROM Alarm_Info WHERE Alarm_ID = " & Alarm_ID
Using DS As DataSet = vService.ReturnDataSetHAS(strSQL)
For Each Row As DataRow In DS.Tables(0).Rows
vTime = Row("Alarm_Time")
vDate = LocalDateFormat(Row("Alarm_Date"))
vSubject = ReturnText(Row("Alarm_Subject"))
vDetails = ReturnText(Row("Alarm_Content"))
Dim AlarmSet As Integer = Row("Alarm_Set")
If AlarmSet = 1 Then
IsAlarmSet = True
End If
Next
End Using
End Using
Dim TimeString As String = ReturnFormattedTime(vTime, False, True)
TimeTB.Value = TimeString
DateTB.Value = vDate
SubjectTB.Text = vSubject
DetailsTB.Text = vSubject
EnabledCB.IsChecked = IsAlarmSet
RemoveHandler EditButton.Click, AddressOf ToggleEdit_Click
RemoveHandler SV.MouseDoubleClick, AddressOf ToggleEdit_Click
RemoveHandler SaveUpdateButton.Click, AddressOf DB_Update
AddHandler EditButton.Click, AddressOf ToggleEdit_Click
AddHandler SV.MouseDoubleClick, AddressOf ToggleEdit_Click
AddHandler SaveUpdateButton.Click, AddressOf DB_Update
Else
RemoveHandler SaveUpdateButton.Click, AddressOf DB_Insert
AddHandler SaveUpdateButton.Click, AddressOf DB_Insert
End If
Return MainGrid
Catch ex As Exception
EmailError(ex)
Return Nothing
End Try
End Function
...and clicking the edit button or double clicking the ScrollViewer causes this to run
Private Sub ToggleEdit_Click(sender As Object, e As RoutedEventArgs)
Try
ToggleEdit()
Catch ex As Exception
EmailError(ex)
End Try
End Sub
Private Sub ToggleEdit()
Try
Dim SV As ScrollViewer = PersonalSchedule_Grid.FindName("Alarm_SV")
Dim DateTB As DateTBx = PersonalSchedule_Grid.FindName("Alarm_DateTB")
Dim TimeTB As TimeTBx = PersonalSchedule_Grid.FindName("Alarm_TimeTB")
Dim SubjectTB As TBx = PersonalSchedule_Grid.FindName("Alarm_SubjectTB")
Dim DetailsTB As MultiLineLargeTBx = PersonalSchedule_Grid.FindName("Alarm_DetailsTB")
Dim EnabledCB As CBx = PersonalSchedule_Grid.FindName("Alarm_EnabledCB")
Dim UpdateButton As Button = PersonalSchedule_Grid.FindName("Alarm_SaveUpdateButton")
If UpdateButton.IsEnabled = False Then
UpdateButton.IsEnabled = True
DateTB.IsNewRecord = True
TimeTB.IsNewRecord = True
SubjectTB.IsNewRecord = True
DetailsTB.IsNewRecord = True
EnabledCB.IsNewRecord = True
Else
UpdateButton.IsEnabled = False
DateTB.IsNewRecord = False
TimeTB.IsNewRecord = False
SubjectTB.IsNewRecord = False
DetailsTB.IsNewRecord = False
EnabledCB.IsNewRecord = False
End If
Catch ex As Exception
EmailError(ex)
End Try
End Sub
That should change the update button .IsEnabled to true (and the other controls .IsReadOnly) and back to false if clicked again.
It must be something very basic about WPF that I have missed for years, but running through the code in debug everything is firing as it should - it's changing the value of the control but it's not reflecting in the UI. Tried UpdateLayout and a host of other possible solutions, but...
Any pointers would be really helpful
Thanks

Datagridview INSERT/SELECT/DELETE fail

I'm trying to create a master-detail form with datagridviews all in code. The SELECT/fill part is working fine, but I am having trouble with UPDATE/INSERT/DELETE (which should happen automatically when the form is closed). The following code gives the error:
Update requires the UpdateCommand to have a connection object. The
Connection property of the UpdateCommand has not been initialized.
on the .Update line.
I tried moving the connection out of the GetData() procedure, but that was not the answer.
What should I be doing?
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class TestMe2
Private lblSelector As New Label
Private cbSelector As New ComboBox
Private bsSelector As New BindingSource
Private daMaster As New OleDbDataAdapter
Private dgMaster As New DataGrid
Private bsMaster As New BindingSource
Private dgLookup As New DataGrid
Private bsLookups As New BindingSource
Private dsGFF As New DataSet
Private cnn As New OleDbConnection
Public Sub New()
' This call is required by the designer.
InitializeComponent()
' Add any initialization after the InitializeComponent() call.
Me.Controls.Add(lblSelector)
lblSelector.Location = New System.Drawing.Point(0, 0)
lblSelector.Text = "FileType"
Me.Controls.Add(cbSelector)
cbSelector.Location = New System.Drawing.Point(Me.lblSelector.Width, 0)
Me.Controls.Add(dgMaster)
dgMaster.Location = New System.Drawing.Point(0, cbSelector.Height)
dgMaster.Height = 300
dgMaster.Width = 2000
Me.Controls.Add(dgLookup)
dgLookup.Location = New System.Drawing.Point(0, cbSelector.Height + dgMaster.Height)
dgLookup.Height = 100
dgLookup.Width = 1000
End Sub
Private Sub TestMe2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
bsSelector.DataSource = dsGFF
cbSelector.DataSource = bsSelector
bsMaster.DataSource = dsGFF
dgMaster.DataSource = bsMaster
bsLookups.DataSource = dsGFF
dgLookup.DataSource = bsLookups
GetData()
bsMaster.DataSource = bsSelector
bsMaster.DataMember = "FileTypesToGFFTranslator"
bsLookups.DataSource = bsMaster
bsLookups.DataMember = "GFFTranslatorToTranslations"
'dgMaster.autoresizecolumns()
End Sub
Private Sub GetData()
Dim command As New OleDbCommand
Dim parameter As New OleDbParameter
' where's the data?
Dim cnnString As String = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\GFFTranslator.accdb"
cnn = New OleDb.OleDbConnection(cnnString)
' set up up the selector
Dim daSelector As New OleDbDataAdapter("SELECT FileType FROM vwFileTypeSelection", cnn)
daSelector.Fill(dsGFF, "vwFileTypeSelection")
bsSelector.DataSource = dsGFF
bsSelector.DataMember = "vwFileTypeSelection"
cbSelector.ValueMember = "FileType"
cbSelector.DisplayMember = "FileType"
daSelector.Fill(dsGFF)
' set up the master
daMaster = New OleDbDataAdapter("SELECT * FROM GFFTranslator", cnn)
daMaster.Fill(dsGFF, "GFFTranslator")
daMaster.UpdateCommand = New OleDbCommand("UPDATE GFFTRanslator" & _
" SET FileType = ?" & _
" , FieldPosition = ?" & _
" , FieldType = ?" & _
" , StartRepeatingSection = ?" & _
" , FileTypeIdentifier = ?" & _
" , Flag = ?" & _
" , DataStart = ?" & _
" , DataLength = ?" & _
" , NextLine = ?" & _
" , Lookup = ?" & _
" , Title = ?" & _
" , ExtraInfo = ?" & _
" WHERE FileType = ?" & _
" AND FieldPosition = ? ;")
daMaster.UpdateCommand.Parameters.Add("#FileType", OleDbType.VarChar, 255, "FileType")
daMaster.UpdateCommand.Parameters.Add("#FieldPosition", OleDbType.Integer, 4, "FieldPosition")
daMaster.UpdateCommand.Parameters.Add("#FieldType", OleDbType.VarChar, 255, "FieldType")
daMaster.UpdateCommand.Parameters.Add("#StartRepeatingSection", OleDbType.Boolean, 1, "StartRepeatingSection")
daMaster.UpdateCommand.Parameters.Add("#FileTypeIdentifier", OleDbType.Boolean, 1, "FileTypeIdentifier")
daMaster.UpdateCommand.Parameters.Add("#Flag", OleDbType.Boolean, 1, "Flag")
daMaster.UpdateCommand.Parameters.Add("#DataStart", OleDbType.Integer, 5, "DataStart")
daMaster.UpdateCommand.Parameters.Add("#DataLength", OleDbType.Integer, 5, "DataLength")
daMaster.UpdateCommand.Parameters.Add("#NextLine", OleDbType.Boolean, 1, "NextLine")
daMaster.UpdateCommand.Parameters.Add("#Lookup", OleDbType.Boolean, 1, "Lookup")
daMaster.UpdateCommand.Parameters.Add("#Title", OleDbType.VarChar, 255, "Title")
daMaster.UpdateCommand.Parameters.Add("#ExtraInfo", OleDbType.VarChar, 255, "ExtraInfo")
' set up the lookup
Dim daLookup As New OleDbDataAdapter("SELECT * FROM Translations", cnn)
daLookup.Fill(dsGFF, "Translations")
' link everything together
Try
Dim drMaster As New DataRelation("FileTypesToGFFTranslator" _
, dsGFF.Tables("vwFileTypeSelection").Columns("FileType") _
, dsGFF.Tables("GFFTranslator").Columns("FileType"))
dsGFF.Relations.Add(drMaster)
Dim dcMaster As DataColumn() = New DataColumn() {dsGFF.Tables("GFFTranslator").Columns("FileType"), dsGFF.Tables("GFFTranslator").Columns("FieldType")}
Dim dcLookup As DataColumn() = New DataColumn() {dsGFF.Tables("Translations").Columns("FileType"), dsGFF.Tables("Translations").Columns("FieldType")}
Dim drLookup As New DataRelation("GFFTranslatorToTranslations" _
, dcMaster _
, dcLookup)
dsGFF.Relations.Add(drLookup)
Catch ex As Exception
MessageBox.Show("Error: " & ex.Message)
End Try
End Sub
Protected Overrides Sub Finalize()
Me.Validate()
Me.daMaster.Update(dsGFF.Tables("GFFTranslator"))
Me.dsGFF.AcceptChanges()
MyBase.Finalize()
End Sub
End Class
In the code below, you are passing a connection object to your SELECT command.
' set up the master
daMaster = New OleDbDataAdapter("SELECT * FROM GFFTranslator", cnn)
Once the .Fill() method is done, your connection object is closed.
daMaster.Fill(dsGFF, "GFFTranslator")
You are now trying to create an UPDATE command but you aren't passing it a connection object for it to work with.
daMaster.UpdateCommand = New OleDbCommand("UPDATE GFFTRanslator" & _
" SET FileType = ?" & _
" , FieldPosition = ?" & _
" , FieldType = ?" & _
" , StartRepeatingSection = ?" & _
" , FileTypeIdentifier = ?" & _
" , Flag = ?" & _
" , DataStart = ?" & _
" , DataLength = ?" & _
" , NextLine = ?" & _
" , Lookup = ?" & _
" , Title = ?" & _
" , ExtraInfo = ?" & _
" WHERE FileType = ?" & _
" AND FieldPosition = ? ;")
daMaster.UpdateCommand.Parameters.Add("#FileType", OleDbType.VarChar, 255, "FileType")
daMaster.UpdateCommand.Parameters.Add("#FieldPosition", OleDbType.Integer, 4, "FieldPosition")
daMaster.UpdateCommand.Parameters.Add("#FieldType", OleDbType.VarChar, 255, "FieldType")
daMaster.UpdateCommand.Parameters.Add("#StartRepeatingSection", OleDbType.Boolean, 1, "StartRepeatingSection")
daMaster.UpdateCommand.Parameters.Add("#FileTypeIdentifier", OleDbType.Boolean, 1, "FileTypeIdentifier")
daMaster.UpdateCommand.Parameters.Add("#Flag", OleDbType.Boolean, 1, "Flag")
daMaster.UpdateCommand.Parameters.Add("#DataStart", OleDbType.Integer, 5, "DataStart")
daMaster.UpdateCommand.Parameters.Add("#DataLength", OleDbType.Integer, 5, "DataLength")
daMaster.UpdateCommand.Parameters.Add("#NextLine", OleDbType.Boolean, 1, "NextLine")
daMaster.UpdateCommand.Parameters.Add("#Lookup", OleDbType.Boolean, 1, "Lookup")
daMaster.UpdateCommand.Parameters.Add("#Title", OleDbType.VarChar, 255, "Title")
daMaster.UpdateCommand.Parameters.Add("#ExtraInfo", OleDbType.VarChar, 255, "ExtraInfo")
So, to fix this, do the following:
" AND FieldPosition = ? ;", cnn) // Pass your connection object here.

"The parameter is incorrect" in crystal report viewer

I am dynamically creating a crystal report. When I run the program and click on a button, I get "Parameter is incorrect" information box. How can I resolve this?
Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.ReportSource
Imports CrystalDecisions.Shared
Imports CrystalDecisions.Windows.Forms
Imports System.Data.SqlClient
Imports System.Data.OleDb
Public Class Form1
Dim objRpt As CrystalReport1
Dim con As New SqlConnection
Private Function CreateSelectQueryAndParameters() As String
Dim paramFields As ParameterFields
Dim paramField As ParameterField
Dim paramDiscreteValue As ParameterDiscreteValue
paramFields = New ParameterFields
Dim query As String = "SELECT "
Dim columnNo As Integer = 0
If CheckBox1.Checked Then
columnNo = columnNo + 1
query = query.Insert(query.Length, "pcode as Column" + columnNo.ToString())
paramField = New ParameterField()
paramField.Name = "col" + columnNo.ToString()
paramDiscreteValue = New ParameterDiscreteValue()
paramDiscreteValue.Value = "Property Code"
paramField.CurrentValues.Add(paramDiscreteValue)
paramFields.Add(paramField)
End If
If CheckBox2.Checked Then
columnNo = columnNo + 1
If query.Contains("Column") Then
query = query.Insert(query.Length, ", ")
End If
query = query.Insert(query.Length, "pname as Column" + columnNo.ToString())
paramField = New ParameterField()
paramField.Name = "col" + columnNo.ToString()
paramDiscreteValue = New ParameterDiscreteValue()
paramDiscreteValue.Value = "Property Name"
paramField.CurrentValues.Add(paramDiscreteValue)
paramFields.Add(paramField)
End If
For i As Integer = columnNo To 2
columnNo = columnNo + 1
paramField = New ParameterField()
paramField.Name = "col" + columnNo.ToString()
paramDiscreteValue = New ParameterDiscreteValue()
paramDiscreteValue.Value = ""
paramField.CurrentValues.Add(paramDiscreteValue)
paramFields.Add(paramField)
Next
CrystalReportViewer1.ParameterFieldInfo = paramFields
query += " FROM propdb"
TextBox1.Text = query
Return query
End Function
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cnn As SqlConnection
Dim connString As String
Dim query As String
connString = "Data Source=RANJITHA-PC;Initial Catalog=hrmdb;Integrated Security=True"
cnn = New SqlConnection(connString)
cnn.Open()
query = CreateSelectQueryAndParameters()
If Not query.Contains("Column") Then
MessageBox.Show("No selection to display!")
Return
End If
Dim adepter As New SqlDataAdapter(query, cnn)
Dim Ds As New DataSet2
adepter.Fill(Ds, "propdb")
MsgBox(Ds.Tables(0).Rows.Count)
cnn.Close()
objRpt = New CrystalReport1()
objRpt.SetDataSource(Ds.Tables(0))
Me.CrystalReportViewer1.ReportSource = objRpt
Me.CrystalReportViewer1.Refresh()
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub End Class
Seems you have a issue in you for-loop, should changed to
For i As Integer = columnNo+1 To 2
paramField = New ParameterField()
paramField.Name = "col" + i.ToString()
paramDiscreteValue = New ParameterDiscreteValue()
paramDiscreteValue.Value = ""
paramField.CurrentValues.Add(paramDiscreteValue)
paramFields.Add(paramField)
Next
And put a break pint in CrystalReportViewer1.ParameterFieldInfo = paramFields and check the parameter names

Creating new Table

How could I add a new table in my access database using vb.net code.
I am supposed to write a code that when i am going to add a new file it will be having its own table in my data base.
here is my code:
Public Class Form3
Dim con As New OleDb.OleDbConnection
Dim dbprovider As String
Dim dbsource As String
Dim ds As New DataSet
Dim da As New OleDb.OleDbDataAdapter
Dim sql As String
Dim inc As Integer
Dim intDB_ID_Selected As Integer
Dim ocmd As OleDb.OleDbCommand
Dim odatareader As OleDb.OleDbDataReader
Dim state As Integer
Dim rownumber As Integer
Dim intCurIndex As Integer
Private Sub Form3_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'SofEngdbDataSet.DentalRecords' table. You can move, or remove it, as needed.
dbprovider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
dbsource = "Data Source = C:\Users\AllyzaJane\Desktop\Finals\SofEngdb.mdb"
con.ConnectionString = dbprovider & dbsource
con.Open()
sql = "SELECT * FROM DentalRecords"
da = New OleDb.OleDbDataAdapter(sql, con)
da.Fill(ds, "SofEngdb")
tb_fname.ReadOnly = True
tb_minitial.ReadOnly = True
tb_lname.ReadOnly = True
tb_add.ReadOnly = True
tb_num.ReadOnly = True
rtb_remarks.ReadOnly = True
tb_search.ReadOnly = False
b_add.Enabled = True
b_cancel.Enabled = True
b_save.Enabled = False
b_search.Enabled = True
b_delete.Enabled = False
b_update.Enabled = False
End Sub
Private Sub b_cancel_Click(sender As System.Object, e As System.EventArgs) Handles b_cancel.Click
b_save.Enabled = False
b_add.Enabled = True
b_update.Enabled = False
b_delete.Enabled = False
b_search.Enabled = True
MsgBox("Cancelled")
tb_search.ReadOnly = False
cleartextvalue()
End Sub
Private Function DentalRecordsTableAdapter() As Object
Throw New NotImplementedException
End Function
Private Function SampledDataSet() As Object
Throw New NotImplementedException
End Function
Private Sub b_update_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles b_update.Click
state = 0
tb_fname.ReadOnly = False
tb_fname.ReadOnly = False
tb_minitial.ReadOnly = False
tb_lname.ReadOnly = False
tb_num.ReadOnly = False
tb_add.ReadOnly = False
rtb_remarks.ReadOnly = False
tb_search.ReadOnly = True
b_add.Enabled = False
b_save.Enabled = True
b_cancel.Enabled = True
b_delete.Enabled = False
b_search.Enabled = False
b_update.Enabled = False
End Sub
Private Sub b_add_Click(sender As System.Object, e As System.EventArgs) Handles b_add.Click
state = 1
b_add.Enabled = False
b_search.Enabled = False
b_delete.Enabled = False
b_update.Enabled = False
b_save.Enabled = True
b_cancel.Enabled = True
tb_fname.ReadOnly = False
tb_minitial.ReadOnly = False
tb_lname.ReadOnly = False
tb_num.ReadOnly = False
tb_add.ReadOnly = False
rtb_remarks.ReadOnly = False
cleartextvalue()
End Sub
Private Sub b_save_Click(sender As System.Object, e As System.EventArgs) Handles b_save.Click
b_add.Enabled = True
b_save.Enabled = False
b_search.Enabled = True
' for ADD
If state = 1 Then
If tb_fname.Text = "" Or tb_minitial.Text = "" Or tb_lname.Text = "" Or tb_num.Text = "" Or tb_add.Text = "" Or rtb_remarks.Text = "" Then
MessageBox.Show("Fillup all the information needed.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
Try
con.Close()
con.Open()
sql = "INSERT INTO DentalRecords (fname,minitial,lname,cnumber,address,remarks) values ('" & tb_fname.Text & "','" & tb_minitial.Text & "','" & tb_lname.Text & "','" & tb_num.Text & "','" & tb_add.Text & "','" & rtb_remarks.Text & "')"
ocmd = New OleDb.OleDbCommand(sql, con)
odatareader = ocmd.ExecuteReader()
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
MessageBox.Show("Succesfully added.", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
cleartextvalue()
tb_search.ReadOnly = False
End If
Else
If tb_fname.Text = "" Or tb_minitial.Text = "" Or tb_lname.Text = "" Or tb_num.Text = "" Or tb_add.Text = "" Or rtb_remarks.Text = "" Then
MessageBox.Show("Fillup all the information needed.", "Personal Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
Else
sql = "UPDATE DentalRecords set lname = '" & tb_lname.Text & "', minitial = '" & tb_minitial.Text & "', fname = '" & tb_fname.Text & "', address = '" & tb_add.Text & "', remarks = '" & rtb_remarks.Text & "' WHERE cnumber = '" & tb_num.Text & "'"
ocmd = New OleDb.OleDbCommand(sql, con)
odatareader = ocmd.ExecuteReader()
MessageBox.Show("Successfully updated.", "Personal Info", MessageBoxButtons.OK, MessageBoxIcon.Information)
cleartextvalue()
b_update.Enabled = False
b_save.Enabled = False
b_delete.Enabled = False
b_add.Enabled = True
b_search.Enabled = True
End If
con.Close()
End If
tb_search.ReadOnly = False
End Sub
Private Sub b_delete_Click(sender As System.Object, e As System.EventArgs) Handles b_delete.Click
Dim del As Integer
If tb_add.Text = "" Or tb_fname.Text = "" Or tb_minitial.Text = "" Or tb_lname.Text = "" Or tb_num.Text = "" Or tb_add.Text = "" Or rtb_remarks.Text = "" Then
MsgBox("No Info")
Else
Try
con.Close()
con.Open()
del = MessageBox.Show("Are you sure you want to delete this file?", "Information System", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If del = 6 Then
sql = "DELETE FROM DentalRecords WHERE fname = '" & tb_fname.Text & "' AND lname = '" & tb_lname.Text & "'AND minitial = '" & tb_minitial.Text & "'AND address = '" & tb_add.Text & "'AND cnumber = '" & tb_num.Text & "' AND remarks = '" & rtb_remarks.Text & "'"
ocmd = New OleDb.OleDbCommand(sql, con)
odatareader = ocmd.ExecuteReader()
MessageBox.Show("Data has been deleted", "Information System", MessageBoxButtons.OK, MessageBoxIcon.Information)
cleartextvalue()
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Sub cleartextvalue() ' for clearing the datas after doin an operation
tb_fname.Text = ""
tb_minitial.Text = ""
tb_lname.Text = ""
tb_add.Text = ""
tb_num.Text = ""
rtb_remarks.Text = ""
tb_search.Text = ""
End Sub
Private Sub b_search_Click(sender As System.Object, e As System.EventArgs) Handles b_search.Click
If tb_search.Text = "" Then
MsgBox("Not Found")
Else
con.Close()
con.Open()
sql = "SELECT * FROM DentalRecords WHERE fname LIKE '" & tb_search.Text & "'"
ocmd = New OleDb.OleDbCommand(sql, con)
odatareader = ocmd.ExecuteReader()
If odatareader.HasRows Then
Do While odatareader.Read
tb_fname.Text = odatareader.Item(1)
tb_minitial.Text = odatareader.Item(2)
tb_lname.Text = odatareader.Item(3)
tb_num.Text = odatareader.Item(5)
tb_add.Text = odatareader.Item(4)
rtb_remarks.Text = odatareader.Item(6)
Loop
tb_fname.ReadOnly = True
tb_minitial.ReadOnly = True
tb_lname.ReadOnly = True
tb_num.ReadOnly = True
tb_add.ReadOnly = True
tb_search.ReadOnly = False
rtb_remarks.ReadOnly = True
Else
MsgBox("Not Found")
End If
b_add.Enabled = True
b_delete.Enabled = True
b_cancel.Enabled = True
b_update.Enabled = True
End If
End Sub
End Class
i want to modify this in such way that creating a table in every new file added

Login Form in Visual Basic 2010 and Access Database

When trying to login on my system I get a "COMexception was unhandled" error on the line that contains:
DBConn.Open("Provider = Microsoft.Jet.OLEDB.4.0;"
This is the code for the function that searches the database:
Public Function Login()
Dim DBConn As New ADODB.Connection
Dim User As New ADODB.Recordset
Dim Username As String
Dim UserDB As String
Dim PassDB As String
Dim UserFound As Boolean
DBConn.Open("Provider = Microsoft.Jet.OLEDB.4.0;" & "DataSource = '" & Application.StartupPath & "\LoginDB.mdb'")
User.Open("UserTable", DBConn, ADODB.CursorTypeEnum.adOpenStatic, ADODB.LockTypeEnum.adLockOptimistic)
UserFound = False
Login = False
Username = "Username = '" & txtboxUsername.Text & "'"
Do
User.find(Username)
If User.BOF = False And User.EOF = False Then
UserDB = User.Fields("Username").Value.ToString
PassDB = User.Fields("Password").Value.ToString
If UserDB <> txtboxUsername.Text Then
User.movenext()
Else
UserFound = True
If PassDB = txtboxPassword.Text Then
User.Close()
DBConn.Close()
Return True
Else
LoginError = "Invalid Password"
User.Close()
DBConn.Close()
Return False
End If
End If
Else
LoginError = "Invalid Username"
User.Close()
DBConn.Close()
Return False
End If
Loop Until UserFound = True
End Function

Resources