1

.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);
2
  • 2
    Please be aware that your code is prone to SQL injection.
    – Uwe Keim
    Sep 7, 2013 at 6:39
  • use storeprocedure instead of this query
    – Vikas Rana
    Sep 7, 2013 at 6:46

6 Answers 6

1
    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");
    }
1

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.

0

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);
3
  • @Rahul Tripathi - Sorry, but i think others also know c#. :)
    – Bibhu
    Sep 7, 2013 at 6:46
  • In which format i should input the date.. i mean dd/mm/yyyy or mm/dd/yyyy
    – user2660112
    Sep 7, 2013 at 8:49
  • it gives error like..String was not recognized as a valid DateTime...When date is given in the format mm/dd/yyyy
    – user2660112
    Sep 7, 2013 at 9:01
0

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);
0

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);
0
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.