|
The only time you're displaying the message is if the reportType is not set to either "Today" , "Yesterday" or "All" . If it's one of those values, but there are no matching records, you don't display the message.
As an aside, it looks like you're storing the date in a varchar column. That's an extremely bad idea. You should use one of the date/time types[^] provided by SQL.
Using con As New SqlConnection("...")
Using cmd As New SqlCommand("", con)
Dim reportType As String = ddlrpttype.SelectedValue
If reportType = "Today" Then
cmd.CommandText = "SELECT * FROM mytble WHERE work_request_date >= @MinDate And work_request_date < @MaxDate"
cmd.Parameters.AddWithValue("@MinDate", DateTime.Today)
cmd.Parameters.AddWithValue("@MaxDate", DateTime.Today.AddDays(1))
ElseIf reportType = "Yesterday" Then
cmd.CommandText = "SELECT * FROM mytble WHERE work_request_date >= @MinDate And work_request_date < @MaxDate"
cmd.Parameters.AddWithValue("@MinDate", DateTime.Today.AddDays(-1))
cmd.Parameters.AddWithValue("@MaxDate", DateTime.Today)
Else
cmd.CommandText = "SELECT * FROM mytble"
End If
Using dr As SqlDataReader = cmd.ExecuteReader()
reportgrv.DataSource = dr
reportgrv.DataBind()
End Using
End Using
End Using
If reportgrv.Rows.Count = 0 Then
lblmsg.Text = "No matching records found."
Else
lblmsg.Text = Nothing
End If
As an alternative to manually updating the label, you could use either the EmptyDataTemplate[^] or EmptyDataText[^] properties on the GridView to display a message when there are no rows to display.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
 Hi Richard,
