?
Solved

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

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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month12 days, 22 hours left to enroll

777 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