Solved

Error converting varchar to number

Posted on 2012-04-11
3
367 Views
Last Modified: 2012-04-11
I have code behind a button on my website that runs a stored procedure.  The user uses a date picker control to insert a date and then clicks the button to run the stored procedure.  When I click the button I get an error 'SQLException: error converting varchar to number' .

Below is my code for my button. I have also attached my stored procedure code. When I execute the procedure in SQL Management Studio with @enddate = N'20120331' it works fine.

Not sure what I need to change on my code behind my button click.



 Dim oConn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("CMSConnectionString").ConnectionString)
        Dim sql As New System.Data.SqlClient.SqlCommand()
        sql.Connection = oConn
        oConn.Open()

        'Run stored Procedure sp_Update_Job_PO_Amt_Adhoc

        sql.CommandText = "sp_Update_Job_PO_Amt_Adhoc"
        sql.CommandType = Data.CommandType.StoredProcedure
        'Declare parameters
        'sql.Parameters.AddWithValue("@begdate", Data.SqlDbType.Char)
        sql.Parameters.AddWithValue("@enddate", Data.SqlDbType.Char)
        'Assign control values to parameters
        'sql.Parameters("@begdate").Value = rdpBegDate
        sql.Parameters("@enddate").Value = rdpenddate.SelectedDate.Value.ToShortDateString
        oConn.Close()

        Try
            'open connection
            oConn.Open()
            sql.ExecuteNonQuery()
            oConn.Close()
        Catch ex As Exception
            'handle error
            Throw (ex)

        End Try

Open in new window

Poamtsp.sql
0
Comment
Question by:imstac73
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
Comment Utility
rdpenddate.SelectedDate.Value.ToShortDateString returns the date formatted with /.  Try using rdpenddate.SelectedDate.Value.ToString("yyyyMMdd") instead.
0
 
LVL 20

Assisted Solution

by:BuggyCoder
BuggyCoder earned 250 total points
Comment Utility
Change your code as:-
sql.Parameters("@enddate").Value = rdpenddate.SelectedDate.Value.ToString("yymmdd");

also make a change to your procedure ensuring that any datetime comparisons are done by converting the date passed to sql datetime object like this:-

convert(datetime,@enddate,112)
0
 

Author Comment

by:imstac73
Comment Utility
Thank you guys..this worked.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

763 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

10 Experts available now in Live!

Get 1:1 Help Now