As usual, thanks so much for your constructive help.
I was able to get it to for me, slightly different from my original idea but we are fine with the results from the code below:
Private Sub BindGrid()
Dim s As String = ""
Dim reportType = ddlrpttype.SelectedValue
conn.Open()
If reportType = "Today" Then
s = "SELECT * from mytable WHERE (DATEDIFF(day,CONVERT(datetime,mytable.date, 121),GETDATE()) = 0) ORDER By date DESC "
ElseIf reportType = "Yesterday" Then
s = "SELECT * FROM mytable WHERE (DateDiff(Day, Convert(DateTime, mytable.date, 121), GETDATE()) = 1) ORDER By date DESC "
ElseIf reportType = "All" Then
s = "select * from mytable ORDER By mytable_no DESC "
Else
s = "select * from mytable ORDER By mytable_no DESC "
End If
Dim cmd As New SqlCommand(s)
reportgrv.DataSource = GetData(cmd)
reportgrv.DataBind()
'Showing Numbers in Label
Dim iTotalRecords As Integer = DirectCast(reportgrv.DataSource, DataTable).Rows.Count
Dim iEndRecord As Integer = reportgrv.PageSize * (reportgrv.PageIndex + 1)
Dim iStartsRecods As Integer = iEndRecord - reportgrv.PageSize
If iEndRecord > iTotalRecords Then
iEndRecord = iTotalRecords
End If
If iStartsRecods = 0 Then
iStartsRecods = 1
End If
If iEndRecord = 0 Then
iEndRecord = iTotalRecords
End If
lblTotalRecords.Text = "Total records Found: " & iTotalRecords.ToString() & ""
End Sub
And yes, you are absolutely correct, the date data type is of varchar. I don't design databases like this. I inherited this.
On a separate note, please forgive me for saying this.
This guy that calls himself RyanDev, I just would hope he can learn to avoid my threads.
I have followed some of his post and a lot of them laden with insults.
My parents thought us that if you don't have anything good to say, don't say anything at all.
He must have been raised from a family environment where insults reign. He certainly is a good student because he copied very well.
|
|
|
|
|
samflex wrote: and a lot of them laden with insults. Where did I insult you?
There are two kinds of people in the world: those who can extrapolate from incomplete data.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Hi,
I have a second problem, which is very hard to describe for me.
The project i have to work on was a .net2.0 project. I added MVC and so I had to re-build the whole project with .net4.
I don't know if *that* is the reason for this strange behaviour, but:
On my local development computer everything works fine.
On the productive server (same code), I get
Quote: ErrorMessage: The object of type "System.Web.UI.WebControls.Label" can not be converted to type "System.Web.UI.WebControls.TextBox".
when a datagrid binds its data from a stored procedure.
It does not make sense to me, but I can't describe the problem better.
If anyone of you have an idea, where I could better or more information, which could lead to a solution.
All the best,
Markus
|
|
|
|
|
You need to track down the line of code failing and look at the stack trace. If it works in dev that tells me you have some data coming back from the db that is different on the prod server that is causing the problem.
There are two kinds of people in the world: those who can extrapolate from incomplete data.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Hi RyanDev,
also thanks for replying to this topic.
I'm working on my local development machine, but I'm connected with the productive database.
Yes, - I'm also thinking, that it looks like, as if the problem is caused by an not compatible result of the stored procedure.
But as it is the some database, with the same stored procedure ... I'm asking myself if the different version could be the problem.
All the best,
Markus
|
|
|
|
|
Somewhere you are casting a Label to a TextBox;
TextBox t = (TextBox)e.Item.FindControl("XYZ");
so in the above if the control "XYZ" is a Label you'll get this error, look for this kind of code in the bind events of the gridview.
|
|
|
|
|
My response is late, but I didn't noticed your reply. Sorry! But I want to thank you for replying to my question, F-ES Sitecore. It is always nice to know, that there are nice people out there, taken the time to help other people.
Thank you.
Best,
Markus
|
|
|
|
|
Hi,
I'm new to asp.net programming and so may ask simple things - I hope you will help me anyway.
The project (a Webseite with a mix of WebForms and MVC) I have to work on shows a strange behaviour to me:
Everytime I build the project, VisualStudio creates a <projectname>.config file in the bin folder.
The config file looks very simmiler to theh web.config.
I would like to prevent this behaviour. I googled and found a solution, which does not work for me:
"Do not copy"
and
"No build"
in the "extended preferences" (? My VisualStudio is not in English) of web.config.
Does anyone of you have an idea, what I can do to change this behaviour?
All the best,
Markus
|
|
|
|
|
Markus Evacoso wrote: I would like to prevent this behaviour. Why? Just leave it alone. It should be your apps config file.
There are two kinds of people in the world: those who can extrapolate from incomplete data.
There are only 10 types of people in the world, those who understand binary and those who don't.
|
|
|
|
|
Thank you for your reply, RyanDev.
Unfortunately I have to delete this config file everytime, as otherwise I get the error message a config value is already defined. So, if possible I would like to disable this "feature".
Why is the project building an app config file, when I'm programming a website?
Or does the MVC concept makes the project to an application in the .net world?
All the best,
Markus
|
|
|
|
|
This is very interesting, I just noticed one of my MVC project has the same behavior. The project was created using VS 2012 Pro version. I remember upgrading the solution to VS2015.
During the build, it create a <project name="">.dll.config in the bin folder.
I suspect it has something to do with the VS patch/version because I don't see this behavior on another machine using VS 2015 Enterprise version with latest patch/update.
Anyway, here the post describing the same issue. It also recommend a work around by placing the following entry in the Microsoft.WebApplication.targets file to delete the .config from the bin after the build. Place it below the AppConfig PropertyGroup.
<Target Name="AfterBuild">
<Delete ContinueOnError="true" Files="$(TargetPath).config" />
</Target>
vb.net - Web.config is being renamed by msbuild - Stack Overflow[^]
|
|
|
|
|
see here we store offset. so tell me what is the importance of storing offset data along with date and time ?
what we can do with offset data if we store ?
public void DemoDateTimeOffset()
{
DateTimeOffset offsetValue = new DateTime(2017, 1, 21, 4, 34, 0);
using (SqlConnection cn = new SqlConnection(myConnection))
{
string commandText = @"
INSERT INTO DateTimeDemo (DateTimeExample,DateTimeOffsetExample)
VALUES (@DateTimeExample,@DateTimeOffsetExample)";
using (SqlCommand cmd = new SqlCommand(commandText, cn))
{
cmd.Parameters.AddWithValue("@DateTimeExample", DateTime.Now);
cmd.Parameters.AddWithValue("@DateTimeOffsetExample", offsetValue);
cn.Open();
cmd.ExecuteNonQuery();
}
}
}
[screen shot]
thanks
tbhattacharjee
|
|
|
|
|
The offset is the difference between UTC time and local time. To know why it is stored and for what purpose ask the creator of the database.
DateTime stamps should be always stored in UTC. When displaying such values retrieved from a database, they may be converted to local time using the user/system settings.
The only reasons to store the offset might be to show the stamp using the local time of the system that has written the data (not very meaningful) or knowing the time zone where the creator resides.
In your example code the value it is not an offset but a fixed stamp. The real offset would be the difference between the values. So I would name that value LocalTime instead of DateTimeOffset .
|
|
|
|
|
thanks for reply.
1) would you post some code to grab the offset between local date and time and UTC date and time.
2) would show how to convert utc date and time to local one if we know the offset ?
please share the knowledge if possible. thanks
tbhattacharjee
|
|
|
|
|
It depends on the used language and the used DateTime format.
If you have a format that uses the elapsed seconds or milliseconds since some epoch, just do addition / subtraction.
If you have a DateTime class, use the provided class functions to add/subtract time spans and get the difference between two objects. These classes have usually also functions to convert between UTC and local time using the user or system settings for the local time zone.
For JavaScript see for example Date - JavaScript | MDN[^].
For .NET see DateTime Structure (System)[^].
There is usually no need to get the offset. Use conversion functions instead.
The only situation to use an offset might be when it is configured by a user setting on the server (e.g. the user has specified his time zone). Then use the language dependant addition method with a time span set to the offset.
With .NET DateTime , use for example DateTime.ToUniversalTime Method (System)[^] and DateTime.ToLocalTime Method (System)[^]. But note that the object must be in the source format (when using ToLocalTime for example, the object must contain a UTC time).
|
|
|
|
|
i guess if i store date time in utc format and if i store also offset then i can convert utc date time to local one just adding or subtract offset hours from UTC date and time. am i right ?
tbhattacharjee
|
|
|
|
|
Yes, you are right.
But storing is only necessary if you want to track this kind of information (the local time of the system that has created the time stamp). This is often not necessary because you usually show local times using the time zone of the system displaying the time.
If for example a time stamp is created by a user in India and another user somwhere in the U.S. is viewing the related data, it makes more sense to show it in the local time of the viewer or even UTC rather than using the Indian time.
|
|
|
|
|
suppose i have web UI from where user post issue. our web UI and db hosted in USA pc. now user can appear from any country and post issue.
from my web UI i am sending user issue details and user pc date and time to server side function.
now my question is how could i convert client pc date and time to utc date and time at server side before storing in db ?
again how could i show client date and time in his local date and time if i know client time zone.
so here i have two question how to convert client date and time. for example say i capture client date and time by javascript 25/01/2017 03:08:01 and send it to server side function. now how could i convert client date and time 25/01/2017 03:08:01 to UTC date and time ?
again how could i later convert utc date and time to client local time if i know client timezone.
please help with sample code if possible.
thanks
tbhattacharjee
|
|
|
|
|
Important: Always use UTC and binary types (no local time and no strings)!
When data must be send as strings use a universal format like ISO 8601 - Wikipedia[^].
On the client side get the time as UTC (e.g. using JavaScript Date().getTime() ) and send that to the server.
Handle and store them on the server side as UTC.
To display them they may be optionally converted to the client's local time.
When this is done on the client side, use appropriate functions (just search for "<language> utc to local").
When this is done on the server side the time zone used by the client must known. This can be for example stored in the user record of the login database like here at CodeProject (see your profile settings).
To convert times when having an offset (timezone offsets are usually defined in minutes) just use the Add method of the DateTime class of the used language.
|
|
|
|
|
i guess this code Date().getTime() will show me time only but i need client local date and time. so tell me which one i should use ?
1) if possible show me what JavaScript code i need to use to grab the client local date and time.
2) how to convert that client local date and time to UTC at server side by c# code ?
please share code example for my question if possible. thanks
tbhattacharjee
|
|
|
|
|
Again:
Don't get the local time on the client. Get the UTC time. Do all operations with UTC. Only when displaying local time is required convert to local time for displaying.
When the time stamp is going to be stored in a database on the server there is even no need to get it on the client. Get it on the server instead. This will also avoid wrong times (the clients clock may be wrong) and time deviations between clients (clocks of clients may be not exact).
See my answer to the above thread for references.
|
|
|
|
|
are you trying to say grab client utc date time instead of local date time ?
how to grab client utc date time by javascript ?
for my second question please include a small js code. thanks
tbhattacharjee
|
|
|
|
|
It depends on what you want to do with the time stamp.
If you want to send it to server:
That is not necessary as already explained. Let the server get the time of receiving the client's request.
If you want to process it on the client use the format (UTC or local) according to your requirements.
To get the current time just use Date.now() - JavaScript | MDN[^].
|
|
|
|
|
i know unit testing but not familiar with integration testing and regression testing.
it will be really helpful if some one explain what is integration testing and regression testing with sample example. thanks
tbhattacharjee
|
|
|
|