Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I am trying to update a record in an access database. and I am getting the following error...

 

 

argument 'Prompt' cannot be converted to type 'String'

 

 

My SQL statement is as the following:

 

SQL = "UPDATE Products SET TotalAmount = " & Trim(txtBoxTotal.Text) & ", Edited = '" & Trim(a) & "' WHERE ProductName = '" & cBoxCat.Text & "'"

 

the variable a = DataTime.Now.ToShortDateString

 

if someone knows what i am doing wrong please help.

Posted (edited)
How would i go about that?

 

There are tons of reference out there about parameterized query. Here's one of those many: http://aspnet101.com/aspnet101/tutorials.aspx?id=1

 

Even MSDN should provide you with a quick start on using parameterized query.

 

Back to your problem. Can you give us more detail about your code? Btw what type of exception did you code produce?

 

Just trying to help. :D

Edited by amir100
Amir Syafrudin
Posted

When I have this as my code it works fine.... but

 

 

    Function GetInsertSQL()

       Dim a As Integer
       txtBoxNew.Text = txtBoxTotal.Text - txtAmount.Text
       Dim SQL As String
       Dim Q As String = Chr(34)
       SQL = "UPDATE Products SET TotalAmount = " & Trim(txtBoxNew.Text) & " WHERE ProductName = '" + cboxCat.Text + "'"
       Return SQL
   End Function

 

but i want to add txtboxtotal and txtAmount.... when I do that.. it makes them strings and then gives me an error

  • Administrators
Posted

Firstly make sure you put

Option Strict On

at the top of all your vb files - this will prevent a lot of run time errors slipping past the compiler and will highlight where the problem you are having occurs.

 

In regards to parameterising your quesry somethink lile

       cmd.CommandText = "UPDATE Products SET TotalAmount = ? WHERE ProductName = ?"
       cmd.Parameters.AddWithValue("TotalAmount", txtboxnew.text)
       cmd.Parameters.AddWithValue("ProductName", cboxCat.Text)

should get you started.

Posting Guidelines FAQ Post Formatting

 

Intellectuals solve problems; geniuses prevent them.

-- Albert Einstein

Posted

Arithmetic operations on strings

 

but i want to add txtboxtotal and txtAmount.... when I do that.. it makes them strings and then gives me an error

 

The textbox contents need to be converted to a numeric data type (such as Decimal) to perform the addition:

 

Dim newTotal As Decimal

newTotal = Decimal.Parse(txtBoxTotal.Text) + Decimal.Parse(txtAmount.Text)
txtBoxNew.Text = newTotal.ToString()

 

Of course it would be prudent to encapsulate this with code to handle the possible FormatException.

 

Good luck :)

Never trouble another for what you can do for yourself.
Posted

Hi MrPaul,

 

Slightly off topic but what is the difference between

 

Decimal.Parse(TextBox1.Text)

 

and

 

CDec(TextBox1.Text)

 

Do they both give the same result, personally i've always used the latter, is there a difference?

 

Thanks

Posted

CDec vs Parse

 

The C* functions such as CDec, CBool and CInt are keywords in the Visual Basic language. C# does not have any comparable keywords beside the generic cast operator, as there are methods within the .Net framework itself, such as Parse, which can perform conversions. Therefore coming from a C# background I am not used to using Visual Basic-specific features such as this, as it appears to me they primarily exist to provide backwards compatibility with VB6 code.

 

Incidentally, MSDN actually recommends the C* keywords be used over the more general methods provided in the framework. In this case I might agree since they are keywords and not merely utility functions such as Trim. However, since the Parse method is part of the framework it can be applied to any .Net language, whereas CDec cannot. Using CDec almost certainly compiles to a call to Parse anyway.

 

As you point out, the results are usually the same, but they are not guaranteed to be, especially with the backwards compatibility functions within the Microsoft.VisualBasic namespace. I know the Val function in particular acts differently from similar methods in the framework.

 

As I've said elsewhere I recommend all Visual Basic programmers remove the reference to Microsoft.VisualBasic and use standard .Net methods wherever possible. Since CDec is a keyword this would still be useable.

Never trouble another for what you can do for yourself.

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...