Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 asp.net insert data in sql form

Author  Topic 

igorblackbelt
Constraint Violating Yak Guru

407 Posts

Posted - 2006-08-17 : 16:58:06
Guys -

Is my code behind correct to insert records from a form into a sql db ? It gives an error on the "cmd.Connection.Open()" line, it gives me the error msg below:
You may have tried to use an object without providing an instance of the object. For example, Dim CustomerTable As DataTable should be rewritten as Dim CustomerTable As New DataTable.

All that I need from this form is be able to add data from the form to a single table.



Imports System.Data.SqlClient

Partial Class _Default
Inherits System.Web.UI.Page

Dim strConn As String = "data source=SERVER; User ID=dbUser; Password=pa$$w0rd; Persist Security Info=True;packet size=4096"

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click

Dim cmd As New SqlCommand("INSERT INTO tblCustomers ( field1, field2, field3) 'VALUES('" & txtField1.Text & "','" & txtField2.Text & "','" & txtField3.Text & "')", New SqlConnection(strConn))

cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
End Sub

End Class



---
"There's no sexy way to carry a lunch bag." My boss
   

- Advertisement -