debugging an sql format string

G

Guest

Hi,
I am having trouble with a little piece of code that formats an SQL string.
I am not looking for folks to debug my code, what I would like to know is how
can I see the string that I've built before it is executed, so I can fix
whatever is wrong.

//snippet
....lots of code building the string

sql = String.Format(sql, values.ToArray());

....more code, data connection, sql command, etc..

The exception is being thrown right at this line (above) and I would like to
view it. If I were doing vb, I would just say msgbox sql

what is the asp.net - c# way of doing this?

Thanks.
 
C

Cor Ligthert

Ramonred,

The same as in VBNet however than only one ; behind it and in the right
case.
MessageBox.Show(sql);

I hope this helps?

Cor
 
G

Guest

ramonred said:
Hi,
I am having trouble with a little piece of code that formats an SQL string.
I am not looking for folks to debug my code, what I would like to know is how
can I see the string that I've built before it is executed, so I can fix
whatever is wrong.

//snippet
...lots of code building the string

sql = String.Format(sql, values.ToArray());

...more code, data connection, sql command, etc..

The exception is being thrown right at this line (above) and I would like to
<snip>

I assume you mean that the exception occurs on the line starting with
"sql =", which means that you cannot show it through a message box since
that statement would have to occur after the exception has been thrown.

It would help knowing which kind of exception you have, but testing the
following simple code worked:

String sql = "SELECT * FROM tablename WHERE key = {0}";
ArrayList values = new ArrayList();
values.Add(123);
sql = String.Format(sql, values.ToArray());
MessageBox.Show(sql);

I should mention that the best way to deal with sql and values like this
is using parameters, which would remove any problems you might have with
formatting the values correctly (especially datetime values), but I'll
assume you can't use parameters in this case. Just make sure you don't
set yourself up for an sql injection attack.

If the exception occurs in String.Format, make sure you got the right
number of {xxx} specifies corresponding to the entries in the values
collection, and that you haven't used invalid format specifiers.

If you still can't figure out the cause of the error, post some more
code and the exception type and message.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top