Sunday, 30 October 2011


Refresh Datagridview after update, insert. Resolved

All names are in italian language. As client you can choose an Oledb client instead of Fb client.
As connection string, for embedded server,  you can use this one: strConn= "servertype=1;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\YourDB.gdb"

We'll name the application main form "MainForm". First, in main form class of your project insert this routine:

    Public Sub GetData(ByVal strSQL As String, ByVal Table As String)

        Dim com As New FbCommand
        Dim adap As New FbDataAdapter
        Dim ds As New DataSet

        Dim connection As FbConnection = New FbConnection(strConn)
        connection.Open()

        adap = New FbDataAdapter(strSQL, strConn)
        adap.Fill(ds, Table)
        Me.DataGridView1.DataSource = ds.Tables(Table)
        connection.Close()
    End Sub

As edit routine use this code:

        ObjConnection.Open()
        Try
            Dim ObjCommand As New FbCommand()
            ObjCommand.Connection = ObjConnection

            ObjCommand.CommandText = "update YourTable set contesto='" & Replace(Me.txtDidascalia.Text, "'", "''") & "', Percorso='" & Replace(Me.txtPercorso.Text, "'", "''") & "'where contatore='" & MaiForm.DataGridView1.CurrentRow.Cells("Contatore").Value & "'"
            ObjCommand.ExecuteNonQuery()
        Catch
            MsgBox("Percorso del file non univoco", MsgBoxStyle.Critical, "Errore di immisione")
            Exit Sub
        Finally
MainForm.GetData("select*from YourTable", "YourTable")

' Columns names are in italian language
MainForm.DataGridView1.Columns("Contesto").HeaderText = "TITLE"
            MainForm.DataGridView1.Columns("Percorso").Visible = False
            MainForm.DataGridView1.Columns("Contesto").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill

        End Try
        Me.Close()

As insert routine we'll use code:

Dim ObjConnection As New FbConnection()
        Dim connection As FbConnection = New FbConnection("servertype=1;username=sysdba;password=masterkey;database=" & My.Application.Info.DirectoryPath & "\YourDB.gdb")
        connection.Open()

        ' Start a local transaction.


        ' Enlist the command in the current transaction.
        Dim command As FbCommand = connection.CreateCommand()


        Try
            command.CommandText = _
              "insert into Immobili" _
            & " (Contesto, Percorso) values ('" & Replace(Me.txtDidascalia.Text, "'", "''") & "','" _
             & Replace(Me.txtPercorso.Text, "'", "''") & "')"
            command.ExecuteNonQuery()

        Catch
            MsgBox("Questo titolo di immagine esiste già. Cambia il titolo")

            Exit Sub
        End Try

        ObjConnection.Close()
        MainForm.GetData("Select*from YourTable order by contesto", "YourTable")
            MainForm.DataGridView1.Columns("Contesto").HeaderText = "TITLE"
            MainForm.DataGridView1.Columns("Percorso").Visible = False
            MainForm.DataGridView1.Columns("Contesto").AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill
        MainForm.txtSearch.Focus()
Me.Close()
Goodbye
       

Saturday, 29 October 2011


Remove empty rows in text files? Solved

A small code snippet may be very useful in many situations to remove empty rows in text files. Here is the code:
First: in Form1 insert a label by which you indicate to user the directory  where is the converted file: ie C:\beta.tex
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim objFileToRead, objFileToWrite, objFSO, strPathWrite
        Try
            OpenFileDialog1.FileName = ""
            OpenFileDialog1.Filter = "Text files .txt|*.txt"
            Me.OpenFileDialog1.ShowDialog()
            strPathWrite = "C:\beta.txt"

            objFSO = CreateObject("Scripting.FileSystemObject")

            objFileToRead = objFSO.OpenTextFile(OpenFileDialog1.FileName, 1)
            objFileToWrite = objFSO.OpenTextFile(strPathWrite, 2, True)


            Dim strTemp
            Do While Not objFileToRead.AtEndOfStream
                strTemp = objFileToRead.ReadLine
                If strTemp <> "" Then
                    objFileToWrite.WriteLine(strTemp)
                End If
            Loop


            objFileToRead.Close()
            objFileToWrite.Close()
            objFileToRead = Nothing
            objFileToWrite = Nothing
            objFSO = Nothing

        Catch
            Me.Label1.Text = "You haven't chosed any file"
            Me.Label1.TextAlign = Drawing.ContentAlignment.MiddleCenter
            Me.Label1.Left = (Me.Width - Me.Label1.Width) / 2
        End Try

    End Sub

Tuesday, 12 April 2011


Can't install .net framework 3.5? Solved

Updated
Many people can't install .net framework 3.5. And I was one of them. After many attempts I solved the issue.
I downloaded the updated Aaron Stebner "dotfix cleanup tool" from here  in the page top left and launched it. This tool clean your system from any net. framework trace. After cleanup you can safely install the .net framework 3.5. Maybe the Aaron Stebner dotfix cleanup tool fails at the first attempt.
Then follow these steps:
  1. Download "Free Uninstaller 1.1" form this link: http://www.anarchia.com/link_in_frame.php?link=4906&c=
  2. Free Uninstaller 1.1 is a zip package.  Doesn't need installation.
  3. Double click on it
  4. Will appear some registry keys red highlighted on the top of the window
  5. The red highlighted registry keys are invalid registry keys. Remove them.
  6. Retry with the Aaron Stebner dotfix cleanup.
Now of course you'll be able to install .net framework 3.5.

