Click here to Skip to main content
15,907,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using calendar.for that i design that calendar in global function(calendaruc.ascx)


Design page as follows;

TExtbox calendar(in image button)

source code as follows in the calendar.ascx.cs page as follows;

C#
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class CalendarUC : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    public string SelectedDateValue
    {
        get
        {
            return txtDate.Text;
        }
        set
        {
            txtDate.Text = value;
        }
    }
}

Then i design a aspx page.

for that i am using that calendar fuction here.
for this source code as follows;

C#
<Uc1:CalendarUC ID="FromDate"  runat="server" />



    <Uc1:CalendarUC ID="Todate"  runat="server" />.

in that insert code as follows;

 try
        {
            con.Open();
            Sql = "insert into BirthDayWish values('" + txt_name.Text + "','" + FromDate.SelectedDateValue.ToString() + "','" + txt_mobile.Text + "','" + Todate.SelectedDateValue.ToString() + "','a','" + txt_Email.Text + "')";
            SqlCommand cmd = new SqlCommand(Sql, con);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception Ex1)
        {
            Response.Write(Ex1);
        }


when i running the aspx page in that when i click that calendar and select the date that date is displayed in the textbox and save the record.

but in the database date not save.it save as this format 1/1/1900 12:00:00 AM.

why which date is selected that date is not selected why.

from my above code and design page wat is the problem.please help me.
Posted
Updated 24-Dec-12 1:17am
v2
Comments
[no name] 24-Dec-12 7:20am    
Check your database for the date field, whether the datatype is date and time or not.
CHill60 24-Dec-12 7:24am    
As I said earlier - debug this
a) Confirm that FromDate.SelectedDateValue actually has something in it
b) Make sure you are explicit in the date formatting so use ToString("dd-MMM-yyyy")
If that still doesn't work then by all means reply to this comment but please don't keep posting the same question

Don't do it that way.
Firstly, do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Secondly, doing it by concatenating strings takes the date from a generic data value to a culture sp[ecific value (based on wheatever culture your server pc was set to, almost certainly not what you wanted) and then expects Sql (probably on a different server complelty) to reverse the convertion and turn it back into a DateTime value again. But SQL assumes yyyy-MM-dd format which your web server is almost certainly not set to use.

Leave it as a DateTime, and pass it through as such via a parametrised query. It will probably fix your problem and make your code safer, more maintainable, and more readable as well!
C#
con.Open();
Sql = "insert into BirthDayWish values(@NM, @FD, @MO, @TD, 'a', @EM)";
SqlCommand cmd = new SqlCommand(Sql, con);
cmd.Parameters.AddWithValue("@NM", txt_name.Text);
cmd.Parameters.AddWithValue("@FD", FromDate.SelectedDateValue);
cmd.Parameters.AddWithValue("@MO", txt_mobile.Text);
cmd.Parameters.AddWithValue("@TD", Todate.SelectedDateValue);
cmd.Parameters.AddWithValue("@EM", txt_Email.Text);
cmd.ExecuteNonQuery();


BTW: It is a bad idea to leave out the names of the columns you are inserting - if they are not in exactly the order you expect, then either the date will be inserted in the wrong columns, or you will get an error. Always list the colum,ns - it makes your code more robust and better capable to cope with future changes.
 
Share this answer
 
v2
Hi,
First of all your code in unsafed!! It's open for sql injection to SQL command!!

It would be much better to use SqlCommandParameter, so as a result:

C#
 try
        {
            con.Open();
            Sql = "insert into BirthDayWish values(@p1,@p2,@p3,@p4,#p5)";
            SqlCommand cmd = new SqlCommand(Sql, con);
            cmd.Parameters.Add("@p1", SqlDbType.VarChar);
            cmd.Parameters["@p1"].Value = customerID;
            DateTime dt;
            if(!DateTime.TryParse(romDate.SelectedDateValue.ToString(),out dt))
            {// error in date format!!!}  
            cmd.Parameters.Add("@p2", SqlDbType.DateTime);
            cmd.Parameters["@p2"].Value = dt;           
/* another stuff goes here!!!*/

            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception Ex1)
        {
            Response.Write(Ex1);
        }
 
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