Results 1 to 5 of 5

Thread: [02/03] SQL Adapter/ Connection

  1. #1

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    48

    [02/03] SQL Adapter/ Connection

    Hey guys, there is something wrong with my Sql connection to my program which I really do not understand what is wrong. I followed the procedures of creating Sql Adapter/ connection from a VB.net 2003 guidebook exactly, then I added a datagrid and fill the form with the dataset. But when I try to run the program, this is what I experience:

    An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

    Additional information: System error.

    Can somebody please explain to me what could have went wrong and what can be done to solve the problem?

    Please note that the original data was done in MS Access and I used the upsizing wizard to copy all the information to MS SQL Server. When I preview the dataset in my SqlDataAdapter, I have no problem viewing all the data in it

  2. #2
    Hyperactive Member
    Join Date
    Aug 2005
    Location
    Lancashire UK
    Posts
    375

    Re: [02/03] SQL Adapter/ Connection

    You need to post the code where your getting the exception, otherwise it will just be guess work

  3. #3

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    48

    Re: [02/03] SQL Adapter/ Connection

    VB Code:
    1. ' Imports Data and SqlClient namespaces...
    2. Imports System.Data
    3. Imports System.Data.SqlClient
    4.  
    5. Public Class frmStaffExp
    6.     Inherits System.Windows.Forms.Form
    7.     Dim SqlConnection1 As SqlConnection = New _
    8.             SqlConnection("Server=(local);Database=pbStaff;User ID=sa;Password=sa;Trusted_Connection=True")
    9.     Dim SqlDataAdapter1 As New SqlDataAdapter
    10.     Dim DsSQLSTAFF1 As DataSet = New DataSet
    11.  
    12.     Private Sub frmStaffExp_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    13.  
    14.         Try
    15.             'Open the database connection...
    16.             SqlConnection1.Open()
    17.             'Now execute the command...
    18.             SqlDataAdapter1.SelectCommand.ExecuteNonQuery()
    19.  
    20.             'Fill the DataSet object with data...
    21.             SqlDataAdapter1.Fill(DsSQLSTAFF1)
    22.         Catch ex As Exception
    23.             MsgBox(ex.Message)
    24.         Finally
    25.             SqlConnection1.Close()
    26.         End Try
    27.  
    28.     End Sub
    29.  
    30.  
    31. End Class

    Here is my code.. These is all that I have for the form. Now if I run, a MsgBox prompt me: "login failed for user 'sa'"

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [02/03] SQL Adapter/ Connection

    Firstly, you wouldn't call ExecuteNonQuery on the SelectCommand because it IS a query. Remove that line altogether as it's the Fill method that retrieves the data.

    Secondly, your adapter has no query and no connection. The fact that you have an SqlDataAdapter doesn't mean that it automatically knows how and where to get data. You have to tell it that by specifying an SQL query AND the connection to use to get the data. This line:
    VB Code:
    1. Dim SqlDataAdapter1 As New SqlDataAdapter
    should be something like this:
    VB Code:
    1. Dim SqlDataAdapter1 As New SqlDataAdapter("SELECT * FROM MyTable", SqlConnection1)
    The SQL code tells it what data to get and the connection tells it where to get it from.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Member
    Join Date
    Jan 2007
    Posts
    48

    Re: [02/03] SQL Adapter/ Connection

    Hi JMC, I did according to what u posted, but still it has the error Login failed for user 'sa'.

    I've searched several other forums and some commented that its the problem of not having a secure Sql connection, some commented not to use sa as the user name as its a bad idea

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