[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Simple VB.Net DB Question:  Simple INSERT into DB from Form

Posted on 2007-08-01
Medium Priority
Last Modified: 2013-11-26
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 & "')"
            cn = New System.Data.SqlClient.SqlConnection(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.  
           (********This is where I am assuming the INSERT Command will occur********(
        Catch ex As Exception
        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
Question by:clebo99
  • 3
LVL 18

Accepted Solution

Sethi earned 2000 total points
ID: 19613758
LVL 18

Assisted Solution

Sethi earned 2000 total points
ID: 19613774
ExecuteNonQuery method is similar to Execute method that we had in a connection and command object in ADO.

Author Comment

ID: 19613823
Guys, that worked like a charm...I knew it was something easy like that......I'm going to assume that Update/Delete and any other SQL commands are the same.

Thanks again!!!!

LVL 18

Expert Comment

ID: 19613947
Both the guys are same :-))

Yes, for update and delete also you use the same method.

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses
Course of the Month19 days, 9 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question