clebo99
asked on
Simple VB.Net DB Question: Simple INSERT into DB from Form
Ok, here is what I think is probably another easy DB/VB.Net 2005 question.
I have a form that has 2 textbox's (textbox1 and textbox2). I want to input the information typed into these textbox's into a SQL DB. I have done the following for my code (buiding on what I learned on may last post yesterday:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
Const dbct As String = "Data Source=fakedbname;Initial Catalog=fakedb;Integrated Security=SSPI;"
Dim cn As SqlClient.SqlConnection ' Usually would be in the Function, but it's alone here.
Dim str2 As SqlClient.SqlCommand 'The actual SQL Command object.
Dim cnn As New SqlClient.SqlConnection 'The SQL CONNECITON. THIS IS NEW TO YOU.
namevar = TextBox1.Text
datevar = TextBox2.Text
str = "insert inv(ComputerName, datescanned) values ('" & namevar & "','" & datevar & "')"
Try
cn = New System.Data.SqlClient.SqlC onnection( dbct) ' Make the conneciton.
cn.Open() 'Open
str2 = New SqlClient.SqlCommand ' Not sure why I need to do this twice.
str2.CommandText = str 'Making the string.
str2.Connection = cn 'Making the connection.
MsgBox(str2.CommandText)
(********This is where I am assuming the INSERT Command will occur********(
Catch ex As Exception
MsgBox("ERRROR")
End Try
End Sub
Would I use a DATAWRITER or some other "function" to enter the data. I'm a VB6 person and in the past I would just use the ADODB.RECORDSET and run the insert. I'm trying to figure out how to do this now in VB.Net 2005.
Again, this is probably a pretty easy solution, but I'm willing to give 500 points for a good/quick answer.
Thanks everyone.
Chris, Baltimore
I have a form that has 2 textbox's (textbox1 and textbox2). I want to input the information typed into these textbox's into a SQL DB. I have done the following for my code (buiding on what I learned on may last post yesterday:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim str As String
Const dbct As String = "Data Source=fakedbname;Initial Catalog=fakedb;Integrated Security=SSPI;"
Dim cn As SqlClient.SqlConnection ' Usually would be in the Function, but it's alone here.
Dim str2 As SqlClient.SqlCommand 'The actual SQL Command object.
Dim cnn As New SqlClient.SqlConnection 'The SQL CONNECITON. THIS IS NEW TO YOU.
namevar = TextBox1.Text
datevar = TextBox2.Text
str = "insert inv(ComputerName, datescanned) values ('" & namevar & "','" & datevar & "')"
Try
cn = New System.Data.SqlClient.SqlC
cn.Open() 'Open
str2 = New SqlClient.SqlCommand ' Not sure why I need to do this twice.
str2.CommandText = str 'Making the string.
str2.Connection = cn 'Making the connection.
MsgBox(str2.CommandText)
(********This is where I am assuming the INSERT Command will occur********(
Catch ex As Exception
MsgBox("ERRROR")
End Try
End Sub
Would I use a DATAWRITER or some other "function" to enter the data. I'm a VB6 person and in the past I would just use the ADODB.RECORDSET and run the insert. I'm trying to figure out how to do this now in VB.Net 2005.
Again, this is probably a pretty easy solution, but I'm willing to give 500 points for a good/quick answer.
Thanks everyone.
Chris, Baltimore
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Both the guys are same :-))
Yes, for update and delete also you use the same method.
Yes, for update and delete also you use the same method.
ASKER
Thanks again!!!!
Chris