Database 'Select' Problem

  • Thread starter Thread starter Nitin
  • Start date Start date
N

Nitin

I'm using the following code
**********
command.commandtext="Select * from DB where Acc= 'Net Profit'
connection open
i= command.executescalar()
connection close
***********
This code returns zero(i=0)i use the following code it works fine

**********
command.commandtext="Select * from DB where Acc= 'Net Profit'
Msgbox(command.commandtext)
connection open
i= command.executescalar()
connection close
***********
When I use the messagebox it works fine(i=2000).How can i make this code
work without the message box.!!!

ThanX
 
Nitin,

This code would not work with the messagebox as well.
You have typed it in, can you show the original code because it should work
without the messagebox, however never with this code.

(first copied to a notebook, than copied again and paste it in your message)

There should be a dot between connection open and that is not now therefore
I see this.

Cor
 
Nitin - Like Cor mentions - there's nothing about the MessageBox that's
going to make this code work. It's completely unrelated.

How are you verifying that it's working in the second scenario and not
working in the first?

A few side notes as well. If you're physically concatenating stringsk - you
probably don't want to do that. Instead, use a Paramater
command.Parameters.Add("@Account", SqlDbType.Varchar, 50)
command.Parameters("@Account").Value = "Net Profit"

then set the commandText to "SELECT * FROM DB Where Acc= @Account"

similarly, you should probably wrap that open statement in a
Try/Catch/Finally block. Lots' of things could cause your Connection to
fail - or the command itself could throw an exception - you can't be sure
the connection is closing with this code and that could cause you to leak
connections - somethign you don't want.

Like Cor pointed out though - this isn't the actualy code you're using - can
you post the actual code- we can be of more help if you do.

MsgBox == "From H3ll" --- MessageBox.Show("Is Cooler")
 
The problem is that I am unable to access the last record of the access
database table. I am able to access the record i want to only if I insert a
dummy record which then becomes the last record. I am using this code in a
'Class' which uses one Oledbconnection for various functions( I open and
close the connection again and again).(The message box solution is funny but
it DOES work, i don't know why.). Can any body help!
ThanX
Nitin
 

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

Back
Top