473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

exception with special characters

The following query runs perfectly fine with SQL Query Analyzer:

insert into TableName(ID,ParamName,ParamValue) values
(27706,'Param6','\0V*\0\0\0\0\0')

But if i try to execute the same query using OdbcCommand object, it throws
an exception. Any help plz?
Nov 16 '05 #1
10 2898
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote:
The following query runs perfectly fine with SQL Query Analyzer:

insert into TableName(ID,ParamName,ParamValue) values
(27706,'Param6','\0V*\0\0\0\0\0')

But if i try to execute the same query using OdbcCommand object, it throws
an exception. Any help plz?


Have you tried using parameters instead of including the nulls directly
in the SQL statement?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #2
I am having problems with parameterized queries with OdbcCommand object.
Event if i set it with ? instead of @ still it throws an exception. Can you
guide me a little on this? Do you think that it will solve the problem?

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote:
The following query runs perfectly fine with SQL Query Analyzer:

insert into TableName(ID,ParamName,ParamValue) values
(27706,'Param6','\0V*\0\0\0\0\0')

But if i try to execute the same query using OdbcCommand object, it throws
an exception. Any help plz?


Have you tried using parameters instead of including the nulls directly
in the SQL statement?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote:
I am having problems with parameterized queries with OdbcCommand object.
Event if i set it with ? instead of @ still it throws an exception. Can you
guide me a little on this? Do you think that it will solve the problem?


What exception is it throwing, exactly? Could you post a short but
complete program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Whether or not it solves this particular problem, parameterisation is
important enough that it's worth getting it working now, and then
seeing whether it helps with your immediate problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #4
Thanx Jon, You have always been so helpful. Here is the code that is causing
problem with parameterized queries (I dont want to use SqlCommand object
since in my whole application, i have used OdbcCommand object):

OdbcCommand cmd = new OdbcCommand();

OdbcConnection cn = new OdbcConnection("dsn=test;uid=;pwd=");

string query = "insert into Person (name,phone) values(?name,?phone)";
cmd.CommandType = CommandType.Text;

cmd.Connection = cn;

cmd.CommandText = query;
OdbcParameter param1 = new OdbcParameter();

param1.DbType = DbType.String;

param1.ParameterName = "?name";

param1.Value = "Test";

cmd.Parameters.Add(param1);

OdbcParameter param2 = new OdbcParameter();

param2.DbType = DbType.String;

param2.ParameterName = "?phone";

param2.Value = "123456";

cmd.Parameters.Add(param2);
cn.Open();
try

{

cmd.ExecuteNonQuery();

}

catch(Exception ex)

{

MessageBox.Show(ex.ToString());

}

- An exception is thrown that says " Message "ERROR [07002] [Microsoft][ODBC
Microsoft Access Driver] Too few parameters. Expected 4."
- I have also tried @ in place of ?, still doesnt work. However, the
exception text becomes a bit different in this case. Exception thrown is "
Message "ERROR [07002] [Microsoft][ODBC Microsoft Access Driver] Too few
parameters. Expected 2."

Please tell me what i am doing wrong? Thanx again.

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote:
I am having problems with parameterized queries with OdbcCommand object.
Event if i set it with ? instead of @ still it throws an exception. Can you guide me a little on this? Do you think that it will solve the problem?


What exception is it throwing, exactly? Could you post a short but
complete program which demonstrates the problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Whether or not it solves this particular problem, parameterisation is
important enough that it's worth getting it working now, and then
seeing whether it helps with your immediate problem.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #5
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote:
Thanx Jon, You have always been so helpful. Here is the code that is causing
problem with parameterized queries (I dont want to use SqlCommand object
since in my whole application, i have used OdbcCommand object):

OdbcCommand cmd = new OdbcCommand();
OdbcConnection cn = new OdbcConnection("dsn=test;uid=;pwd=");
string query = "insert into Person (name,phone) values(?name,?phone)";

cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
cmd.CommandText = query;

OdbcParameter param1 = new OdbcParameter();
param1.DbType = DbType.String;
param1.ParameterName = "?name";
param1.Value = "Test";
cmd.Parameters.Add(param1);


<snip>

I believe the problem is one which is slightly obscurely documented, in
OdbcCommand.Parameters:

<quote>
When CommandType is set to Text, the .NET Framework Data Provider for
ODBC does not support passing named parameters to an SQL statement or
to a stored procedure called by an OdbcCommand. In either of these
cases, use the question mark (?) placeholder. For example:

SELECT * FROM Customers WHERE CustomerID = ?

The order in which OdbcParameter objects are added to the
OdbcParameterCollection must directly correspond to the position of the
question mark placeholder for the parameter in the command text.
</quote>

In other words, you should use:

"insert into Person (name, phone) values (?, ?)"

