I have 3 drop down list having Date,Month ,Year.So when Updating I want this 3 Field to be a single Datafield in Sql Database. Iam using Asp.Net 2.0 Version(VB.Net).(Now these 3 Dropdown list values are saved as 3 Datafields in sql Database)
Why not use a date/time picker instead? It seems to me to be a better idea than your own triplet of day, month and year.
I take it you're using a SqlCommand for this? Here's some sample, non-compiling code which I'll whip up for you here:
Dim cmd as SqlCommand("update tableX set dt = #var where ID = 1");
cmd.Parameters.AddWithValue("#var", new DateTime(CInt(day.SelectedValue), CInt(month.SelectedValue, CInt(year.SelectedValue)));
cmd.ExecuteNonQuery();
Join up the values:
month + "/" + day + "/" + year
and use DateTime.Parse() or DateTime.TryParse() to convert them to a date. If you are concerned about the date format for your culture, pass in a CultureInfo object. Add your own validation as needed.
http://msdn.microsoft.com/en-us/library/1k1skd40.aspx
Not foolproof, but it might satisfy your requirement.
Related
I have a table containing entries with date and time. I try to create a NamedQuery which only compares the date part.
#NamedQuery(name = "Vote.findForDate", query = "SELECT v FROM Vote v WHERE v.createdAt = :date")
...
createNamedQuery("Vote.findForDate").setParameter("date", date, TemporalType.DATE).getResultList();
But it seems that it always tries to compare the whole datetime.
Is there no way without using date() function in SQL?
I try to be independent from the database. For example h2 has no date() function.
One possible solution is the usage of date as column type and reduce the information.
#Column(name = "voteDate")
#Temporal(value = TemporalType.DATE)
private Date voteDate;
I am currently working on a website that offers tutoring services and I have been stuck on this issue quite a while..
I have 2 text boxes where the user chooses a start date and a finish date, when the user clicks view he would be suppose to see the results in a gridview. I am using FormParameters to insert the date into the query.
SelectCommand of the sqldatasource
SelectCommand="SELECT [Session].Session_num AS Num, [Session].Session_Time_Stamp AS Session_Date, Student.Student_First & ' ' & Student.Student_Last AS Student FROM (([Session] INNER JOIN Student ON [Session].Student_Num = Student.Student_Num) INNER JOIN Class ON [Session].Class_ID = Class.Class_ID) WHERE ((([Session].Session_Time_Stamp) Between #firstdate And #seconddate))">
Parameters of the SqlDataSource
<SelectParameters>
<asp:FormParameter Name="firstdate" Type="DateTime"/>
<asp:FormParameter Name="seconddate" Type="DateTime"/>
</SelectParameters>
This is executed when the user clicks the view button, it is where I set the values of the parameters and execute the sql select.
Dim fdate As DateTime = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")
Dim ldate As DateTime = Format(CDate(txtEndDate.Text), "MM/dd/yyyy")
gridTutor.SelectParameters("firstdate").DefaultValue = fdate
gridTutor.SelectParameters("seconddate").DefaultValue = ldate
gridTutor.Select(DataSourceSelectArguments.Empty)
gridTutorSessions.DataBind()
fdate and ldate are not empty, however after this is executed the query result is empty. Could it be that wrong methods to execute the select?
Edit: I realized the problem is probably with the query and DateTime format. When I transformed my textbox value to DateTime it put it like this #2/20/2014#. However, it doesn't return anything even if there are values between the two dates.
If anybody have an access query with DateTime I would like to see it. Thanks
I managed to fix it by formatting the date in my access database it's not the best solution but it is a fix for the situation
I believe you need to manually set fdate and ldate just before you do your 'selectparameters' (i.e. use "fdate = Format(CDate(txtStartDate.Text), "MM/dd/yyyy")". According to the following, values asigned to Dim in the manner you have would not reflect the realtime values you want. See the following regarding VB: "You can assign a value to a variable when it is created. For a value type, you use an initializer to supply an expression to be assigned to the variable. The expression must evaluate to a constant that can be calculated at compile time.
This gives me an error-
The conversion of a char data type to a datetime data type resulted in
an out-of-range datetime value.
This doesn't work and had tried this too, but this orders by date not month
SqlCommand cmd = new SqlCommand("select * from tbl_Member where Email LIKE '" + id.ToString() + "%' ORDER BY YEAR(RegisterDT) DESC,MONTH (RegisterDT) DESC, DAY(RegisterDT) DESC", con);
SqlCommand cmd = new SqlCommand("select * from tbl_Member where Email LIKE '" + id.ToString() + "%' ORDER BY(RegisterDT) DESC", con)
When posting a question, it is a good idea to give the table format, the database you are using, and sample data. In this case, I speculate that you are using SQL Server, because you are using the DAY() and MONTH() functions.
These functions take dates as arugments, not strings. You should not depend on implicit conversion in SQL. Let me repeat that. You should always explicitly convert between data types, to prevent errors like the ones you see.
The problem that you have is that your date format is not in the default format for the system. For instance, you may have a date like 10/03/2012. Is this Oct 3rd, as Americans would believe? Or is this March 10th, as Europeans would? Then consider 10/20/2012. It is out of range for Europeans.
So, you have two options:
(1) Do an explicit conversion, using the CONVERT() function.
(2) Extract the month and day using string functions. In the above case, this might be substring(, 1, 2) for one and substring(, 4, 2) for the other.
My guess is that RegisterDT is a char column and you have an invalid date in there for your current date settings (DMY | MDY). Dates should be stored as dates, not strings. This should resolve your problem.
As a first side point, if RegisterDT is a date column, why do you have to break it down by year, month day? Why can't you just ORDER BY RegisterDT DESC?
As a final side point BEWARE SQL INJECTION. You should be using paramerised queries like so:
SqlCommand cmd = new SqlCommand("SELECT * FROM tbl_Member WHERE Email LIKE #ID + '%' ORDER BY RegisterDT DESC", con);
cmd.Parameters.Add("#ID", SqlDbType.Varchar).Value = id.ToString();
This not only elimates SQL injection, it also makes code more readable by separating SQL from values, and stops data loss through string conversion.
I'm using DateDiff() function of ASP to find the date difference between two dates.
The Function works fine and displays the exact date difference between two dates but when it comes to insert this value in the database it takes 9 as the value irrespect of any date difference.
Suppose difference between two dates is more than 15 or 20 days, in the database it takes "9".
I have used INT as the DATA TYPE for the column where it displaying the date difference.
Is DATA TYPE creating an issue here?
I even tried using session variable to store the value but no luck - here's my code below:
if request.Form("sub") <> "" then
sql = "Select * from emp_leave_details"
rs.open sql , con, 1, 2
dim diff
dim todate
dim fromdate
fromdate= rs("leave_from")
todate= rs("leave_to")
session("date_diff")=datediff("d",fromdate,todate)
rs.addnew
rs("emp_name") = request.Form("name")
rs("emp_no") = request.Form("number")
rs("address") = request.Form("address")
rs("contact_no") = request.Form("contact")
rs("mobile_no") = request.Form("mobile")
rs("contact_onleave") = request.Form("contact_details")
rs("leave_type") = request.Form("rad")
rs("other_leave_details") = request.Form("PS")
rs("leave_from") = request.Form("from")
rs("leave_to") = request.Form("to")
rs("applied_by") = request.Form("apply")
rs("accepted_by") = request.Form("accept")
rs("approved_by") = request.Form("approve")
rs("no_of_leave_taken")= session("date_diff")
rs.update
response.Write("<script language='javascript'>{update();}</script>")
rs.close
end if
The datatype has nothing to do with this. Storing the value in the session is not the solution. You can use a regular variable.
From your code it looks like you always use the same values for fromdate and todate. This is because you do not iterate the rows in the resultset.
if not rs.bof and not rs.eof then
do while not rs.eof
'' code to execute for each row
rs.moveNext
loop
end if
In your current script rs will always return the results of the first row returned by the query.
The second problem your running into might be the Date datatype. Convert your value to a date using cDate and use this to calculate the difference.
Your problem is , you search for "Select * from emp_leave_details" which always gives all records from that table. You retrieve the values of the first record and do a diff of these, which results in always the same value, that is normal. From your question it is unclear what you really want to do. I suppose so want so select a record like
Select * from emp_leave_details where emp_name=<%=request.Form("name")%>
and based on that add a new record with a computed no_of_leave_taken.
Sorry guys my bad...
It was the database field name that I was calling instead of
fromdate= request.form("from")
todate= request.form("to")
I was calling this
fromdate= request.form("leave_from")
todate= request.form("leave_to")
Sorry again..but I really appreciate you all for providing me with all that possible solutions.
Thanks.
Duplicate: How to truncate a date in
.net?
I have datetime field containing '4/1/2009 8:00:00AM'. I want to get '4/1/2009' without the time.
Use the Date property of the datetime field (if you need to do this on the client)
DateTime.Date will give you just the date portion of the datetime if you want to pass it around your application
If you are inside of .NET as it appears that you are based on the tags
dim myDate as DateTime = DateTime.Parse('4/1/2009 8:00:00AM')
dim myDesiredValue as String = myDate.ToShortDateString()
This is C# (yeah - I know you want VB) but given that none of the following uses anything other than DataTime then it should give you want you want...
string foo = "4/1/2009 8:00:00AM";
DateTime bar = DateTime.Parse(foo);
string output = bar.ToString("M/d/yyyy");
Depends on your database server, but in sql server I normally use this in my sql query:
CAST(FLOOR(CAST([MyDateTimeColumn] AS float)) AS datetime)
CONVERT(varchar,mydate,101)
CONVERT(DATE, dateFieldName)