473,699 Members | 2,892 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sql vs Oledb error

Below is the code for 2 separate functions. The first one gets data from an
Access database and works fine. The second one is almost a duplicate except
it gets data from an SQL database. It works fine if I insert the actual
value for the criteria. In other words, if I insert "mscott" in the place of
"txtData.Te xt", it will return the expected results. But if I use the
criteria from my input field, I get the following error:

"Invalid column name 'mscott'." (mscott = value from text box)

Can anyone tell me how to correct this and why is this different in SQL?

*************** *************** *************** *********
Private Sub GetDataAccess()
Using conn As New OleDb.OleDbConn ection _
("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source= _
C:\Data\Access\ ItemMaster.mdb" )
Using com As OleDb.OleDbComm and = conn.CreateComm and()
conn.Open()
com.CommandType = Data.CommandTyp e.Text
com.CommandText = "Select Dept From Security Where UserID =
" & _
txtData.Text
com.Parameters. Add("UserID", OleDb.OleDbType .VarChar).Value
= _
txtData.Text
Dim strDept As String = String.Empty

Try
strDept = com.ExecuteScal ar().ToString()
Catch ex As NullReferenceEx ception
MsgBox("UserID not found....")
txtData.Text = String.Empty
End Try
lblData.Text = strDept
conn.Close()
End Using
End Using
End Sub

*************** *************** *************** **********
Private Sub GetDataSQL1()
Using conn As New SqlClient.SqlCo nnection _
("Server=KIN-SSQL02;" & "Database=F TQ;" & "User ID=abc;" _
& "Password=123;" )
Using com As SqlClient.SqlCo mmand = conn.CreateComm and()
conn.Open()
com.CommandType = Data.CommandTyp e.Text
com.CommandText = "Select Dept from Security Where UserID =
" & _
txtData.Text
com.Parameters. Add("UserID", SqlDbType.VarCh ar).Value = _
txtData.Text
Dim strData As String = String.Empty

Try
strData = com.ExecuteScal ar().ToString()
Catch ex As NullReferenceEx ception
MsgBox("User ID not found....")
txtData.Text = String.Empty
End Try
lblData.Text = strData
conn.Close()
End Using
End Using

End Sub

*************** *************** *************** *******

Thanks in advance,

Mike

Sep 20 '07 #1
3 1420
Try changing it to this:

//////////////////////////////////
Private Sub GetDataSQL1()
Using conn As New SqlClient.SqlCo nnection("Serve r=KIN-
SSQL02;Database =FTQ;User ID=abc;Password =123;"), _
com As SqlClient.SqlCo mmand = conn.CreateComm and()
conn.Open()
com.CommandType = Data.CommandTyp e.Text
com.CommandText = "Select Dept from Security Where UserID =
@UserId"
com.Parameters. Add("@UserID", SqlDbType.VarCh ar).Value =
txtData.Text
Dim strData As String = String.Empty

Try
strData = com.ExecuteScal ar().ToString()
Catch ex As NullReferenceEx ception
MsgBox("User ID not found....")
txtData.Text = String.Empty
End Try
lblData.Text = strData
End Using
End Sub
////////////////////////////
Let me know how it works.

Thanks,

Seth Rowe


Sep 20 '07 #2
Thanks Seth....that works great.

"rowe_newsgroup s" wrote:
Try changing it to this:

//////////////////////////////////
Private Sub GetDataSQL1()
Using conn As New SqlClient.SqlCo nnection("Serve r=KIN-
SSQL02;Database =FTQ;User ID=abc;Password =123;"), _
com As SqlClient.SqlCo mmand = conn.CreateComm and()
conn.Open()
com.CommandType = Data.CommandTyp e.Text
com.CommandText = "Select Dept from Security Where UserID =
@UserId"
com.Parameters. Add("@UserID", SqlDbType.VarCh ar).Value =
txtData.Text
Dim strData As String = String.Empty

Try
strData = com.ExecuteScal ar().ToString()
Catch ex As NullReferenceEx ception
MsgBox("User ID not found....")
txtData.Text = String.Empty
End Try
lblData.Text = strData
End Using
End Sub
////////////////////////////
Let me know how it works.

Thanks,

Seth Rowe


Sep 20 '07 #3
On Sep 20, 12:22 pm, MikeS <Mi...@discussi ons.microsoft.c omwrote:
Below is the code for 2 separate functions. The first one gets data from an
Access database and works fine. The second one is almost a duplicate except
it gets data from an SQL database. It works fine if I insert the actual
value for the criteria. In other words, if I insert "mscott" in the place of
"txtData.Te xt", it will return the expected results. But if I use the
criteria from my input field, I get the following error:

"Invalid column name 'mscott'." (mscott = value from text box)

Can anyone tell me how to correct this and why is this different in SQL?

*************** *************** *************** *********
Private Sub GetDataAccess()
Using conn As New OleDb.OleDbConn ection _
("Provider=Micr osoft.Jet.OLEDB .4.0;Data Source= _
C:\Data\Access\ ItemMaster.mdb" )
Using com As OleDb.OleDbComm and = conn.CreateComm and()
conn.Open()
com.CommandType = Data.CommandTyp e.Text
com.CommandText = "Select Dept From Security Where UserID =
" & _
txtData.Text
com.Parameters. Add("UserID", OleDb.OleDbType .VarChar).Value
= _
txtData.Text
Dim strDept As String = String.Empty

Try
strDept = com.ExecuteScal ar().ToString()
Catch ex As NullReferenceEx ception
MsgBox("UserID not found....")
txtData.Text = String.Empty
End Try
lblData.Text = strDept
conn.Close()
End Using
End Using
End Sub

*************** *************** *************** **********
Private Sub GetDataSQL1()
Using conn As New SqlClient.SqlCo nnection _
("Server=KIN-SSQL02;" & "Database=F TQ;" & "User ID=abc;" _
& "Password=123;" )
Using com As SqlClient.SqlCo mmand = conn.CreateComm and()
conn.Open()
com.CommandType = Data.CommandTyp e.Text
com.CommandText = "Select Dept from Security Where UserID =
" & _
txtData.Text
com.Parameters. Add("UserID", SqlDbType.VarCh ar).Value = _
txtData.Text
Dim strData As String = String.Empty

Try
strData = com.ExecuteScal ar().ToString()
Catch ex As NullReferenceEx ception
MsgBox("User ID not found....")
txtData.Text = String.Empty
End Try
lblData.Text = strData
conn.Close()
End Using
End Using

End Sub

*************** *************** *************** *******

Thanks in advance,

Mike
If the column UserID is a varchar, then the value from txtData.Text
must be wrapped in single quotes.

The only difference between the way you were trying it and the way a
suggested workaround works, is the workaround specifies the value as a
parameter, where the "wrapping in single quotes" is not applicable.

Sep 20 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1905
by: Error while executing SP | last post by:
Hi, I am getting an error while executing a sp from Oracle database. Can you let me know what could be the problem? here is the code using(System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection()) { cn.ConnectionString="Provider=OraOLEDB.Oracle;Password=STORED_OBJ;User ID=STORED_OBJ;Data Source=PDCDGL1.NA.HPFS.COM;Extended Properties=;Persist Security Info=False;PLSQLRSet=1;"; cn.Open(); //String strProcName =...
1
1780
by: N S S | last post by:
I Get the following Error ================ Error =============================== Procedure 'GetCetgoriesOrProducts' expects parameter '@CategoryID', which was not supplied. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Data.OleDb.OleDbException: Procedure...
0
5280
by: silesius | last post by:
I've been using VS 2003 to develop a webapplication using C#. Today I exported the application to a remote webserver I begun experiencing problems. It's a simple application that retrieves some information from an Access database. It worked fine when I ran on my computer. I use a parameterized query to retrieve some customer specific information. Customer logs in, I store the username they log in with in the session state and on the...
1
6885
by: T8 | last post by:
I have a asp.net (framework 1.1) site interfacing against SQL 2000. It runs like a charm 99% of the time but once in a while I get the following "unspecified error". Sometimes it would resolve by itself (asp.net recycled?); it also can be solved by restarting IIS. Any ideas what cause(s) this to happen? System.Data.OleDb.OleDbException: Unspecified error at System.Data.OleDb.OleDbDataReader.ProcessResults(Int32 hr) at...
1
1488
by: EMW | last post by:
I use the following function to read an Excel worksheet into a dataset: Public Function GetDataFromExcel(ByVal FileName As String, ByVal ds As DataSet) As Boolean Try Dim strConn As String = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & FileName & "; Extended Properties=Excel 8.0;" Dim objConn As New System.Data.OleDb.OleDbConnection(strConn) objConn.Open() ' Create objects ready to grab data
0
2768
by: silesius | last post by:
I've been using VS.NET 2003 to develop a webapplication using C#. Today I exported the application to another webserver I begun experiencing problems. It's a simple application that retrieves some information from an Access database. It worked fine when I ran on my home computer. I use a datagrid witha a dataset and a parameterized query to retrieve some customer specific information. Customer logs in, I store the username they log in...
3
8828
by: Brian Foree | last post by:
I am developing an ASP.NET application that uses Access 2000 as its backend, and have just started getting the following error on 2 ASP.NET pages that had been working until late last week (and I don't think I made any changes to either page other than changing the user control that creates the header). Server Error in '/myApp' Application. ---------------------------------------------------------------------------- ----
2
2854
by: Martin | last post by:
Hi, I currently have an application that connects to an MS ACCESS database. This application uses an OLEDB connection string for MS ACCESS. Now, I'd like to upsize the application so I converted the db to SQL SERVER and this went well. Next thing I changed the connection string from OLEDB for MS ACCESS to OLEDB for SQL SERVER. I expected this to work resonably well, however I found that it never worked
0
5659
by: NicK chlam via DotNetMonster.com | last post by:
this is the error i get System.Data.OleDb.OleDbException: Syntax error in INSERT INTO statement. at System.Data.Common.DbDataAdapter.Update(DataRow dataRows, DataTableMapping tableMapping) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet, String srcTable) at System.Data.Common.DbDataAdapter.Update(DataSet dataSet) at TryThis.Form1.save() in C:\Documents and Settings\Nick\My Documents\...
5
9645
by: petro | last post by:
Hello all, My asp.net web application works on my machine but I get the following error on our test web server, There is only one oracle home on the test server. Does anyone know how to resolve this error? Oracle error occurred, but error message could not be retrieved from Oracle. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the...
0
8704
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9192
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9054
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
6546
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5879
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4637
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3071
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2362
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2015
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.