At that point, it doesn't matter what names you give the parameters,
but they have to be in the right order.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6
Thanx jon....It solved the problem of parameterized queries but i have tired
inserting that
'\0V*\0\0\0\0\0' as a parameter but that still doesnt work :(
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote:
Thanx Jon, You have always been so helpful. Here is the code that is causing problem with parameterized queries (I dont want to use SqlCommand object
since in my whole application, i have used OdbcCommand object):

OdbcCommand cmd = new OdbcCommand();
OdbcConnection cn = new OdbcConnection("dsn=test;uid=;pwd=");
string query = "insert into Person (name,phone) values(?name,?phone)";

cmd.CommandType = CommandType.Text;
cmd.Connection = cn;
cmd.CommandText = query;

OdbcParameter param1 = new OdbcParameter();
param1.DbType = DbType.String;
param1.ParameterName = "?name";
param1.Value = "Test";
cmd.Parameters.Add(param1);


<snip>

I believe the problem is one which is slightly obscurely documented, in
OdbcCommand.Parameters:

<quote>
When CommandType is set to Text, the .NET Framework Data Provider for
ODBC does not support passing named parameters to an SQL statement or
to a stored procedure called by an OdbcCommand. In either of these
cases, use the question mark (?) placeholder. For example:

SELECT * FROM Customers WHERE CustomerID = ?

The order in which OdbcParameter objects are added to the
OdbcParameterCollection must directly correspond to the position of the
question mark placeholder for the parameter in the command text.
</quote>

In other words, you should use:

"insert into Person (name, phone) values (?, ?)"

At that point, it doesn't matter what names you give the parameters,
but they have to be in the right order.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #7
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote:
Thanx jon....It solved the problem of parameterized queries but i have tired
inserting that
'\0V*\0\0\0\0\0' as a parameter but that still doesnt work :(


What exception do you get when you're using that as a parameter value?
(And how exactly are you setting the value?)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #8
I am using this approach

//the things that i have not pasted are the same as before
string query = "insert into Person (name,phone) values(?,?)";
OdbcParameter param1 = new OdbcParameter();
param1.DbType = DbType.String;
param1.ParameterName = "?name";
param1.Value = "\0V*\0\0\0\0\0";
cmd.Parameters.Add(param1);
It is *not* thorwing any exceptions now. Instead, it is adding a row in the
database but the column in which i am trying to insert \0V*\0\0\0\0\0 is
empty
"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote: Thanx jon....It solved the problem of parameterized queries but i have tired inserting that
'\0V*\0\0\0\0\0' as a parameter but that still doesnt work :(


What exception do you get when you're using that as a parameter value?
(And how exactly are you setting the value?)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #9
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote:
I am using this approach

//the things that i have not pasted are the same as before
string query = "insert into Person (name,phone) values(?,?)";
OdbcParameter param1 = new OdbcParameter();
param1.DbType = DbType.String;
param1.ParameterName = "?name";
param1.Value = "\0V*\0\0\0\0\0";
cmd.Parameters.Add(param1);


It is *not* thorwing any exceptions now. Instead, it is adding a row in the
database but the column in which i am trying to insert \0V*\0\0\0\0\0 is
empty


Ah - is it *definitely* empty though, or does it just display as being
empty on some displays? Don't forget that some things (like the VS.NET
debugger in some cases, at least for VS.NET 2002) won't display
anything beyond the first null character in a string. If you fetch the
string from the database and print it out in the console (not the
debugger), is it still empty? What happens if you run a select statment
which returns the number of characters in a string?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #10
Thanx a lot Jon for all your help. I checked it again, it was showing it as
empty in the database but if i debug it, the string *does* contain the value
in it. So the insertion of unicode characters using Parameterized query
approach is working perfectly fine. Thanx again for all your help :)

Best Regards
Wajih

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Wajih-ur-Rehman <wr*****@ro1.adiscon.com> wrote:
I am using this approach

//the things that i have not pasted are the same as before
string query = "insert into Person (name,phone) values(?,?)";
OdbcParameter param1 = new OdbcParameter();
param1.DbType = DbType.String;
param1.ParameterName = "?name";
param1.Value = "\0V*\0\0\0\0\0";
cmd.Parameters.Add(param1);
It is *not* thorwing any exceptions now. Instead, it is adding a row in

the database but the column in which i am trying to insert \0V*\0\0\0\0\0 is
empty


Ah - is it *definitely* empty though, or does it just display as being
empty on some displays? Don't forget that some things (like the VS.NET
debugger in some cases, at least for VS.NET 2002) won't display
anything beyond the first null character in a string. If you fetch the
string from the database and print it out in the console (not the
debugger), is it still empty? What happens if you run a select statment
which returns the number of characters in a string?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11

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

Similar topics

7
by: Roy W. Andersen | last post by:
I've been searching google about this for days but can't find anything, so I'm hoping someone here can help me out. I'm trying to create zip-files without needing the zip-file extension in PHP,...
3
by: Barry Olly | last post by:
Hi, I'm working on a mini content management system and need help with dealing with special characters. The input are taken from html form which are then stored into a varchar column in...
5
by: Sakharam Phapale | last post by:
Hi All, I am using an API function, which takes file path as an input. When file path contains special characters (@,#,$,%,&,^, etc), API function gives an error as "Unable to open input file"....
17
by: Carl Mercier | last post by:
Hi, Is it possible to use special characters like \n or \t in a VB.NET string, just like in C#? My guess is NO, but maybe there's something I don't know. If it's not possible, does anybody...
8
by: david.lindsay.green | last post by:
Hello all, I am quite new a web scripting and making web pages in general and I have stumbled across a problem I have as yet been unable to solve. I am trying to take the contents of a textarea box...
5
by: Doc | last post by:
Hello! I'm experiencing a little problem counting the number of characters in a textarea on a html page. This is the content type of my HTML document content="text/html; charset=iso-8859-1" ...
1
by: sonald | last post by:
Dear All, I am working on a module that validates the provided CSV data in a text format, which must be in a predefined format. We check for the : 1. Number of fields provided in the text file,...
7
by: Trac Bannon | last post by:
When I load XML from a file into a dotNet XMLDataDocument, the UTF-8 codes are resolved but the 5 special XML entities are not. How can I force those 5 special character types to be translated?
3
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class....
0
by: AAaron123 | last post by:
Been playing with asp:changepassword and have it looking OK except that I can't elininate or change the title at the top that says "Change Your Password". It's a repeat of my pages title. ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.