I am building an application in VB.net and trying to get detail information from the database from de persons name that I write in a textbox, i've the following code (i get an eeror, can not find column "the name i give in the textbox").Can somebody help me??
in the load of the form :
mijnKlant.KlantVullen()
dgvKlanten.DataSource = mijnKlant.p_klant
With Me.dgvKlanten
.Columns("klant_id").DisplayIndex = 0
.Columns("klant_id").HeaderText = "Klantnummer"
.Columns("naam").DisplayIndex = 1
.Columns("naam").HeaderText = "Naam"
.Columns("voornaam").DisplayIndex = 2
.Columns("voornaam").HeaderText = "Voornaam"
.Columns("straat_nr").DisplayIndex = 3
.Columns("straat_nr").HeaderText = "Straat en Nr."
.Columns("postcode").DisplayIndex = 4
.Columns("postcode").HeaderText = "Postcode"
.Columns("naam_gemeente").DisplayIndex = 5
.Columns("naam_gemeente").HeaderText = "Gemeente"
.Columns("landen_naam").DisplayIndex = 6
.Columns("landen_naam").HeaderText = "Land"
.Columns("code_afwijkend_postadres").DisplayIndex = 7
.Columns("code_afwijkend_postadres").HeaderText = "Postadres"
.Columns("facturatieadres_straat_nr").DisplayIndex = 8
.Columns("facturatieadres_straat_nr").HeaderText = "Facturatie Straat en Nr."
.Columns("facturatieadres_postcode").DisplayIndex = 9
.Columns("facturatieadres_postcode").HeaderText = "Facturatie Postcode"
.Columns("facturatieadres_gemeente").DisplayIndex = 10
.Columns("facturatieadres_gemeente").HeaderText = "Facturatie Gemeente"
.Columns("facturatieadres_land").DisplayIndex = 11
.Columns("facturatieadres_land").HeaderText = "Facturatie Land"
.Columns("identiteitskaart_code").DisplayIndex = 12
.Columns("identiteitskaart_code").HeaderText = "Identiteitskaartnummer"
.Columns("postcode_id").Visible = False
.Columns("land_id").Visible = False
.Columns("land_id1").Visible = False
.Columns("postcode_id1").Visible = False
.Columns("postcode_gemeente").Visible = False
.RowHeadersVisible = True
.AutoSizeColumnsMode = DataGridViewAutoSizeColumnMode.Fill
.AllowUserToOrderColumns = False
.AllowUserToResizeColumns = False
.AllowUserToResizeRows = False
.SelectionMode = DataGridViewSelectionMode.FullRowSelect
End With
on the presentation layer:
'enablen van buttons zodat de functie gebruikt kan worden
Me.btnVerwijderKlant.Enabled = True
Me.btnWijzigKlant.Enabled = True
Dim strNaam As String
strNaam = Me.txtNaam.Text 'geeft de naam in de databank
'If outindex = -1 Then Exit Sub
Dim myrw As DataRowView
'functie aanroepen op DAL
myrw = mijnKlant.klant_detail(strNaam)
'binden van opgehaalde details aan de toegewezen controls
Me.txtNaam.DataBindings.Add("Text", myrw, "naam")
Me.txtNaam.DataBindings.Clear()
Me.txtVoornaam.DataBindings.Add("Text", myrw, "voornaam")
Me.txtVoornaam.DataBindings.Clear()
Me.txtStraat.DataBindings.Add("Text", myrw, "straat_nr")
Me.txtStraat.DataBindings.Clear()
Me.cboGemeente.DataBindings.Add("Text", myrw, "naam_gemeente")
Me.cboGemeente.DataBindings.Clear()
Me.cbxAfwijkenAdres.DataBindings.Add("text", myrw, "code_afwijkend_postadres")
'omzetting van string naar boolean
If cbxAfwijkenAdres.Text = "T" Then
Me.cbxAfwijkenAdres.Checked = True
Me.cbxAfwijkenAdres.Text = "Postadres is gelijk aan het facturatieadres"
End If
If cbxAfwijkenAdres.Text = "F" Then
Me.cbxAfwijkenAdres.Checked = False
Me.cbxAfwijkenAdres.Text = "Postadres is gelijk aan het facturatieadres"
End If
'binden van opgehaalde details aan de toegewezen controls
Me.cbxAfwijkenAdres.DataBindings.Clear()
Me.txtPost.DataBindings.Add("Text", myrw, "postcode")
Me.txtPost.DataBindings.Clear()
Me.cboLand.DataBindings.Add("Text", myrw, "landen_naam")
Me.cboLand.DataBindings.Clear()
Me.txtID_Nr.DataBindings.Add("Text", myrw, "identiteitskaart_code")
Me.txtID_Nr.DataBindings.Clear()
Me.txt_F_Straat.DataBindings.Add("Text", myrw, "facturatieadres_straat_nr")
Me.txt_F_Straat.DataBindings.Clear()
Me.cbo_F_Gemeente.DataBindings.Add("Text", myrw, "facturatieadres_gemeente")
Me.cbo_F_Gemeente.DataBindings.Clear()
Me.cbo_F_Land.DataBindings.Add("Text", myrw, "facturatieadres_land")
Me.cbo_F_Land.DataBindings.Clear()
Me.txt_F_Post.DataBindings.Add("Text", myrw, "facturatieadres_postcode")
Me.txt_F_Post.DataBindings.Clear()
dgvKlanten.DataSource = mijnKlant.p_klant
myrw = Nothing
End Sub
on my datalayer:
Public Sub KlantVullen()
' methode om dataview op te bouwen die dan later als bron zal dienen voor de zoekcombo in ons
Dim sqlStr As String = "SprKlantzoeken"
Dim objConn As New SqlConnection(conStr.ConnString)
objConn.Open()
Dim adapter As New SqlDataAdapter(sqlStr, objConn)
'opvangen van foutmelding bij het ledigen van tabel klanten in dataset
Try
ds.Tables.Remove("klanten")
Catch ex As ArgumentException
End Try
adapter.Fill(ds, "klanten")
dt_klanten = ds.Tables("klanten")
dv_klanten = dt_klanten.DefaultView
dv_klanten.Sort = "klant_id"
adapter.Dispose()
adapter = Nothing
objConn.Close()
objConn.Dispose()
End Sub
' functie voor details op te vragen van klanten
Public Function klant_detail(ByVal i_index As String) As DataRowView
Dim dv As DataView
Dim anyrow As DataRowView
dv = New DataView
With dv
.Table = ds.Tables("klanten")
.AllowDelete = True
.AllowEdit = True
.AllowNew = True
.RowFilter = "naam = " & i_index
End With
anyrow = dv.Item(0) 'geeft de eerste rij van de dataview dv
' Simple bind to a TextBox control
dv = dt_klanten.DefaultView
Return anyrow
dv.Dispose()
dv = Nothing
End Function
Stored procedure in SQL database:
USE [parken3]
GO
/* Object: StoredProcedure [dbo].[SprKlantzoeken] Script Date: 07/13/2011 13:45:07 */
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[SprKlantzoeken]
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
select * from landen_lijst l , klant k , postcode p
where k.postcode_id = p.postcode_id
and k.land_id = l.land_id
END
You need to change the line where you set .Rowfilter
With dv
.Table = ds.Tables("klanten")
.AllowDelete = True
.AllowEdit = True
.AllowNew = True
.RowFilter = "naam = " & i_index <---------
End With
Change it to this:
.RowFilter = "naam = '" & i_index.Replace("'", "''") & "'"
Related
I'm having problems with my code, the idea is to find a file in a folder but this is a remote folder and it has many sub folders if it doesn't find it then it should look for it in another folder, but it doesn't seem to work could anyone help me please? thanks for any help :).
Dim dt2 As New System.Data.DataTable()
Dim query2 As String = "SELECT juzgado, rutaPDF FROM mismoCorreo WHERE DATEPART(DAY, BirthDate) = #Day AND DATEPART(MONTH, BirthDate) = #Month"
Dim constr2 As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr2)
Using cmd As New SqlCommand(query2)
cmd.Connection = con
cmd.Parameters.AddWithValue("#Day", DateTime.Today.Day)
cmd.Parameters.AddWithValue("#Month", DateTime.Today.Month)
Using sda As New SqlDataAdapter(cmd)
sda.Fill(dt2)
End Using
End Using
End Using
For Each row As DataRow In dt2.Rows
Dim juzgado As String = row("juzgado").ToString()
Dim nombre_archivo As String = row("rutaPDF").ToString()
If (juzgado = "PRIMERO DE LO FAMILIAR") Then
enviar = "\\172.16.20.101\SisInt\Juzgados\acuerdos\J92016SEM2" & "\" & nombre_archivo & ".docx"
ElseIf (juzgado = "SEGUNDO DE LO FAMILIAR") Then
enviar = "\\172.16.20.101\SisInt\Juzgados\acuerdos\J102016SEM2" & "\" & nombre_archivo & ".docx"
End If
Next
For index = 0 To ListaArchivos.Count - 1
Dim wordApplication = New Microsoft.Office.Interop.Word.Application()
Dim wordDocument As Document = Nothing
Dim paramSourceDocPath As String = ListaArchivos.Item(index)
Dim paramExportFilePath As String = "\\172.16.20.101\SisInt\Convertidos" + "\" + ListaNombres.Item(index) + ".pdf"
Dim paramExportFormat As WdExportFormat =
WdExportFormat.wdExportFormatPDF
Dim paramOpenAfterExport As Boolean = False
Dim paramExportOptimizeFor As WdExportOptimizeFor =
WdExportOptimizeFor.wdExportOptimizeForPrint
Dim paramExportRange As WdExportRange =
WdExportRange.wdExportAllDocument
Dim paramStartPage As Int32 = 0
Dim paramEndPage As Int32 = 0
Dim paramExportItem As WdExportItem =
WdExportItem.wdExportDocumentContent
Dim paramIncludeDocProps As Boolean = True
Dim paramKeepIRM As Boolean = True
Dim paramCreateBookmarks As WdExportCreateBookmarks =
WdExportCreateBookmarks.wdExportCreateWordBookmarks
Dim paramDocStructureTags As Boolean = True
Dim paramBitmapMissingFonts As Boolean = True
Dim paramUseISO19005_1 As Boolean = False
Try
'Abrir documento basados en el que selecciono
wordDocument = wordApplication.Documents.Open(paramSourceDocPath)
' Exportar al formato deseado
If Not wordDocument Is Nothing Then
wordDocument.ExportAsFixedFormat(paramExportFilePath,
paramExportFormat, paramOpenAfterExport,
paramExportOptimizeFor, paramExportRange, paramStartPage,
paramEndPage, paramExportItem, paramIncludeDocProps,
paramKeepIRM, paramCreateBookmarks,
paramDocStructureTags, paramBitmapMissingFonts,
paramUseISO19005_1)
End If
Catch ex As Exception
'MessageBox.Show(ex.Message)
'Registro en base de datos fecha hora y ex.message
Finally
If Not wordDocument Is Nothing Then
wordDocument.Close(False)
wordDocument = Nothing
End If
If Not wordApplication Is Nothing Then
wordApplication.Quit()
wordApplication = Nothing
End If
GC.Collect()
GC.WaitForPendingFinalizers()
GC.Collect()
GC.WaitForPendingFinalizers()
End Try
Next
Path variable is defined globally.
Values of er.item(0),er.item(1), er.item(2),er.item(2),er.item(3) are not accessible after reader object has used item(0). What is wrong?
here is the code
Private Sub load_qc_by()
Dim dname = CategoryDropDown2.Text
con.ConnectionString = path
cmd.Connection = con
con.Open()
cmd.CommandText = "SELECT SQC_By,PQC_By,FQC_By,CQC_By FROM Deliverables WHERE Deliverable ='" & dname & "' AND Project='" & ProjectDropDown2.Text & "'"
Dim er As OleDbDataReader = cmd.ExecuteReader
If er.HasRows Then
While er.Read
If (er.Item(0).ToString.Equals("NA")) Then
Call load_all_Names("sqcby_combobox")
sqcby_combobox.SelectedIndex = 0
Else
MessageBox.Show(er.Item(0))
sqcby_combobox.Text = er.Item(0).ToString
sqcby_combobox.Enabled = False
End If
If (er.Item(1).ToString.Equals("NA")) Then
Call load_all_Names("pqcby_combobox")
pqcby_combobox.SelectedIndex = 0
Else
MessageBox.Show(er.Item(1))
pqcby_combobox.Text = er.Item(1)
pqcby_combobox.Enabled = False
End If
If (er.Item(2).ToString.Equals("NA")) Then
Call load_all_Names("fqcby_combobox")
fqcby_combobox.SelectedIndex = 0
Else
MessageBox.Show(er.Item(2))
fqcby_combobox.Text = er.Item(2)
fqcby_combobox.Enabled = False
End If
If (er.Item(3).ToString.Equals("NA")) Then
Call load_all_Names("cqcby_combobox")
cqcby_combobox.SelectedIndex = 0
Else
MessageBox.Show(er.Item(3))
cqcby_combobox.Text = er.Item(3)
cqcby_combobox.Enabled = False
End If
End While
End If
er.Close()
con.Close()
End Sub
When you run the code, the following error occurs: 'Number of invalid table'.
Can anyone tell me why?
Does anyone have any example that can help me?
I need to know how to pass the address and password of a database of acces to the report and subreport.
RptDocument.Load("c:\ContDeta.rpt", CrystalDecisions.Shared.OpenReportMethod.OpenReportByDefault)
Dim CTableLogInfo As CrystalDecisions.Shared.TableLogOnInfo
Dim ConnInfo As CrystalDecisions.Shared.ConnectionInfo = New CrystalDecisions.Shared.ConnectionInfo()
ConnInfo.Type = CrystalDecisions.Shared.ConnectionInfoType.CRQE
ConnInfo.ServerName = String.Empty
ConnInfo.DatabaseName = "c:\5354Tmp_Contadores.mdb"
ConnInfo.Password = Chr(10) & "5354"
ConnInfo.AllowCustomConnection = False
ConnInfo.IntegratedSecurity = False
For Each CTable As CrystalDecisions.CrystalReports.Engine.Table In RptDocument.Database.Tables
CTable.LogOnInfo.ConnectionInfo = ConnInfo
CTableLogInfo = CTable.LogOnInfo
CTableLogInfo.ReportName = "ContDeta.rpt"
CTableLogInfo.TableName = CTable.Name
CTable.ApplyLogOnInfo(CTableLogInfo)
Next
For Each CTable As CrystalDecisions.CrystalReports.Engine.Table In RptDocument.Subreports("Cabecera.rpt").Database.Tables()
CTable.LogOnInfo.ConnectionInfo = ConnInfo
CTableLogInfo = CTable.LogOnInfo
CTableLogInfo.ReportName = "Cabecera.rpt"
CTableLogInfo.TableName = CTable.Name
CTable.ApplyLogOnInfo(CTableLogInfo)
Next
For Each CTable As CrystalDecisions.CrystalReports.Engine.Table In RptDocument.Subreports("Selecciones.rpt").Database.Tables()
CTable.LogOnInfo.ConnectionInfo = ConnInfo
CTableLogInfo = CTable.LogOnInfo
CTableLogInfo.ReportName = "Selecciones.rpt"
CTableLogInfo.TableName = CTable.Name
CTable.ApplyLogOnInfo(CTableLogInfo)
Next
CrystalReportViewer1.ReportSource = RptDocument
I know there are a few other posts about putting sql results into a vb.net variable but I just couldn't wrap my head around how they did it and how I could do it in my project.
So what I'm trying to do is query my database for 4 different values and then display each value in a certain part of the form. I was going to store the each value into a variable and then do an textbox1.text = i
Updated code for 10/5/2014
Private Sub LBmembers_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LBmembers.SelectedIndexChanged
Dim i As String = LBmembers.SelectedItem
Dim dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
Dim dbSource = "Data Source= C:\members.mdb "
Dim SqlQuery As String = "SELECT StartTime, EndTime, ShipCode, CycleTime, WorkPercent, Share FROM tblMembers WHERE Member = #ID;"
Using con = New OleDb.OleDbConnection(dbProvider & dbSource)
Using cmd = New OleDb.OleDbCommand(SqlQuery, con)
con.Open()
cmd.Parameters.AddWithValue("#ID", OleDb.OleDbType.VarWChar).Value = i
Using reader = cmd.ExecuteReader()
If reader.Read() Then
TBtimestart.Text = reader.ToString(0)
TBtimeend.Text = reader.ToString(1)
Dim j = Convert.ToInt32(reader.ToString(2))
TBtimecycle.Text = reader.GetInt32(3).ToString
TBmemberpercent.Text = reader.GetInt32(4)
TBmembershare.Text = reader.GetInt32(5)
If j = 1 Then
RBpro.Checked = True
ElseIf j = 2 Then
RBret.Checked = True
ElseIf j = 3 Then
RBcov.Checked = True
ElseIf j = 4 Then
RBskiff.Checked = True
ElseIf j = 5 Then
RBmack.Checked = True
ElseIf j = 6 Then
RBhulk.Checked = True
Else
RBpro.Checked = False
RBret.Checked = False
RBcov.Checked = False
RBskiff.Checked = False
RBmack.Checked = False
RBhulk.Checked = False
Exit Sub
End If
End If
End Using
con.Close()
End Using
End Using
End Sub
The exception
InvalidCastException was unhandled Specified cast is not valid.
Pops up on the line TBtimecycle.text = reader.GetInt32(3).ToString
The reader is also reading "System.Data.OleDb.OleDbDataReader" when I insert the test code "TBgrossisk.Text = reader.ToString()" into the using statement
If you look carefully to the syntax of the SELECT statement you will see that you can request any column of the table with just one query.
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles LBmembers.SelectedIndexChanged
if string.IsNullOrWitheSpace(ListBox1.SelectedItem.ToString()) Then
MessageBox.Show("Select an item from the Listbox")
End If
Dim member As String = ListBox1.SelectedItem
Dim dbProvider = "PROVIDER=Microsoft.Jet.OLEDB.4.0;"
Dim dbSource = "Data Source= C:\members.mdb "
Dim SqlQuery As String = "SELECT StartTime, EndTime, ShipCode, CycleTime " & _
"FROM tblMembers WHERE Member = #ID;"
Using con = New OleDb.OleDbConnection(dbProvider & dbSource)
Using cmd = New OleDb.OleDbCommand(SqlQuery, con)
con.Open()
cmd.Parameters.AddWithValue("#ID", OleDb.OleDbType.VarWChar).Value = member
Using reader = cmd.ExecuteReader
if reader.Read() Then
TextBox1.Text = reader.GetString(0)
TextBox2.Text = reader.GetString(1)
Dim j = reader.GetInt32(2)
If j = 1 Then
Radio1.Checked = True
ElseIf j = 2 Then
Radio2.Checked = True
ElseIf j = 3 Then
Radio3.Checked = True
ElseIf j = 4 Then
Radio4.Checked = True
ElseIf j = 5 Then
Radio5.Checked = True
ElseIf j = 6 Then
Radio6.Checked = True
Else
Exit Sub
End If
TextBox4.Text = reader.GetInt32(3).ToString()
Else
MessageBox.Show("The search for '" & member & "' doesn't find any data")
End If
End Using
End Using
End Using
End Sub
Instead of using ExecuteScalar that returns just the first column of the first row retrieved you could use an OleDbDataReader returned by the method ExecuteReader. This object allows to access the various column in the current record using an index (or the column name). Read more about OleDbDataReader in MSDN
I have two sections of code. The first one updates data in an access database table called tbl_Customers. It works fine:
Private Sub UpdateRecord()
Dim imgLocation As String
imgLocation = idPictureBox.ImageLocation
Dim cb As New OleDb.OleDbCommandBuilder(da)
ds.Tables("tbl_Customers").Rows(inc).Item(1) = txtFirstName.Text
ds.Tables("tbl_Customers").Rows(inc).Item(2) = txtSurname.Text
ds.Tables("tbl_Customers").Rows(inc).Item(3) = imgLocation
ds.Tables("tbl_Customers").Rows(inc).Item(4) = mtxtPhoneNumber.Text
ds.Tables("tbl_Customers").Rows(inc).Item(5) = cbReferred.Text
ds.Tables("tbl_Customers").Rows(inc).Item(6) = custType
da.Update(ds, "tbl_Customers")
MessageBox.Show("Data updated", "Update")
btnCommit.Enabled = False
btnClear.Enabled = False
btnAddNew.Enabled = True
btnupdate.Enabled = True
btnDelete.Enabled = True
btnFirst.Enabled = True
btnLast.Enabled = True
btnNext.Enabled = True
btnPrevious.Enabled = True
btnFind.Enabled = True
End Sub
The second piece of code updates an access database table called tbl_SalesInventory. This piece of code duplicates every record in the table!
Private Sub UpdateItemRecord()
Dim cb As New OleDb.OleDbCommandBuilder(da10)
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(1) = txtItemDescription.Text
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(2) = txtItemMin.Text
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(3) = txtItemAsk.Text
Dim SelectedType As Integer = cbSellItemType.SelectedIndex
Dim Type As Integer = SelectedType + 1
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(4) = Type.ToString("D2")
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(5) = txtItemCost.Text
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(6) = dtpItemPDate.Value
If lblItemStatusDisplay.Text = "For Sale" Then
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(7) = "F"
ElseIf lblItemStatus.Text = "On Layaway" Then
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(7) = "L"
ElseIf lblItemStatus.Text = "Sold" Then
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(7) = "S"
ElseIf lblItemStatus.Text = "Displayed" Then
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(7) = "D"
ElseIf lblItemStatus.Text = "Scrapped" Then
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(7) = "X"
End If
Try
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(8) = txtDiaMin.Text
Catch ex As Exception
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(8) = ""
End Try
Try
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(9) = txtDiaValue.Text
Catch ex As Exception
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(9) = ""
End Try
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(10) = txtItemWeight.Text
ds10.Tables("tbl_SalesInventory").Rows(incInv).Item(11) = lblItemMeasure.Text
da10.Update(ds10, "tbl_SalesInventory")
MessageBox.Show("Data updated", "Update")
RefreshDataset()
SortDS10()
btnCommitItem.Enabled = False
btnClearItem.Enabled = False
btnAddItem.Enabled = True
btnUpdateItem.Enabled = True
btnDeleteItem.Enabled = True
btnFirstItem.Enabled = True
btnLastItem.Enabled = True
btnNextItem.Enabled = True
btnPreviousItem.Enabled = True
btnSearchItem.Enabled = True
End Sub
Can anyone shed some light as to why this is happening? Thank you for your prompt response to this issue.