Solved

Error converting varchar to number

Posted on 2012-04-11
3
371 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
[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
3 Comments
 
LVL 23

Accepted Solution

by:
wdosanjos earned 250 total points
ID: 37834794
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
ID: 37834878
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
ID: 37835041
Thank you guys..this worked.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ASP.NET - Why is accordion not working? 3 68
Connect Gridview column to Textbox in C# 2 54
SQL query 7 49
Adding Rows to Grid 4 29
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

737 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