Hi there,
I'm having an issue with the startup of my build app with vb2010.
Let me explain what the situation is at the moment.
I have a form and a splashscreen. I use the application events (startup event) to check if there is a network connection, if so the splashscreen will run and it will check if the database is available, if this is the case, it will copy the online database to the local ms access database.
If the startup check of the network is false, it the user gets a message that there isn't any network available and the program will try to run normal, then when the user clicks the ok button of that message, it will check if the local database has any records. If so the splashscreen will run and the program starts normal (without copy the database, because there isn't any network available).
The issue that I am having is, that when there isn't a network connection and the local database is empty, (I do get the message that the local database is empty), but the program still runs, while it should exit.
Here is the code of the application event:
And the splashscreen:
The "DBConnectionStatus" is a function in a class:
I hope someone can help me with this.
If I need to clarify something, please let me know.
Thanks in advanced.
I'm having an issue with the startup of my build app with vb2010.
Let me explain what the situation is at the moment.
I have a form and a splashscreen. I use the application events (startup event) to check if there is a network connection, if so the splashscreen will run and it will check if the database is available, if this is the case, it will copy the online database to the local ms access database.
If the startup check of the network is false, it the user gets a message that there isn't any network available and the program will try to run normal, then when the user clicks the ok button of that message, it will check if the local database has any records. If so the splashscreen will run and the program starts normal (without copy the database, because there isn't any network available).
The issue that I am having is, that when there isn't a network connection and the local database is empty, (I do get the message that the local database is empty), but the program still runs, while it should exit.
Here is the code of the application event:
VB.NET:
Protected Overrides Function OnInitialize(commandLineArgs As System.Collections.ObjectModel.ReadOnlyCollection(Of String)) As Boolean
Me.MinimumSplashScreenDisplayTime = 5000
Return MyBase.OnInitialize(commandLineArgs)
End Function
Private Sub MyApplication_Startup(sender As Object, e As Microsoft.VisualBasic.ApplicationServices.StartupEventArgs) Handles Me.Startup
Dim topmessage As New TopMostMessage
Dim result As DialogResult
Dim sql As String
Dim ds As New DataSet
Dim oledbAdapter As OleDbDataAdapter
Dim connetionString As String
Dim connection As OleDbConnection
If My.Computer.Network.IsAvailable Then
Dim splash As SplashScreen1 = CType(My.Application.SplashScreen, SplashScreen1)
Else
result = topmessage.Show("There is no internet connection available!" & vbNewLine & _
"Songlist Editor 2 will try to run normal!", _
"Songlist Editor 2", MessageBoxButtons.OK, MessageBoxIcon.Warning)
If result = DialogResult.OK Then
connetionString = "Provider=" + My.Settings.dbaccess + "Data Source = |DataDirectory|\" + My.Settings.msaccess
sql = "SELECT * FROM songs"
connection = New OleDbConnection(connetionString)
Try
connection.Open()
oledbAdapter = New OleDbDataAdapter(sql, connection)
oledbAdapter.Fill(ds, "songs")
oledbAdapter.Dispose()
connection.Close()
If ds.Tables(0).Rows.Count > 0 Then
'Do nothing
Else
result = topmessage.Show("Host is not available!" & vbNewLine & _
"Songlist Editor 2 cannot use the local database and will exit!" & vbNewLine & _
"Make sure you have an active internet connection and try again later", _
"Songlist Editor", MessageBoxButtons.OK, MessageBoxIcon.Warning)
If result = DialogResult.OK Then
e.Cancel = False
End If
End If
Catch ex As Exception
MsgBox("Can not open connection ! ")
End Try
End If
End If
End Sub
And the splashscreen:
VB.NET:
Sub New()
InitializeComponent()
End Sub
Public Overrides Sub ProcessCommand(ByVal cmd As System.Enum, ByVal arg As Object)
MyBase.ProcessCommand(cmd, arg)
End Sub
Public Enum SplashScreenCommand
SomeCommandId
End Enum
Private Sub SplashScreen1_Load(sender As Object, e As System.EventArgs) Handles Me.Load
If DataBase.DBConnectionStatus = True Then
'copy database from online to local
'dbc.CopyDatabase()
Dim dt As New DataSet()
Try
'Get Data into DataTable from MySQL Server database
Using cnn As New MySqlConnection("Server=" + My.Settings.host + ";Database=" + My.Settings.database + ";Uid=" + My.Settings.username + ";Pwd=" + My.Settings.mysqlpw)
Dim cmdSelect As MySqlCommand = New MySqlCommand("Select * From songs")
cmdSelect.Connection = cnn
Dim ad As New MySqlDataAdapter(cmdSelect)
ad.AcceptChangesDuringFill = False
ad.Fill(dt)
End Using
Try
DeleteRecords()
Try
'Insert Data from DataTable into Access database
Using cnn As New OleDbConnection("Provider=" + My.Settings.dbaccess + "Data Source = |DataDirectory|\" + My.Settings.msaccess)
Dim cmdSelect As OleDbCommand = New OleDbCommand("Select * From songs")
cmdSelect.Connection = cnn
Dim ad As New OleDbDataAdapter(cmdSelect)
Dim cmdBuilder As New OleDbCommandBuilder(ad)
Dim cmd As OleDbCommand = cmdBuilder.GetInsertCommand()
cmd.Connection = cnn
For Each row As DataRow In dt.Tables(0).Rows
ad.InsertCommand = cmd
Next
ad.Update(dt)
End Using
Catch ex3 As Exception
MessageBox.Show(ex3.Message)
End Try
Catch ex2 As Exception
MessageBox.Show(ex2.Message)
End Try
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Else
Application.Exit()
End If
version.Text = System.String.Format(version.Text, My.Application.Info.Version.Major, My.Application.Info.Version.Minor, My.Application.Info.Version.Build, My.Application.Info.Version.Revision)
End Sub
Public Sub DeleteRecords()
MScmd.Connection = MScon
MScmd.CommandType = CommandType.Text
MScmd.CommandText = "DELETE FROM songs"
Using MScon
MScon.Open()
MScmd.ExecuteNonQuery()
End Using
End Sub
The "DBConnectionStatus" is a function in a class:
VB.NET:
Public Shared Function DBConnectionStatus() As Boolean
Try
Using sqlConn As New MySqlConnection("Server=" + My.Settings.host + ";Database=" + My.Settings.database + ";Uid=" + My.Settings.username + ";Pwd=" + My.Settings.mysqlpw)
sqlConn.Open()
Return (sqlConn.State = ConnectionState.Open)
End Using
Catch e1 As MySqlException
Return False
Catch e2 As Exception
Return False
End Try
End Function
I hope someone can help me with this.
If I need to clarify something, please let me know.
Thanks in advanced.