Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I want to see if a value in the field 'Key2' >= 11000

 

I use this SQL statement

 

"Select * from Contacts Where val(Key2)>=" & txtVal.text

 

txtVal is the text box that contains the value 11000

 

This gives me a DataType Mismatch. Any idea why?

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

Posted

Close Jon.

 

11000 is a string, but Key2 is a string as well, which is why I am trying to convert it to a numeric.

 

I also tried replacing Val with cast(key2 as long) and cast(rtrim(key2) as long) and got an exception error.

Read the Fovean Chronicles

Because you just can't spend your whole day programming!

  • *Experts*
Posted

You'll need to make them both integers:

Dim inttxtVal as Integer
inttxtVal = DirectCast(txtVal.text, Integer)

"SELECT* FROM Contacts WHERE CAST(Key2 AS Integer)>=" & inttxtVal

 

 

 

Jon

Posted

jfackler:

 

Actually that would be a bit silly as you'd unbox (string to int) then box (int to string) the value.

 

Then again I'm probably wrong and just myself look like a doofus.

Gamer extraordinaire. Programmer wannabe.
Posted

TheWizardofInt:

 

I think he ment Integer.Parse(txtVal.Text). (or is it int.Parse()? Either way..) DirectCast() is for casting object to object.

 

EDIT:

Ohya, and if Integer.Parse() throws an exception then the value is not a valid integer. IE: 23k23 will throw an exception.

Gamer extraordinaire. Programmer wannabe.
  • *Experts*
Posted

Wyrd,

Right on all counts, synaptic lubricant deficiency on my part.

 

For those unaware and looking to this forum for direction:

 

DirectCast is special in that conversions from type Object to any other type are performed as a direct cast down the hierarchy � all of the special conversion behaviors from Object are ignored. When converting an expression of type Object whose run-time type is a primitive value type, DirectCast throws a System.InvalidTypeException exception if the specified type is not the same as the run-time type of the expression. If the specified type and the run-time type of the expression are the same, however, the run-time performance of DirectCast is better than that of CType.

 

In addition, the concatenation (&) operator can convert a number to a string implicitly. Which is what it will do in my code above.

 

Thanks for the slight correction *zzzztt* I'm better now.

 

Jon

  • *Experts*
Posted

Wiz,

I believe the answer to your problem lies in the use of a selection parameter.

Parameters are indicated either with a placeholder (a question mark) or with a named parameter variable. Parameters for queries involving OleDbCommand objects use question marks; queries that use SqlCommand objects use named parameters.

 

In your application, you prompt the user for a value. You then set the parameter to that value and run the command.

 

We all knew that of course.

 

Jon

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...