C# To SQL - Date Format [Resolved]

Posted by Sharpcnet under C# on 1/8/2014 | Points: 10 | Views : 21127 | Status : [Member] | Replies : 1
Table - tblData (Data for Jan.):
This is how I see the data with select * from tblData
ID  Tag     Date
1 OB 2014-07-01
2 OC 2014-07-01
3 AB 2014-07-01
Trying to acheive the same from C#, but this is not working.
C#:
DateTime dtFrom = 
Convert.ToDateTime(txtFromDate.Text.Trim(),System.Globalization.CultureInfo.GetCultureInfo
("hi-IN").DateTimeFormat);

DateTime dtTo =
Convert.ToDateTime(txtToDate.Text.Trim(),System.Globalization.CultureInfo.GetCultureInfo
("hi-IN").DateTimeFormat);

gridview1.DataSource = objDAL.GetData(dtFrom,dtTo);
gridview1.DataBind();
Stored Procedure(sql server 2008 r2):
Alter Procedure GetData
(
@DateFrom Date,
@DateTo Date
)
As
Begin
Select * from tblData
where date between @DateFrom and @DateTo
End


The only date in table is Jan 7,2014. The SP works fine when I tested with
execute GetData '2014-07-01','2014-07-01' (yyyy-dd-mm).
But from webpage, Even if i enter '08-01-2014' & '08-01-2014' (dd-mm-yyyy),in the textboxes, I get all records..!!! –




Responses

Posted by: kgovindarao523-21772 on: 1/8/2014 [Member] [MVP] Bronze | Points: 50

Up
0
Down

Resolved
Hi,

I dont know how your Data logic is.
I tried like this and get succeed.
using (SqlConnection connection = new SqlConnection(@"DataConnection"))
{
// Open the connection.
connection.Open();

// Create the command.
using (SqlCommand command = new SqlCommand("GetData", connection))
{
// Set the command type.
command.CommandType = System.Data.CommandType.StoredProcedure;

// Add the parameter.
SqlParameter parameter = command.Parameters.Add("@DateFrom",
System.Data.SqlDbType.DateTime);

// Set the value.
parameter.Value = Convert.ToDateTime(txt.Text).ToString("yyyy-MM-dd");
SqlParameter parameter2 = command.Parameters.Add("@DateTo",
System.Data.SqlDbType.DateTime);

// Set the value.
parameter2.Value = Convert.ToDateTime(txt.Text).ToString("yyyy-MM-dd");

// Make the call.
DataTable dt = new DataTable();
//DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter(command);
da.Fill(dt);
}
}

Input in Text boxes:
08-01-2014

Thank you,
Govind

Sharpcnet, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response