SQL Server 2008 datetime will not insert via c# but works within SQL Server Management Studio? -


i using sql server stored procedure put data database via c#. when run stored procedure within sql server management studio works when try insert row c# fails , cannot find out why. had trouble getting datetime type in stored procedure works when use cast. have tried passing datetime values in strings c# not work either!?

my sp below:

alter procedure [dbo].[saveflightinfo] (         @flightinfoid int,         @airportfrom varchar(5),         @airportto varchar(5),         @timedeparture datetime,         @timearrival datetime,         @price float,         @dateadded datetime,         @carrier varchar(30),         @url varchar(300)  )   declare @sql varchar(1000)   --if primary key 0 insert new record if ( @flightinfoid = 0) begin declare @sql3 varchar(1000) set @sql =' insert '+@airportfrom+' (airportfrom, airportto, timedeparture, timearrival, price, dateadded, carrier, url)   values ('''+@airportfrom+''', '''+@airportto+''', cast('''+cast(@timedeparture varchar(50))+''' datetime), cast('''+cast(@timearrival varchar(50))+''' datetime), cast('''+cast(@price varchar(10))+''' float), cast('''+cast(@dateadded varchar(50))+''' datetime), '''+@carrier+''', '''+@url+''')  '     end     exec(@sql) 

my c# code below:

   database db = databasefactory.createdatabase("dbconnectionstring");         dbcommand dbcommand = db.getstoredproccommand("saveflightinfo");          db.addinparameter(dbcommand, "flightinfoid", dbtype.int32, flightinfoid);         db.addinparameter(dbcommand, "airportfrom", dbtype.string, airportfrom);         db.addinparameter(dbcommand, "airportto", dbtype.string, airportto);         db.addinparameter(dbcommand, "timedeparture", dbtype.datetime, timedeparture);         db.addinparameter(dbcommand, "timearrival", dbtype.datetime, timearrival);         db.addinparameter(dbcommand, "price", dbtype.int16, price);         db.addinparameter(dbcommand, "dateadded", dbtype.datetime, dateadded);         db.addinparameter(dbcommand, "carrier", dbtype.string, carrier);         db.addinparameter(dbcommand, "url", dbtype.string, url);          idatareader dr = db.executereader(dbcommand); 

also can use sp_executesql system stored procedure parameters

declare @sql varchar(1000) --if primary key 0 insert new record if (@flightinfoid = 0) begin   declare @sql3 varchar(1000)   set @sql = 'insert ' + @airportfrom +      '(airportfrom, airportto, timedeparture, timearrival, price, dateadded, carrier, url)      values @airportfrom, @airportto, @timedeparture, @timearrival, @price, @dateadded, @carrier, @url)' end exec sp_executesql @sql, n'@airportfrom varchar(5),                             @airportto varchar(5),                             @timedeparture datetime,                            @timearrival datetime,                             @price float,                             @dateadded datetime,                             @carrier varchar(30),                             @url varchar(300)', @airportfrom,                                                @airportto,                                                @timedeparture,                                                @timearrival,                                                @price,                                                @dateadded,                                                @carrier,                                                @url 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -