Solved

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

Posted on 2015-02-23
3
381 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now