Hi Folks
Forgive a very rusty VB4/5 sometime coder trying to catch up on the .net art, for a possibly stupid question.
I have been playing around with small coding tasks as a sort of self inflicted training course, and thought that this should be possible:-
To loop through a process that creates the capital letters of the alphabet and then numbers them, as it does so, to write them as entries to a MYSQL table, i.e. A1, B2 etc.
I thought the logic through and cobbled together a procedure which runs fine once then just bombs out, please see the code below. I think that I have either made a classic schoolboy mistake, or this is just confirmation that my old grey cells have finally packed their bags and I'm now flying solo!!
Any help or guidance greatly appreciated
Code
Private Sub btnSeed_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSeed.Click
Dim conn As New MySqlConnection
Dim myCommand As New MySqlCommand
Dim myConnectionString As String
Dim iCount As Integer
Dim StrChar As String
Dim sdNum As Integer
StrChar = "@"
sdNum = 0
For iCount = 1 To 26
StrChar = Chr(Asc(StrChar) + 1)
sdNum = sdNum + 7
myConnectionString = "server=localhost;User ID=Tester;Password=Tester;Database=Test"
conn.ConnectionString = myConnectionString
myCommand.Connection = conn
myCommand.CommandText = "INSERT INTO seed_number(StartLetter, SeedNo) VALUES(?StartL, ?sedNum)"
myCommand.Parameters.Add("?StartL", StrChar)
myCommand.Parameters.Add("?sedNum", sdNum)
Try
conn.Open()
myCommand.ExecuteNonQuery()
conn.Close()
Catch ex As MySqlException
MsgBox("There was an error updating the database: " + ex.Message)
End Try
Next
End Sub
Best Regards
Alex
Loading
Alex StoreyPosted Feb 20, 2008, 8:59 AM
Most grateful for all the help, much appreciated, maybe I can move on from here and learn something more
Cheers
Alex
Jan MontanoPosted Feb 20, 2008, 4:33 AM
myCommand.Parameters.Clear()
to the code above. Perhaps that should fix the error you're encountering.
Alex StoreyPosted Feb 20, 2008, 4:23 AM
Thanks for responding so quickly, I will try your suggestion, hadn't thought of that!
The actual error seems to occur on the 2nd pass of the loop at the line myCommand.Parameters.Add("?sedNum", sdNum)
The application just closes with no error messages, if you restart you just get another A and 1 entry in the table.
Please also ignore my typo in sdNum = sdNum + 7 the seven should actually be a one and is so in the real code, that was just me playing :-)
Cheers
Alex
Jan MontanoPosted Feb 19, 2008, 9:42 PM
Do you know what the exact error is?
try to initialize the connection before entering the loop.
----------------------------------------------------
myConnectionString = "server=localhost;User ID=Tester;Password=Tester;Database=Test"
conn.ConnectionString = myConnectionString
conn.Open()
myCommand.Connection = conn
For iCount = 1 To 26
StrChar = Chr(Asc(StrChar) + 1)
sdNum = sdNum + 7
myCommand.Parameters.Clear()
myCommand.CommandText = "INSERT INTO seed_number(StartLetter, SeedNo) VALUES(?StartL, ?sedNum)"
myCommand.Parameters.Add("?StartL", StrChar)
myCommand.Parameters.Add("?sedNum", sdNum)
Try
myCommand.ExecuteNonQuery()
Catch ex As MySqlException
MsgBox("There was an error updating the database: " + ex.Message)
End Try
Next
conn.Close()