-
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
-
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...
-
The error says,
Incorrect syntax near the keyword 'order'
What's that all about??????
-
ORDER is a keyword in SQL Server eg Order by. Put the table name in square brakets and it will work
Select * from [Order]
-
wooohooo
that worked!
thanks!
(i should have known that really shouldn't i!)
-
No probs - We all have metal blocks sometimes:)
-
I missed it also. Funny how that works...lol
-
yet another problem!
how do i get the objReader data into a dataGrid?
-
set the datasource of the datagrid to the datareader then DataBind the grid so
datagrid1.datasource=objReader
datagrid1.databind
-
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
:confused:
-
sorry only got my asp.net hat with me today
-
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")
-