Wrox Programmer Forums
|
Visual Basic 2005 Basics If you are new to Visual Basic programming with version 2005, this is the place to start your questions. For questions about the book: Beginning Visual Basic 2005 by Thearon Willis and Bryan Newsome, ISBN: 0-7645-7401-9 please, use this forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Basic 2005 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 31st, 2007, 05:41 AM
Registered User
 
Join Date: May 2007
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default Connection String (Object)

I am new to Vb.net and I'me trying to store data to the sql databse using text fields. Everything seems ok but i've tried several connection strings but they wont work.Could somebody give a hand, please?

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim conn As New SqlClient.SqlConnection()
Dim cmd As New SqlClient.SqlCommand()
conn.ConnectionString = "Data Source=(local);Initial Catalog=myDatabase;Integrated Security=SSPI"
conn.Open()
cmd.Connection = conn
With cmd.Parameters
.Add(New SqlClient.SqlParameter("@Student_id", TextBox1.Text))
.Add(New SqlClient.SqlParameter("@FirstName", TextBox1.Text))
.Add(New SqlClient.SqlParameter("@LastName", TextBox2.Text))
.Add(New SqlClient.SqlParameter("@Email", TextBox3.Text))
.Add(New SqlClient.SqlParameter("@Module", TextBox1.Text))
End With

cmd.CommandText = "INSERT INTO <Students> (<field1>,<field2>,<field3>,<field4>,<field5>) VALUES (@Student_id, @FirstName, @LastName, @Email, @Module)"
Progress.Show()
End Sub


 
Old June 1st, 2007, 10:41 AM
Friend of Wrox
 
Join Date: Nov 2004
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Having filled up your command, you need to execute it.

Note that in this forum, if you click the [[u]#</u>] button, that will add two “code tags” to your post. Everything between the tags will be fixed pitch, and there will be a blank line before and after the code section. All spaces are preserved when you do that. So:
Code:
Private Sub btnSubmit_Click(ByVal sender As System.Object, _
                            ByVal e As System.EventArgs _
                           ) Handles btnSubmit.Click

    Dim conn As New SqlClient.SqlConnection()
    Dim cmd As New SqlClient.SqlCommand()
    conn.ConnectionString = "Data Source=(local);" & _
                            "Initial Catalog=myDatabase;" & _
                            "Integrated Security=SSPI"
    conn.Open()
    cmd.Connection = conn

    With cmd.Parameters
        .Add(New SqlClient.SqlParameter("@Student_id", TextBox1.Text))
        .Add(New SqlClient.SqlParameter("@FirstName", TextBox1.Text))
        .Add(New SqlClient.SqlParameter("@LastName", TextBox2.Text))
        .Add(New SqlClient.SqlParameter("@Email", TextBox3.Text))
        .Add(New SqlClient.SqlParameter("@Module", TextBox1.Text))
    End With

    cmd.CommandText = _
        "INSERT INTO <Students> ( <field1>, <field2>, <field3>, <field4>, <field5> ) " & _
        "VALUES                 ( @Student_id, @FirstName, @LastName, @Email, @Module )"

    cmd.Execute     ' I believe this is the right method; look into that.

    Progress.Show()

End Sub





Similar Threads
Thread Thread Starter Forum Replies Last Post
connection string Sheraz Khan ASP.NET 2.0 Basics 3 July 5th, 2007 08:33 AM
Connection String(object) ssimkhan Visual Studio 2005 1 May 31st, 2007 04:54 PM
Connection String aadz5 JSP Basics 1 January 24th, 2005 04:36 AM
Connection Object shahchi1 ADO.NET 5 June 1st, 2004 11:48 PM
connection string aadz5 ASP.NET 1.0 and 1.1 Basics 4 October 20th, 2003 08:43 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.