I'm having issues with an OnLoad event. I'm trying to update a SQL Database
when the form loads with the following code:
Public Sub FormEvents_Loading(ByVal sender As Object, ByVal e As
LoadingEventArgs)
' Write your code here.
Dim conn As New SqlConnection()
conn.ConnectionString = "Data Source=SQL2005;Initial
Catalog=Database1;Persist Security Info=True;User ID=DBDude;Password=MyPass"
Dim SqlConnection As String = "INSERT INTO Ticket (Ref) VALUES
('Test')"
conn.Close()
End Sub
It debugs and loads fine, however it's not updating the database at all.
S.Y.M. Wong-A-Ton - 09 Sep 2007 02:06 GMT
Currently, the code is not really doing any actions on the database: You set
the connection string, but don't open the connection. Then you declare a
SqlConnection as a string and don't do anything with it. And then you just
close the connection...
You need to create and use a SqlCommand object and then execute the INSERT
statement using the ExecuteNonQuery() method on the command (see
http://msdn2.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.execut
enonquery(vs.80).aspx).
---
S.Y.M. Wong-A-Ton
> I'm having issues with an OnLoad event. I'm trying to update a SQL Database
> when the form loads with the following code:
[quoted text clipped - 11 lines]
>
> It debugs and loads fine, however it's not updating the database at all.