Click here to Skip to main content
15,996,726 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hello ,

I want to particular date data i write some code and also iam using sql server 2008
the column type rdate=datetime some i am given the date in one textbox the textbox
date data can be display the gridview but here is the some error so please help me
i am given the source code

C#
string dateString = TextBox6.Text; // <-- Valid
            string format = "ddd dd MMM h:mm tt yyyy";
            DateTime dateTime;
            TextBox6.Text = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
            SqlConnection con = new SqlConnection("Data Source=NSYS1\\SQLEXPRESS;Initial Catalog=agfl;connect timeout=30;Integrated Security=True");
            con.Open();
            string strQuery = "select * from stf where  rdate =@some";
            SqlCommand cmd = new SqlCommand(strQuery, con);
            cmd.CommandType = CommandType.Text;
            TextBox6.Text = TextBox6.Text.Trim();
            cmd.Parameters.AddWithValue("@some", TextBox6.Text);
            SqlDataAdapter sda = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            gvuser.DataSource = dt;
            gvuser.DataBind();
            con.Close();
Posted
Updated 27-Oct-13 19:19pm
v2
Comments
[no name] 28-Oct-13 1:23am    
please put your error string here
Raghavendra Guptha 28-Oct-13 1:27am    
actually i am entered date in text box like that "date month year" this my format

1 solution

Look at that line:
C#
TextBox6.Text = DateTime.TryParseExact(dateString, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);

TryParse / TryParseExact return a Boolean: the boolean return value indicates whether the parsing succeeded or failed.
You ought to use it like:
C#
if (!DateTime.TryParseExact(TextBox6.Text, format, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
{
    //show a message to the user OR 
    //use a default value OR 
    //do what ever you think is useful when the data in TextBox6 are not a date
};

Also, use the dateTime variable farther down in your code instead of TextBox6.Text!
 
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