Solved

Error converting data type varchar to numeric in UPDATE statement in VB.NET

Posted on 2015-02-23
3
418 Views
Last Modified: 2015-03-03
Hi all.

I'm getting the error converting data type varchar to numeric error whenever the txtMinQty textbox is left blank, I want to be able to UPDATE the field MinQty to NULL if the textbox is left blank or if the number is cleared out by the end user. If it's not blank then update it with the number in the text box. Unfortunately, it's not letting me update the field to NULL. Below is my code, what am I doing wrong? Thank you in advance.

cmd.CommandText = "UPDATE PriceSheet_PendingHeader SET MinQty = '" & If(String.IsNullOrWhiteSpace(txtMinQty.Text), DBNull.Value, Integer.Parse(txtMinQty.Text)) & "' WHERE PriceSheetPendingID = @PriceSheetPendingID_Update"
        cmd.Parameters.AddWithValue("@PriceSheetPendingID_Update", txtPriceSheetPendingID.Text)

        cmd.ExecuteNonQuery()

Open in new window

0
Comment
Question by:printmedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 40
ID: 40626348
What is the error that you get.

Is the field marked to accept Null values in the table definition?

Why not simply let the zero-length string go into the VarChar. In many scenarios, if this is decided from the start (so as not to break old queries and stored procedures), this can be considered as the Null.
0
 

Accepted Solution

by:
printmedia earned 0 total points
ID: 40626364
Nevermind, I figured it out by doing the following:


If (String.IsNullOrWhiteSpace(txtMinQty.Text)) Then
            cmd.CommandText = "UPDATE PriceSheet_PendingHeader SET MinQty = NULL WHERE PriceSheetPendingID = @PriceSheetPendingID_Update1"
            cmd.Parameters.AddWithValue("@PriceSheetPendingID_Update1", txtPriceSheetPendingID.Text)
        Else
            cmd.CommandText = "UPDATE PriceSheet_PendingHeader SET MinQty = " & Integer.Parse(txtMinQty.Text) & " WHERE PriceSheetPendingID = @PriceSheetPendingID_Update2"
            cmd.Parameters.AddWithValue("@PriceSheetPendingID_Update2", txtPriceSheetPendingID.Text)
        End If

cmd.ExecuteNonQuery()

Open in new window

0
 

Author Closing Comment

by:printmedia
ID: 40641433
Solved my own question.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: Kraeven
Introduction Remote Share is a simple remote sharing tool, enabling you to see, add and remove remote or local shares. The application is written in VB.NET targeting the .NET framework 2.0. The source code and the compiled programs have been in…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question