user2660112
user2660112

Reputation:

String to be converted to date format

.aspx:

<asp:TextBox ID="txtend" runat="server"></asp:TextBox></td>

This aspx code will store "date" in its text field.

I want to access this textfield in c# where in the code is shown as How to convert the string format into date type and get accessed..?

SqlDataAdapter adapter = new SqlDataAdapter(
    "select * from Membership_det where updateDate is between "+txtstart.Text+" and "+txtend.Text+" ", 
    con);

Upvotes: 1

Views: 1037

Answers (6)

Shreyas Achar
Shreyas Achar

Reputation: 1435

    DateTime startDate;
    DateTime endDate;

    if (DateTime.TryParse(txtstart.Text, out startDate) && DateTime.TryParse(txtend.Text, out endDate))
    {
        //string n1 = DropDownList2.SelectedItem.Text;

        if (DropDownList1.SelectedItem.Text == "Membership")
        {
            SqlConnection con = new
      SqlConnection(ConfigurationManager.ConnectionStrings["ProjectConnectionString"].ToString());
            con.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("select p.Name,m.FID,m.MembNo,m.MembType,m.Validity,m.Remarks from Membership_det m INNER JOIN Personal_det p  ON m.FID= p.FID where m.updateDate  between @Start and @End ", con);
            adapter.SelectCommand.Parameters.Add("@Start", SqlDbType.Date).Value = startDate;
            adapter.SelectCommand.Parameters.Add("@End", SqlDbType.Date).Value = endDate;
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            con.Close();
            GridView1.DataSource = dt;
            GridView1.DataBind();
        }
        log.Debug("Info: Admin viewed the membership details");
    }

Upvotes: 1

senthilkumar2185
senthilkumar2185

Reputation: 2566

SqlDataAdapter adapter = new SqlDataAdapter(
    "select * from Membership_det where updateDate is between '"+ Convert.ToDateTime( txtstart.Text).ToString("dd/MMM/yyyy")+"' and '"+ Convert.ToDateTime( txtend.Text).Tostring("dd/MMM/yyyy")+"' ", 
    con);

Upvotes: 0

Rajamohan Anguchamy
Rajamohan Anguchamy

Reputation: 1736

Your Textbox datetime is like "25-09-2013" means put the format type "dd-MM-yyyy" like that, which type you get that the string format.

E.g:1.

var da="2013-09-25";
DateTime.ParseExact(da, "yyyy-MM-dd", 
System.Globalization.CultureInfo.CurrentCulture);

E.g:2.

var da="25-09-2013";      
DateTime.ParseExact(da, "dd-MM-yyyy",
System.Globalization.CultureInfo.CurrentCulture);

Upvotes: 0

Rahul Tripathi
Rahul Tripathi

Reputation: 172378

You can use the DateTime.ParseExact to convert it. Something like this:-

DateTime.ParseExact(txtstart.Text, "yyyyMMdd", null);

On a side note:-

As pointed by Uwe Keim in comments your code is prone to SQL injection.

It would be good if you Use Parameterized SQL-queries.

Upvotes: 1

Thilina H
Thilina H

Reputation: 5804

use this

string date = "01/08/2008";
    DateTime dt = Convert.ToDateTime(date); 

||

string date = "Sun, 17 Mar 2013 12:40:23 +0000";
var dt = DateTime.ParseExact(date, "ddd, dd MMM yyyy hh:mm:ss zzz", CultureInfo.InvariantCulture);

Upvotes: 0

Bibhu
Bibhu

Reputation: 4081

Use this :

DateTime startDate = DateTime.ParseExact(txtstart.Text, "yyyyMMdd", null);
DateTime endDate = DateTime.ParseExact(txtend.Text, "yyyyMMdd", null);

SqlDataAdapter adapter = new SqlDataAdapter(
    "select * from Membership_det where updateDate is between "+ startDate.ToString() + " and "+ endDate.ToString() +" ", 
    con);

Upvotes: 0

Related Questions