Please start any new threads on our new site at http://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

Our new SQL Server Forums are live! Come on over! We've restricted the ability to create new threads on these forums.

SQL Server Forums
Profile | Active Topics | Members | Search | Forum FAQ
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Failed to convert parameter value from a String to
 Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

sajitha
Starting Member

Australia
10 Posts

Posted - 03/28/2012 :  00:40:40  Show Profile  Reply with Quote
Hi
I am getting error while inserting details ie,
"Failed to convert parameter value from a String to DateTime". I am facing the same problem, wherever I use ajax Calender Control.


This is my stored Procedure

USE [PMS]
GO
/****** Object: StoredProcedure [dbo].[WorkInsertion] Script Date: 03/28/2012 15:38:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[WorkInsertion]
(@WorkDone nvarchar,
@StartDate datetime,
@StartTime time(7),
@StopDate datetime,
@StopTime time(7),
@Username nvarchar)

AS
INSERT INTO Work(WorkDone, StartDate, StartTime, StopDate, StopTime, Username) VALUES(@WorkDone, @StartDate, @StartTime, @StopDate, @StopTime, @Username);

This is my .CS file

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection conn;
SqlCommand comm;
string connectionString = ConfigurationManager.ConnectionStrings["PMSConnectionstrings"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("WorkInsertion", conn);
comm.CommandType = System.Data.CommandType.StoredProcedure;
comm.Parameters.Add("@WorkDone", System.Data.SqlDbType.NVarChar, 500);
comm.Parameters["@WorkDone"].Value = workTextBox.Text;
comm.Parameters.Add("@StartDate", System.Data.SqlDbType.DateTime);
comm.Parameters["@StartDate"].Value = startdateTextBox.Text;
comm.Parameters.Add("@StartTime", System.Data.SqlDbType.Time);
comm.Parameters["@StartTime"].Value = starttimeTextBox.Text;
comm.Parameters.Add("@StopDate", System.Data.SqlDbType.DateTime);
comm.Parameters["@StopDate"].Value = stopdateTextBox.Text;
comm.Parameters.Add("@StopTime", System.Data.SqlDbType.Time);
comm.Parameters["@StopTime"].Value = stoptimeTextBox.Text;
comm.Parameters.Add("@Username", System.Data.SqlDbType.NVarChar);
comm.Parameters["@Username"].Value = "Administrator";

try
{
conn.Open();
comm.ExecuteNonQuery();
errorLabel.Text = "Inserted Work details Successfully.......";


}

catch
{
errorLabel.Text = "Error inserting work details! Please " +
"try again later, and/or change the entered data!";


}

finally
{
conn.Close();
}

}

Any help appreciated

dinakar
Flowing Fount of Yak Knowledge

USA
2507 Posts

Posted - 03/28/2012 :  00:51:40  Show Profile  Visit dinakar's Homepage  Reply with Quote
I am not a .NET developer but I think you need to convert the date type values to Date format explicitly before you pass them to comm.Parameters collection.

Dinakar Nethi
************************
Life is short. Enjoy it.
************************
http://weblogs.sqlteam.com/dinakar/
Go to Top of Page

rajarajan
Starting Member

India
48 Posts

Posted - 03/28/2012 :  01:06:26  Show Profile  Reply with Quote
u can use Convert.datetime(String Variable) in C# before sending to the Stored Procedure
i can see u have used time ,try with datetime
Go to Top of Page

charlesbecon
Starting Member

3 Posts

Posted - 12/14/2016 :  01:26:44  Show Profile  Reply with Quote
You can use TryParse

public static bool TryParse(
string s,
out DateTime result
)

This method is similar to the DateTime.Parse(String) method, except that the TryParse(String, DateTime) method does not throw an exception if the conversion fails. More...string to date

http://net-informations.com/q/faq/stringdate.html

Charles


Go to Top of Page
  Previous Topic Topic Next Topic  
 Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.02 seconds. Powered By: Snitz Forums 2000