Welcome to Dream.In.Code
Getting VB.NET Help is Easy!

Join 136,513 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,754 people online right now. Registration is fast and FREE... Join Now!




Connecting VB2008 to SQL Server 2005

2 Pages V  1 2 >  
Reply to this topicStart new topic

Connecting VB2008 to SQL Server 2005

brickz_0709
13 Aug, 2008 - 01:46 PM
Post #1

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 10

hi.

im having problems connecting my application to SQl server. it gives me an error of "Database failed to open" whenever i run the codes below. could somebody help me on this? ive look on possible solutions and tried but to no luck. by the way, its an add function in a customer profile database.

CODE

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Or TextBoxSurname.Text = "" Or TextBoxGiven.Text = "" Or TextBoxMiddle.Text = "" Or TextBox78.Text = "" Or TextBox2.Text = "" Or TextBoxCell.Text = "" Then
            MessageBox.Show("Error adding!", "Add", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            Dim objConnection As New SqlConnection("Data Source=.\SQLEXPRESS\Databases;Database=C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Softeng.mdf;Integrated Security=true")
            
            Dim objCommand As New SqlCommand("INSERT INTO dbo.CustomerProfile& #40;LastName,FirstName,MiddleInitial,Address,BdayMonth,BdayDay,BdayYear,Gender,R
emarks)values & #40;@LastName,@FirstName,@MiddleInitial,@Address,@BdayMonth,@BdayDay,@BdayYear,@
Gender,@Remarks)", objConnection)
            objCommand.Parameters.Add("@Company", SqlDbType.NVarChar, 50).Value = TextBox1.Text
            objCommand.Parameters.Add("@LastName", SqlDbType.Char, 20).Value = TextBoxSurname.Text
            objCommand.Parameters.Add("@FirstName", SqlDbType.Char, 20).Value = TextBoxGiven.Text
            objCommand.Parameters.Add("@MiddleInitial", SqlDbType.Char, 1).Value = TextBoxMiddle.Text
            objCommand.Parameters.Add("@Age", SqlDbType.Int, 2).Value = TextBoxAge.Text
            objCommand.Parameters.Add("@Address", SqlDbType.NVarChar, 50).Value = TextBox78.Text
            objCommand.Parameters.Add("@Landline", SqlDbType.NVarChar, 15).Value = TextBoxLand.Text
            objCommand.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = TextBoxEmail.Text
            objCommand.Parameters.Add("@IdNo", SqlDbType.Int, 20).Value = TextBox2.Text
            objCommand.Parameters.Add("@Month", SqlDbType.Char, 20).Value = ComboBoxMonth.SelectedItem.ToString
            objCommand.Parameters.Add("@Day", SqlDbType.Char, 2).Value = ComboBoxDay.SelectedItem.ToString
            objCommand.Parameters.Add("@Year", SqlDbType.Char, 4).Value = ComboBoxYear.SelectedItem.ToString
            objCommand.Parameters.Add("@Gender", SqlDbType.Char, 10).Value = gender

            Try
                objConnection.Open()

            Catch sqlExceptionErr As SqlException
                If MessageBox.Show("Database failed to open.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) = DialogResult.OK Then
                    objConnection.Close()
                    'Exit Sub
                End If
            End Try

            objCommand.ExecuteNonQuery()
            objConnection.Close()

            MessageBox.Show("Customer successfully added!", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Empty1()
        End If
    End Sub


thanks and im alex.
User is offlineProfile CardPM
+Quote Post

brickz_0709
RE: Connecting VB2008 To SQL Server 2005
13 Aug, 2008 - 09:14 PM
Post #2

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 10

hi.

im having problems connecting my application to SQl server. it gives me an error of "Database failed to open" whenever i run the codes below. could somebody help me on this? ive look on possible solutions and tried but to no luck. by the way, its an add function in a customer profile database.

the code within the if only states that it will show error whenever a field is empty while in the else it processes the query to the database, and there also starts my problem.

CODE

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If TextBox1.Text = "" Or TextBoxSurname.Text = "" Or TextBoxGiven.Text = "" Or TextBoxMiddle.Text = "" Or TextBox78.Text = "" Or TextBox2.Text = "" Or TextBoxCell.Text = "" Then
            MessageBox.Show("Error adding!", "Add", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Else
            Dim objConnection As New SqlConnection("Data Source=.\SQLEXPRESS\Databases;Database=C:\Program Files\Microsoft SQL Server\MSSQL.1\MSSQL\Data\Softeng.mdf;Integrated Security=true")
            'Declaration of the Database

            Dim objCommand As New SqlCommand("INSERT INTO dbo.CustomerProfile& #40;LastName,FirstName,MiddleInitial,Address,BdayMonth,BdayDay,BdayYear,Gender,R
emarks)values & #40;@LastName,@FirstName,@MiddleInitial,@Address,@BdayMonth,@BdayDay,@BdayYear,@
Gender,@Remarks)", objConnection)
            objCommand.Parameters.Add("@Company", SqlDbType.NVarChar, 50).Value = TextBox1.Text
            objCommand.Parameters.Add("@LastName", SqlDbType.Char, 20).Value = TextBoxSurname.Text
            objCommand.Parameters.Add("@FirstName", SqlDbType.Char, 20).Value = TextBoxGiven.Text
            objCommand.Parameters.Add("@MiddleInitial", SqlDbType.Char, 1).Value = TextBoxMiddle.Text
            objCommand.Parameters.Add("@Age", SqlDbType.Int, 2).Value = TextBoxAge.Text
            objCommand.Parameters.Add("@Address", SqlDbType.NVarChar, 50).Value = TextBox78.Text
            objCommand.Parameters.Add("@Landline", SqlDbType.NVarChar, 15).Value = TextBoxLand.Text
            objCommand.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = TextBoxEmail.Text
            objCommand.Parameters.Add("@IdNo", SqlDbType.Int, 20).Value = TextBox2.Text
            objCommand.Parameters.Add("@Month", SqlDbType.Char, 20).Value = ComboBoxMonth.SelectedItem.ToString
            objCommand.Parameters.Add("@Day", SqlDbType.Char, 2).Value = ComboBoxDay.SelectedItem.ToString
            objCommand.Parameters.Add("@Year", SqlDbType.Char, 4).Value = ComboBoxYear.SelectedItem.ToString
            objCommand.Parameters.Add("@Gender", SqlDbType.Char, 10).Value = gender

            Try
                objConnection.Open()

            Catch sqlExceptionErr As SqlException
                If MessageBox.Show("Database failed to open.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) = DialogResult.OK Then
                    objConnection.Close()
                    'Exit Sub
                End If
            End Try

            objCommand.ExecuteNonQuery()
            objConnection.Close()

            MessageBox.Show("Customer successfully added!", "Successful", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Empty1()
        End If
    End Sub


just nid the help badly.
thanks in advance smile.gif
User is offlineProfile CardPM
+Quote Post

dineeshd
RE: Connecting VB2008 To SQL Server 2005
14 Aug, 2008 - 01:24 AM
Post #3

D.I.C Addict
Group Icon

Joined: 30 Jun, 2008
Posts: 578



Thanked: 16 times
Dream Kudos: 575
My Contributions
Your connection string not seems to be right. Check the below link and rewrite your connection string.
SQL Server 2005 Connection Strings

And VB2008 is VB.Net so you should post in VB.NET Forum in future.
User is offlineProfile CardPM
+Quote Post

jens
RE: Connecting VB2008 To SQL Server 2005
14 Aug, 2008 - 01:24 AM
Post #4

D.I.C Head
Group Icon

Joined: 9 May, 2008
Posts: 113



Thanked: 3 times
Dream Kudos: 150
My Contributions
Hi there!

Have had my share of problems with this. I'm not where my code is right now so this is from the top of my head...

I think you could use:
CODE

("Data Source=.\SQLExpress;Initial Catalog=TheNameOfYourDatabase;Security=True;")



Where SQLExpress is the name of your instance of SQL server express that manages the DB you want to use.

Where TheNameOfYourDatabase would be somthing like "Northwind" or "ShopData" or whatever the name of your DB is.

But!! This requires that your sql server express is running and allows connections...
If you're trying to get at the datafile without using a running instance of SQL server express the conn string will be different and since I don't do it that way myselfe I can't remember how to right now.

There are a few things to check to get all this to work, some of them are:
Is the server running?
...on the same computer as you are working on?
Are you able to connect to the DB with MSSMSE? DL MSSMSE

Sorry that I cant give more precise help right now.

/Jens
User is offlineProfile CardPM
+Quote Post

eclipsed4utoo
RE: Connecting VB2008 To SQL Server 2005
14 Aug, 2008 - 12:18 PM
Post #5

D.I.C Regular
Group Icon

Joined: 21 Mar, 2008
Posts: 363



Thanked: 19 times
Dream Kudos: 25
My Contributions
you are getting that error message because you are telling the application to display that error message when the connection fails. use this code in the "Catch"...

CODE

                If MessageBox.Show(sqlExceptionErr.Message , "Error", MessageBoxButtons.OK, MessageBoxIcon.Error) = DialogResult.OK Then
                    objConnection.Close()
                    'Exit Sub
                End If


that will give you the real error message. post that error message.

This post has been edited by eclipsed4utoo: 14 Aug, 2008 - 12:18 PM
User is offlineProfile CardPM
+Quote Post

jens
RE: Connecting VB2008 To SQL Server 2005
17 Aug, 2008 - 07:37 AM
Post #6

D.I.C Head
Group Icon

Joined: 9 May, 2008
Posts: 113



Thanked: 3 times
Dream Kudos: 150
My Contributions
Sorry, there's a typo in my previous post.

If you are running a database management system (SQL server express) on your system and want to use it for your code here are two working connection strings. These are to be used only if you do not work with the database file, but with the server.

With SQL security:
CODE

  Const connString As String = "Data Source = acer\sqlexpress; Initial Catalog = TeleTest; UID = Jens; Password = jenspw"


With Windows security:
CODE

    Const connString As String = "Data Source = acer\sqlexpress; Initial Catalog = TeleTest; integrated security=true"


acer is the machine my sql server runs on and it's the same macine as the VB program runs on. sqlexpress is the name of the instance of sql server that handles the database.
You could substitute acer\sqlexpress with .\sqlexpress.
TeleTest is the name of the database within the database handler that I want to connect to.
UID is my user ID in SQL server
password is my user password in SQL server.

/Jens
User is offlineProfile CardPM
+Quote Post

brickz_0709
RE: Connecting VB2008 To SQL Server 2005
18 Aug, 2008 - 06:00 AM
Post #7

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 10

thank you all for your help. il check again and try ur suggestions. the thing i did was i installed vb2008 pro (though vb2008 has an included sql 2005 express installer, i didnt use it), then install the smsse (is the abbreviation correct?) then install the sqlexpress to be able to create name instance, as what others said. were all of these correct? i dont undrstand what u min by "the server should be running".. thanks again smile.gif
User is offlineProfile CardPM
+Quote Post

brickz_0709
RE: Connecting VB2008 To SQL Server 2005
18 Aug, 2008 - 08:28 AM
Post #8

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 10

hi. i've tried what uve suggested and here's the message:

1. ExecuteNonQuery requires an open and available Connection. The connection's current state is closed.

2. An error has occurred while establishing a connection to the server. When connecting o SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: SQL Server Network Interfaces, error: 26 -Error Locating Server / Instance Specified)
User is offlineProfile CardPM
+Quote Post

brickz_0709
RE: Connecting VB2008 To SQL Server 2005
18 Aug, 2008 - 10:52 AM
Post #9

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 10

thanks dineeshd for your suggestion. ive resolved the connection issues. now i have a new problem. im adding info into a table ive created. here is my code:

CODE

Dim objCommand As New SqlCommand("INSERT INTO CustomerProfile& #40;Company,LastName,FirstName,MiddleInitial,Age,Address,Landline,Email,IdNo,Mon
th,Day,Year,Gender)values & #40;@Company,@LastName,@FirstName,@MiddleInitial,@Age,@Address,@Landline,@Email,
@IdNo,@Month,@Day,@Year,@Gender)", objConnection)
            objCommand.Parameters.Add("@Company", SqlDbType.NVarChar, 50).Value = TextBox1.Text
            objCommand.Parameters.Add("@LastName", SqlDbType.Char, 20).Value = TextBoxSurname.Text
            objCommand.Parameters.Add("@FirstName", SqlDbType.Char, 20).Value = TextBoxGiven.Text
            objCommand.Parameters.Add("@MiddleInitial", SqlDbType.Char, 1).Value = TextBoxMiddle.Text
            objCommand.Parameters.Add("@Age", SqlDbType.Int, 2).Value = TextBoxAge.Text
            objCommand.Parameters.Add("@Address", SqlDbType.NVarChar, 50).Value = TextBox78.Text
            objCommand.Parameters.Add("@Landline", SqlDbType.NVarChar, 15).Value = TextBoxLand.Text
            objCommand.Parameters.Add("@Email", SqlDbType.NVarChar, 50).Value = TextBoxEmail.Text
            objCommand.Parameters.Add("@IdNo", SqlDbType.Int, 20).Value = TextBox2.Text
            objCommand.Parameters.Add("@Month", SqlDbType.Char, 20).Value = ComboBoxMonth.SelectedItem.ToString
            objCommand.Parameters.Add("@Day", SqlDbType.Char, 2).Value = ComboBoxDay.SelectedItem.ToString
            objCommand.Parameters.Add("@Year", SqlDbType.Char, 4).Value = ComboBoxYear.SelectedItem.ToString
            objCommand.Parameters.Add("@Gender", SqlDbType.Char, 10).Value = gender


the first line there shows an error, when buillt, that CustomerProfile was an invalid object name. CustomerProfile is the title of the table ive created. can you help me on this? thanks smile.gif
User is offlineProfile CardPM
+Quote Post

brickz_0709
RE: Connecting VB2008 To SQL Server 2005
18 Aug, 2008 - 10:54 AM
Post #10

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 10

hi. ive already resolved the connection issue. thanks for the help. smile.gif

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Connecting VB2008 To SQL Server 2005
18 Aug, 2008 - 10:55 AM
Post #11

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,995



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Please don't create duplicate topics about the same issue. Merged smile.gif
User is online!Profile CardPM
+Quote Post

brickz_0709
RE: Connecting VB2008 To SQL Server 2005
18 Aug, 2008 - 11:12 AM
Post #12

New D.I.C Head
*

Joined: 13 Aug, 2008
Posts: 10

sorry PsychoCoder bout that. just dont know where to post it since it considers 2 categories. smile.gif anyway, the above object name issue still exists. can anyone help me?
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 09:01PM

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month