Results 1 to 13 of 13

Thread: sql server problem

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870

    sql server problem

    hi,

    i am trying to run the code below:

    Code:
    Dim objConnection As New SqlClient.SqlConnection("server=LAPTOP1-NICK\NETSDK;database=orders;trusted_connection=true")
            Dim objCommand As New SqlClient.SqlCommand("Select * from order", objConnection)
            Dim objReader As SqlClient.SqlDataReader
    
            objConnection.Open()
            objReader = objCommand.ExecuteReader
    i get the following error on the last line:
    An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in system.data.dll

    Additional information: System error.



    any idea what's wrong?

    TIA
    NICK

  2. #2
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    Use a try catch block to catch the error and then get the exact error message. We know your connection is ok because your not getting the exception thrown when you open the db connection. The only thing left is the command object. The only thing I can see that might be a problem there is your SQL statement. Maybe you have the table spelt wrong or something...

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    The error says,

    Incorrect syntax near the keyword 'order'

    What's that all about??????

  4. #4
    New Member
    Join Date
    Aug 2000
    Location
    Lancaster, UK
    Posts
    8
    ORDER is a keyword in SQL Server eg Order by. Put the table name in square brakets and it will work

    Select * from [Order]

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    wooohooo

    that worked!

    thanks!

    (i should have known that really shouldn't i!)

  6. #6
    New Member
    Join Date
    Aug 2000
    Location
    Lancaster, UK
    Posts
    8

    Smile

    No probs - We all have metal blocks sometimes

  7. #7
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I missed it also. Funny how that works...lol

  8. #8

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    yet another problem!

    how do i get the objReader data into a dataGrid?

  9. #9
    New Member
    Join Date
    Aug 2000
    Location
    Lancaster, UK
    Posts
    8
    set the datasource of the datagrid to the datareader then DataBind the grid so
    datagrid1.datasource=objReader
    datagrid1.databind

  10. #10

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    that works(i think) in asp.net but doesn't seem to work in VB.net.

    Code:
    datagrid1.datasource = objReader
    gives the error

    An unhandled exception of type 'System.Exception' occurred in system.windows.forms.dll

    Additional information: Complex DataBinding accepts as a data source either an IList or an IListSource

    and datagrid1 doesn't have a method called databind, only databindings


  11. #11
    New Member
    Join Date
    Aug 2000
    Location
    Lancaster, UK
    Posts
    8
    sorry only got my asp.net hat with me today

  12. #12
    New Member
    Join Date
    Aug 2000
    Location
    Lancaster, UK
    Posts
    8
    nswan - you cannot use datareader to bind to a datagrid in windows forms. Youll have to use a dataset


    Dim data_adapter As SqlDataAdapter

    data_adapter = New SqlDataAdapter("SELECT * FROM Contacts ORDER BY LastName, FirstName", objConn)

    ' Map Table to Contacts.
    data_adapter.TableMappings.Add("Table", "Contacts")

    ' Fill the DataSet.
    m_DataSet = New DataSet()
    data_adapter.Fill(m_DataSet)

    ' Bind the DataGrid control to the Contacts DataTable.
    datagrid1.DataSource = m_DataSet.Tables("Contacts")

  13. #13

    Thread Starter
    Fanatic Member
    Join Date
    Oct 2000
    Location
    Reading, UK
    Posts
    870
    thanks steve

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