Results 1 to 27 of 27

Thread: Getting connection error

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    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

  2. #2
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Getting connection error

    What is the error?

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Name:  Error.png
Views: 284
Size:  54.7 KB

    This is the error I'm getting Screen shot Attached

  4. #4
    Hyperactive Member marniel647's Avatar
    Join Date
    Aug 2010
    Location
    MSDN Library
    Posts
    259

    Re: Getting connection error

    Quote Originally Posted by chdboy View Post
    Name:  Error.png
Views: 284
Size:  54.7 KB

    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.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Is there any other way to get your data to your database from text boxes(User input)?

  6. #6
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Getting connection error

    You can not do anythign with a database until you connect to it

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Quote Originally Posted by dunfiddlin View Post
    Try ...

    Data Source=localhost\SQLExpress;

    and/or

    Initial Catalog=Employee; (replacing both Attach and Database statements)
    not working same error given

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Or Sybase(Sql anywhere) is better to work with?

  10. #10
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    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!

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Yes I have tried it in another project

  12. #12
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    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.

  13. #13
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: Getting connection error

    Quote Originally Posted by chdboy View Post
    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!

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Quote Originally Posted by DataMiser View Post
    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

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Quote Originally Posted by techgnome View Post
    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 ?

  17. #17

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Quote Originally Posted by dunfiddlin View Post
    And?
    And not working there also

  18. #18
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    Re: Getting connection error

    Quote Originally Posted by chdboy View Post
    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  19. #19

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Quote Originally Posted by techgnome View Post
    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

  20. #20

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Name:  New Error.png
Views: 165
Size:  26.6 KB

    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

  21. #21
    PowerPoster
    Join Date
    Feb 2012
    Location
    West Virginia
    Posts
    14,205

    Re: Getting connection error

    What changes have you made to the code from the OP?
    Why are you using an sdf file?

  22. #22
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  23. #23

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Quote Originally Posted by techgnome View Post
    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

  24. #24
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  25. #25

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Re: Getting connection error

    Quote Originally Posted by techgnome View Post
    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

  26. #26
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,522

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  27. #27

    Thread Starter
    Lively Member
    Join Date
    Sep 2012
    Posts
    119

    Thumbs up 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
  •  



Click Here to Expand Forum to Full Width