[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2015-02-23
3
Medium Priority
?
473 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

649 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