Join 132,656 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,180 people online right now. Registration is fast and FREE... Join Now!
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")
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
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
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
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.
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
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.
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.
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
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)
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:
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
sorry PsychoCoder bout that. just dont know where to post it since it considers 2 categories. anyway, the above object name issue still exists. can anyone help me?