OnLoad Event

D

Dmox

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

S.Y.M. Wong-A-Ton

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/li...client.sqlcommand.executenonquery(vs.80).aspx).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top