Tuesday, 5 April 2011


Update datagridview with numeric primary key. Solved

The issue, typical in Firebird, consist in the fact that, after you insert a new record  with an autoincrement field in a datagridview, we have to exit from the application to be able to edit the new inserted record.

To solve this issue you have to proceed this way:
1) Proceeding for semplicity with "SQL Manager 2010 lite" software, name the autoincrement field as "Counter"
2) In the database of interest create a new table called for example "Counter" with only a field named "memcont", type Integer.
3) To insert a record use this routine:
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim ObjConnection As New FbConnection()
Dim connection As FbConnection = New
' If we assume an embedded server
FbConnection("servertype=1;username=sysdba;password=masterkey;database=" _

& My.Application.Info.DirectoryPath & "\mydb.gdb")
connection.Open()

' Starts a local transaction.
Dim fbTran As FbTransaction = connection.BeginTransaction()

' Enlist the command in the current transaction.
Dim command As FbCommand = connection.CreateCommand()
command.Transaction = fbTran

Try
command.CommandText = _
"insert into new_table1" _
& " (Clienti, spesa) values ('" & Me.TextBox1.Text & "','"

_
& Me.TextBox2.Text & "')"
command.ExecuteNonQuery()
command.CommandText = _
"delete from counter"
command.ExecuteNonQuery()
'There we make use of the generator GEN_ID function of Firebird with no increment
command.CommandText = _
"INSERT INTO counter(memcont)
VALUES(GEN_ID(new_table1_COUNTER_GEN, 0))"
command.ExecuteNonQuery()
fbTran.Commit()Catch ex As Exception
MsgBox("Insert a Integer number for field spesa")

fbTran.Rollback()Finally
ObjConnection.Close()
End Try

'There we create an instance of the new row in the datatable
Dim persRow As DataRow = Principale.DataSet1.Tables("counter").NewRow
If IsDBNull(persRow("memcont")) Then
persRow("memcont") = Principale.DataSet1.Tables("New_table1").Rows.Count
End If
Principale.DataSet1.Tables("counter").Rows.Add(persRow)
'There we create an instance of the row 0 of the "counter" datatable


Dim CustomersRow As DataRow = Principale.DataSet1.Tables("counter").Rows(0)
'There we create an instance of the new row of the "New_table1" datatable
Dim newCustomersRow As DataRow = Principale.DataSet1.Tables("new_table1").NewRow()
' The autoincremnt field value musts reflect the "memcont" value +1
newCustomersRow("counter") = CustomersRow("MEMCONT") + 1
' there we assign to the unique record in the field "memcont" of the datatable "counter" the
new value of the autoincrement field

CustomersRow("MEMCONT") = newCustomersRow("counter")
newCustomersRow("CLIENTI") = Me.TextBox1.Text
newCustomersRow("spesa") = Me.TextBox2.Text


Principale.DataSet1.Tables("new_table1").Rows.Add(newCustomersRow)

Me.Close()

End Sub

Saturday, 27 November 2010


Vb.net shows error but application works? How to solve the problem

Sometimes the datagridview control show error but the application works normally.
in fact in these cases is sufficient to close the error message and the application works normally.
However one wants that the error message isn't showed.
To eliminate the error message:
  1. Call the datagridview dataerror event
  2. Insert in the event routine this code: e.Cancel = True
Goodbye

Saturday, 13 November 2010


How to manage a firebird database by fbembed.dll

There is around a very good tool to manage firebird databases. Its name is SQL Manager 2010 Lite for Interbase and Firebird. You can downdoad it for free from http://www.softpedia.com/progDownload/EMS-IB-FB-Manager-Lite-Download-22779.html
To manage any database install SQL Manager 2010 Lite for Interbase and Firebird.
Now register a host:
  • I the toolbar click on "Register host" icon or from "Database" menu click on "Register host"
  • Will appear the "Register Host wizard"
  • From the "Host name" dropdown select "embedded" item. Insert username and password
  • In the "Client library" dropdown browse for the fbembed.dll and select it. Next.
  • Skip "Path to database utility". Click "Finish"
Now you can create databases, tables, execute sql scripts, etc.. in visual mode

Friday, 29 October 2010


Using the MS ReportViewer control. Code. Displaying search results

Private Sub AnteRicercaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RicercaTipoAnteToolStripMenuItem.Click

If Me.txtTipo.Text = "" Or Me.txtRicerca.Text = "" Then
MsgBox("Effettuare prima la ricerca", MsgBoxStyle.Critical)
Exit Sub

End If

Report(My.Application.Info.DirectoryPath & "\AnteRicercaTipo.rdl", "Biblioteca")

RepTipo.ReportViewer1.Refresh()

RepTipo.Show()

End Sub


Private Sub Report(ByVal filename As String, ByVal table As String)

Dim RepDS As New ReportDataSource

Dim dv As New System.Data.DataView

dv = DataSet1.Tables(table).DefaultView

RepDS.Name = "DataSource1"
RepDS.Value = dv
RepTipo.ReportViewer1.LocalReport.DataSources.Clear()
RepTipo.ReportViewer1.LocalReport.DataSources.Add(RepDS)

RepTipo.ReportViewer1.LocalReport.ReportPath = filename

End Sub

Trying in google you will be able to find much documentation on how creating a report. In particular I council this link for customizining layout of report and the this link for a practice with ReportViewer.
If you have of the problems with these articles you can contact me obviously.
N.B.
Currently my acquaintances are limited to the type of report which I have created. Perhaps in future I will widen my acquaintances.