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

Posted on 2007-08-01
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
    LVL 18

    Accepted Solution

    LVL 18

    Assisted Solution

    ExecuteNonQuery method is similar to Execute method that we had in a connection and command object in ADO.

    Author Comment

    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

    Both the guys are same :-))

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Find Ransomware Secrets With All-Source Analysis

    Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

    The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Hi everyone! This is Experts Exchange customer support.  This quick video will show you how to change your primary email address.  If you have any questions, then please Write a Comment below!

    761 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now