?
Solved

Error converting varchar to number

Posted on 2012-04-11
3
Medium Priority
?
375 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 1000 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 1000 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
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.
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.

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