-
Sep 20th, 2012, 08:45 AM
#1
Thread Starter
Lively Member
Getting connection error
Created a DB in Visual basics 2010 Express edition and I'm getting problem in this code whenever I run it I have underlined the error point
Code:
Imports System.Data.SqlClient
Public Class Saving
Private Sub Saving_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Button1.Text = "Add to DB"
Button2.Text = "Show DB"
TextBox2.Multiline = True
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim sqCon As New SqlClient.SqlConnection("Server=.\SQLExpress;AttachDbFilename=C:\Users\danial\Documents\Employee.sdf;Database=Employee; Trusted_Connection=Yes;")
Dim sqCmd As New SqlClient.SqlCommand
sqCmd.Connection = sqCon
sqCmd.CommandText = "INSERT INTO [Employee].[dbo].[Empdata] VALUES ('" & TextBox1.Text & "')"
sqCon.Open() 'I AM GETTING ERROR IN THIS LINE
sqCmd.ExecuteNonQuery()
sqCon.Close()
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim sqCon As New SqlClient.SqlConnection("Server=.\SQLExpress;AttachDbFilename=C:\Users\danial\Documents\Employee.sdf;Database=Employee; Trusted_Connection=Yes;")
Dim sqCmd As New SqlClient.SqlCommand
Dim sdrRow As SqlClient.SqlDataReader
sqCmd.Connection = sqCon
sqCmd.CommandText = "SELECT * FROM Empdata"
sqCon.Open()
sdrRow = sqCmd.ExecuteReader()
TextBox2.Text = ""
Do While sdrRow.Read()
TextBox2.Text = TextBox2.Text & sdrRow.GetValue(0) & vbTab
TextBox2.Text = TextBox2.Text & sdrRow.GetValue(1) & vbCrLf
Loop
sdrRow.Close()
sqCon.Close()
End Sub
End Class
-
Sep 20th, 2012, 10:30 AM
#2
Re: Getting connection error
-
Sep 21st, 2012, 02:22 AM
#3
Thread Starter
Lively Member
Re: Getting connection error
This is the error I'm getting Screen shot Attached
-
Sep 21st, 2012, 08:51 AM
#4
Hyperactive Member
Re: Getting connection error
Originally Posted by chdboy
This is the error I'm getting Screen shot Attached
Based on your screenshot it looks like the path of your sql connection is wrong try to check the connection string.
-
Sep 22nd, 2012, 01:44 AM
#5
Thread Starter
Lively Member
Re: Getting connection error
Is there any other way to get your data to your database from text boxes(User input)?
-
Sep 22nd, 2012, 09:29 AM
#6
Re: Getting connection error
You can not do anythign with a database until you connect to it
-
Sep 22nd, 2012, 09:41 AM
#7
Re: Getting connection error
Try ...
Data Source=localhost\SQLExpress;
and/or
Initial Catalog=Employee; (replacing both Attach and Database statements)
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 22nd, 2012, 03:07 PM
#8
Thread Starter
Lively Member
Re: Getting connection error
Originally Posted by dunfiddlin
Try ...
Data Source=localhost\SQLExpress;
and/or
Initial Catalog=Employee; (replacing both Attach and Database statements)
not working same error given
-
Sep 22nd, 2012, 03:09 PM
#9
Thread Starter
Lively Member
Re: Getting connection error
Or Sybase(Sql anywhere) is better to work with?
-
Sep 22nd, 2012, 03:24 PM
#10
Re: Getting connection error
Have you tried a design time connection in a separate project? That will at least tell you whether the database and SQL Express are wired up properly and you can save the connection string to compare it with yours.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 23rd, 2012, 08:34 AM
#11
Thread Starter
Lively Member
Re: Getting connection error
Yes I have tried it in another project
-
Sep 24th, 2012, 08:22 AM
#12
Re: Getting connection error
I notice that you have the file in a users folder. Is this the user that is logged ontp the system when the error occurs? If not that could be your problem as these folders are protected.
-
Sep 24th, 2012, 08:35 AM
#13
Re: Getting connection error
SDF are Compact SQL (SQL CE) files... not normal SQL Server files... so you can't attach them in the same manner. You need to use the SQLCEClient namespace.
-tg
-
Sep 24th, 2012, 09:12 AM
#14
Re: Getting connection error
Originally Posted by chdboy
Yes I have tried it in another project
And?
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Sep 26th, 2012, 09:27 AM
#15
Thread Starter
Lively Member
Re: Getting connection error
Originally Posted by DataMiser
I notice that you have the file in a users folder. Is this the user that is logged ontp the system when the error occurs? If not that could be your problem as these folders are protected.
Yes I use this Laptop and no one else so I'm the one who is logged in always
-
Sep 26th, 2012, 09:29 AM
#16
Thread Starter
Lively Member
Re: Getting connection error
Originally Posted by techgnome
SDF are Compact SQL (SQL CE) files... not normal SQL Server files... so you can't attach them in the same manner. You need to use the SQLCEClient namespace.
-tg
How do I use it can you please explain it in as a sample code ?
-
Sep 26th, 2012, 09:29 AM
#17
Thread Starter
Lively Member
Re: Getting connection error
Originally Posted by dunfiddlin
And?
And not working there also
-
Sep 26th, 2012, 09:49 AM
#18
Re: Getting connection error
Originally Posted by chdboy
How do I use it can you please explain it in as a sample code ?
Really?
Go to MSDN ... type in SQLCEClient in the search... click the search button... one of the results will be this link http://msdn.microsoft.com/en-us/libr...sqlclient.aspx
It's posts like yours that really discourage me... when someone mentions a new topic to you the FIRST instinct should be to go search for it... you need to take some initiative to look this stuff up too you know...
-tg
-
Sep 26th, 2012, 11:05 AM
#19
Thread Starter
Lively Member
Re: Getting connection error
Originally Posted by techgnome
Really?
Go to MSDN ... type in SQLCEClient in the search... click the search button... one of the results will be this link http://msdn.microsoft.com/en-us/libr...sqlclient.aspx
It's posts like yours that really discourage me... when someone mentions a new topic to you the FIRST instinct should be to go search for it... you need to take some initiative to look this stuff up too you know...
-tg
I'm sorry I asked
-
Sep 26th, 2012, 11:10 AM
#20
Thread Starter
Lively Member
Re: Getting connection error
This is the new error popped up when I change in
[code]("Server=localhost\SQLExpress;uid=sa;Data Source=C:\Users\danial\Documents\Visual Studio 2010\Projects\Addtodatabase\Addtodatabase\Database\Employee.sdf;Database=Employee.sdf; Trusted_Connection=Yes;")[code]to the connection string
I have made a new DB naming the same as earlier
-
Sep 26th, 2012, 12:04 PM
#21
Re: Getting connection error
What changes have you made to the code from the OP?
Why are you using an sdf file?
-
Sep 26th, 2012, 12:24 PM
#22
Re: Getting connection error
An SDF file is a compact SQL Server file (SQLCE) ... it isn't meant to be used with a full SQL Server instance...
you're slinging code around with out knowing what it does or any thought to what you're doing...
So let me ask you this: What are you trying to connect to? A database, or a database file? They are not the same thing! How did you create this "database"? You're using two methods there... and neither one is right... you can have a SQL Server install (to include express, or a full-blown version) and connect to a user instance file WHICH IS USUALLY AN MDF Not to mention that just because your file is Employees.sdf, doesn't make the Database name the same... you can have "myreallylongandannoyingname.mdf" but the Database name is "QWERTY" ... meanwhile ... SDF is used for SQL CE (as I keep mentioning) and require a DIFFERENT connection string... AND requires the use of SQLCEClient ... OH, and when you connect to a SQL Server, you give the server name and database name...
Maybe this will help, maybe not: ConnectionStrings.com
-tg
-
Sep 27th, 2012, 09:09 AM
#23
Thread Starter
Lively Member
Re: Getting connection error
Originally Posted by techgnome
An SDF file is a compact SQL Server file (SQLCE) ... it isn't meant to be used with a full SQL Server instance...
you're slinging code around with out knowing what it does or any thought to what you're doing...
So let me ask you this: What are you trying to connect to? A database, or a database file? They are not the same thing! How did you create this "database"? You're using two methods there... and neither one is right... you can have a SQL Server install (to include express, or a full-blown version) and connect to a user instance file WHICH IS USUALLY AN MDF Not to mention that just because your file is Employees.sdf, doesn't make the Database name the same... you can have "myreallylongandannoyingname.mdf" but the Database name is "QWERTY" ... meanwhile ... SDF is used for SQL CE (as I keep mentioning) and require a DIFFERENT connection string... AND requires the use of SQLCEClient ... OH, and when you connect to a SQL Server, you give the server name and database name...
Maybe this will help, maybe not: ConnectionStrings.com
-tg
Just learning the ropes ....
You are very right that I'm slinging code around knowing a little bit what I am doing (A little bit)I was thinking the very same question to myself about .SDF File and .MDF file difference now I know and thank you for that and you are right about that I was making a database from within VSExpress 2010 that is not right (Now I know) so now I will be installing Database and try this code with that only ...and thank you very much techgnome
-
Sep 27th, 2012, 09:38 AM
#24
Re: Getting connection error
If I came across a little harsh I appologize... sometimes it's necessary to slap someone around a little bit to get their attention. A couple things... look in the Database section for the Database FAQ & Tutorials thread... it should be a sticky at the top... it's got some good stuff in there... also when ever you connect to a database... if you're not sure... http://www.connectionstrings.com is a great place to go to get the format right.
-tg
-
Sep 28th, 2012, 10:53 AM
#25
Thread Starter
Lively Member
Re: Getting connection error
Originally Posted by techgnome
If I came across a little harsh I appologize... sometimes it's necessary to slap someone around a little bit to get their attention. A couple things... look in the Database section for the Database FAQ & Tutorials thread... it should be a sticky at the top... it's got some good stuff in there... also when ever you connect to a database... if you're not sure... http://www.connectionstrings.com is a great place to go to get the format right.
-tg
You have done the right thing being harsh otherwise I would have been depending on that someone would provide me the code ...but it's good as me learning .You showed me the light ..otherwise I was hoping to give up.I'm not a quitter generally but when you trying to do some thing which is not your best thing ...then it gets to your nerves .
Thanks techgnome
-
Sep 28th, 2012, 11:06 AM
#26
Re: Getting connection error
give someone code and they'll be productive for 15 minutes... teach them how to code and they can build a career.
-tg
-
Oct 2nd, 2012, 08:02 AM
#27
Thread Starter
Lively Member
Re: Getting connection error
Now this code works with SqlServer Compact Edition file...offcours with little help
Code:
Imports System.Data.SQLServerCE
Dim con as New SqlCeConnection("Data Source=C:\Users\danial\Documents\Visual Studio 2010\Projects\Addtodatabase\Addtodatabase\Database\Employee.sdf")
con.Open
dim Sql as string = "INSERT INTO YourTable (Field1, Field2) VALUES ('Value1', 'Value2')
dim cmd as New SqlCeCommand(Sql, con)
cmd.ExecuteNonQuery
con.close()
now the problem is the one problem resolved then the other arises and I have not started yet ...
anyways thanks for all who helped with the connection error.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|