Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hye, i'm having a problem when i'm inserting data in textbox.the problem is when i'm searching for data 10/10/2010, it work perfectly but when i try to search other date (eg 25/11/2013), i would get error The conversion of a varchar data type to a datetime data type resulted in an out-of-range value. if i change the date to 11/22/2013, i will get error, String was not recognized as a valid DateTime. is there something wrong with my code? i will put where the error occur below.

protected void searchdata_Click(object sender, EventArgs e)
{
    Label1.Text = locID.ToString();
    adddate();
    byday2();

}

public void adddate()
{
    today = DateTime.Parse(txtStartDate.Text);//String was not recognized as a valid DateTime
    DateTime answer = today.AddDays(1);
    adddate2 = answer;
}
public void byday2()
{
    Label9.Text = adddate2.ToShortDateString();
    Label10.Text = txtStartDate.Text;
    if (Byday.Checked == true)
    {
        if (Maxdata.Checked == true)
        {
            //tablemax.hidden = true;
            lblmaxdata.Visible = true;
            lblmaxdata.Text = "Highest Data";
            
            string connectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\Users\\shafiq\\Desktop\\cuba\\App_Data\\radiation.mdf;Integrated Security=True;User Instance=True";

            // Create SQLDataSource.
            SqlDataSource sqlDataSource = new SqlDataSource();
            sqlDataSource.ID = "SqlDataSource123";
            this.Page.Controls.Add(sqlDataSource);

            // Bind ConnectionString to SQLDataSource.
            sqlDataSource.ConnectionString = connectionString;
            sqlDataSource.SelectCommand = "SELECT top 3 [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= '" + today + "') AND ([date] < '" + adddate2 + "')) ORDER BY [data] DESC, [date] DESC";

            // Bind SQLDataSource to GridView after retrieving the records.
            gridmaxdata.DataSource = sqlDataSource;
            gridmaxdata.DataBind(); //The conversion of a varchar data type to a datetime data type resulted in an out-of-range value.
        }
    }
}
Posted
Updated 27-Nov-13 17:48pm
v2

the problem was that i only call for the datetime data wrongly. here is the correct code.
SqlDataSource.SelectCommand = "SELECT top 3 [date], [data] FROM [loc1] WHERE (([data] >= '2') AND ([date] >= '" + today.ToLongDateString() + "') AND ([date] < '" + adddate2.ToLongDateString() + "')) ORDER BY [data] DESC, [date] DESC";
 
Share this answer
 
Add this key in your config file -

HTML
<globalization enableclientbasedculture="true" culture="en-GB" uiculture="en" />


Also while passing date in your query use the date variable as like for eg:-

where fromdate between ' + Convert.ToDateTime(addDate2).ToString("yyyy-MM-dd") + "'"

Hope it helps
 
Share this answer
 
Comments
Jejaka Siber 28-Nov-13 1:15am    
i have try the code and it still produce the same error and the globalization code is not supported by html5
Madhu Nair 28-Nov-13 1:24am    
Are you using ASP.Net? If yes, then add the key in the web.config file. It has nothing to do with HTML 5
Jejaka Siber 28-Nov-13 1:30am    
where exactly in the web.config file should I insert it?? sorry, i'm still new at this...
Madhu Nair 28-Nov-13 1:33am    
It should be put under <system.web> node
Jejaka Siber 28-Nov-13 2:52am    
I have put it under system.web but enableclientbasedculture and uiculture attribute is not allowed.

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