Solved

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

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Well, all of us have seen the multiple EXCEL.EXE's in task manager that won't die even if you call the .close, .dispose methods. Try this method to kill any excels in memory. You can copy the kill function to create a check function and replace the …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

708 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

15 Experts available now in Live!

Get 1:1 Help Now