Click here to Skip to main content
15,917,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear All,

I want to insert date using textbox and calendar extendar

Error :- The type datetime can't insert to table but other types inserted normally


My Example :

I have the following table columns :

userid (int & identity) | name (nvarchar(50)) | expdate (datetime)
-----------------------------------------------------------------

Stored Procedure code

SQL
ALTER PROCEDURE dbo.Manageusers
(

@name nvarchar(50),
@expdate  datetime

)

AS

   

INSERT INTO users (name,expdate) Values (@name,@expdate)

    END


    RETURN




- C# Class :

C#
public class insertusers:opsMaintable 
{
 protected string ProcedureName;

    #region Fields

    private string _name;
     private  DateTime _expdate;
    

    #endregion

    #region Properties


    public string name
    {
        get
        {
            return _name;
        }
        set
        {
            _name = value;
        }
    }
     public DateTime expdate
    {
        get
        {
            return _expdate;
        }
        set
        {
            _expdate = value;
        }
    }

    #endregion

    //Insert & Delete & Update Method
    protected override bool LoadProperties2Parameters(string TypeofOperation)
    {
        SortedList SL = new SortedList();
       
        SL.Add("@name", name);
SL.Add("@expdate", expdate);
        


        ProcedureName = "Manageusers";
        if (db.RunProcedure(ProcedureName, SL) == 1)
            return true;
        else
            return false;
    }
}



- insert button code behind

C#
 insertusers ingang = new insertusers();
ingang.name = Textuser.Text;
ingang.expdate =Convert.ToDateTime(Textdate.Text);

ingang.Add()
Posted
Comments
[no name] 3-Aug-13 7:42am    
You would need to debug your RunProcedure method and see what it is doing with SL.
shms_rony 3-Aug-13 8:00am    
Run Procedure Worked well and datetime inserted
What's problem?
[no name] 3-Aug-13 8:06am    
And how would you think that I would know that? I can't see your code, your database or your project.
shms_rony 3-Aug-13 8:09am    
ok thanks
Check whether DateTime value, which you are inserting is in correct format or not?

C#
SL.Add("@expdate", expdate.ToString("yyyy-MM-dd HH:mm:ss"));
 
Share this answer
 
v2
Comments
shms_rony 4-Aug-13 5:17am    
Thanks , it worked for me :)
Why are you using a SortedList for this?
Think about it: what order are the objects in if the first item added is "Rony" and the second is "2013-08-03 14:39"? And what order are they likely to be passed to your procedure?
 
Share this answer
 
you can convert datetime type to string and then insert it into database:
try this:

C#
public string ConvertDatetimeToValid(DateTime dateTime)
       {
           string datetime = string.Concat(dateTime.Year.ToString(), "/",      dateTime.Month.ToString(), "/", dateTime.Day.ToString());
           return datetime;
       }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900