C# Save Date on event, tell me how many days it has been - datetime

So I have been working on this for quiet a while, I want to basically have something on my form2 save a datetime in Settings. Default["Day Recieved"]; on a event (button click) and I want it tell me how many days it has been on a textbox in form1. I have tried several approaches, but I cant seem to assign it properly as when I try I get a message saying that I cant convert it, even if I convert it to a datetime type, I made a whole class trying to get it to work properly, one time I had too much converting going on or something and the string passed to textbox was "TestApp.Settings+GetDate" I think that I just cant figure out how to do the right way, I have plenty of more ideas to try to get it to work, but I think there very bad ways, at least in practice, how should one do this properly without making a fool of themselves?
(I am aware I made this kinda hard to read, all I can think about is code right now and clear sentences escape my thought, since I been on this for about 8 hours, so please go easy on me, I am a nice guy :D)

I have found out how to do it, indeed i was over complicating it and have learned my lesson,
Form2:
string when = System.DateTime.UtcNow.Date.AddDays(30).ToString();
Form1.passwhen = when;
Back on form one, declare the global string for passwhen and then do:
textBox.Text = passwhen;
Settings.Default["Day"] = textBox10;
Settings.Default.Save();
I still need to clean it up and make it load the saved setting on load, but this is simple and worked just fine, i was confused because once i save the datetime in settings, VS was saying that System.DateTime and DateTime were two different types and that i couldn't explicitly convert it.

Related

I am trying to enter a formatted chunk of code as a custom field but it never makes it to the database

I am trying to pass this into a custom field called "_vtprd_includeOrExclude"
a:2:{s:23:"includeOrExclude_option";s:11:"includeList";s:29:"includeOrExclude_checked_list";a:2:{i:0;s:2:"1856";i:1;s:4:"1857";}}
I am using WPAllImport to get the data out of an XML package and into the field.
I know that the function works because if I put "bob" in the custom field it goes in without any issue.
What is wrong with this string that is causing it to come up empty?
As always, your feedback and help is greatly appreciated.
The problem was that the data within the string would fail if turned into an array because the string count was incorrect for one of the fields.
The part to look at is at the end.
... a:2:{i:0;s:2:"1856";i:1;s:4:"1857";}}
The offender is i:0;s:2:"1856"
"1856" is 4 string characters long, not to.
So it SHOULD look like this.
i:0;s:4:"1856"
When I made that change I silenced the custom function and it worked just fine.
Perfect example of not knowing enough about how this structure works. So I guess knowing is half the battle.
go joe.

Is the VB string function RIGHT$ still part of current VB.NET or is it left over from old vb?

I just found the below line of code in a project I am working on. I have never seen functions with the $
I found online that it is a vb string function. The page said the "Right$" is more efficient than simply writing "Right"
So is this still current with the most up to date vb language features or is it deprecated?
Right$(sNumToBeFormatted, 8)
I found online that it is a vb string function. The page said the "Right$" is more efficient than simply writing "Right"
VB6 had (and VBA still has) two versions of many string functions.
One version accepted and returned Strings, another one accepted and returned Variants. The string versions had $ in their name to make them stand out.
One cannot say that using Right$ is always better than using Right. It depends on the type of your source and result data.
If you receive data as Variants and send it out as Variants, like e.g. Excel does, using Right will result in fewer conversions between String and Variant.
If your data is originally a String, using Right$ is better.
So is this still current with the most up to date vb language features or is it deprecated?
VB.NET only includes the typed versions, but it does not show the $ anymore.
So Right$ is the up to date version, but it was renamed to simply Right. There is no choice anymore.
There is still choice in VBA, where both versions are valid and supported.
$ sign means that returned value of Right will be string
You can also do this
Dim someString$
which is equivalent to
Dim someString as String
For more go here
As to this question
So is this still current with the most up to date vb language features
or is it deprecated?
There is nothing stopping you from using it as it is supported, but because it is not popular at all it shouldn't be so next person reading code wont be going over it like you are at the moment.
This syntax was already optional in vb6.

ASP.Net - Function output shown before function called

I have the following line of code in ASP.Net (VB)
Response.Write("<td class=""tblRow""><strong>" & ITServiceRow.NAME & " </strong><br>" & funcRAGColour(ITServiceRow.RAGSTATUS) & Environment.NewLine)
This should output the Name from ITServiceRow.NAME followed by the result of the function funcRAGColour.
However this is not the case. ASP.Net is outputting the value of the function funcRAGColour. first followed by the value of ITServiceRow.NAME.
Just trying to understand why this might be happening? If I replace the function with static text it executes fine, but when I put the function in it outputs the function result immediately before the name.
The image here, in yellow shows the full output that comes from the function, it is shown before everything else?
Am I missing something obvious here?
Try using String.Format instead to guarantee placement.
Response.Write(string.Format("<td class=""tblRow""><strong>{0}</strong><br />{1}{2}</td>",funcRAGColour(ITServiceRow.RAGSTATUS),Environment.NewLine))
Always do whatever you can to avoid string concatenation. String concatenation is tough on a system and uses much more memory and resources to be garbage collected than you think because it's actually far more complicated. String.Format and StringBuilder help get around this.
I am very suspect of the function funcRAGColour() itself though and think that is the problem. My guess is the function is not returning the output as a string, but instead is using Response.Write() to output it's result. That would cause it's value to appear first since it is called while the string is being assembled.
Keep in mind, Response.Write is NOT the way to do things in ASP.Net. It was need in classic ASP, but ASP.Net has HtmlTextWriters that can be used during the rendering process, controls for result placement, etc.. It's the old school, non object-oriented way of doing things that can get into trouble.

pass data from parent to popup

Apologies if this seems like a duplicate post...
Thomas Warner kindly answeres an earlier post suggesting I use:
Popup.aspx?Data1=Piece_of_data&Data2=Piece_of_data
Just want to ask, if my code is Popup.aspx?Data1=textbox1.text&Data2=textbox2.text
whats the proper way to reference whats in the textboxes?
The way is is above, all that appears in the popup is the actual text 'textbox1.text'
rather than what is actualy in that control.
thanks again
Using asp.net you can litterally write the value straight into the string like:
Popup.aspx?Data1=<%=textbox1.Text%>&Data2=<%=textbox1.Text%>
A more ideal way of doing this would be to build up the URL string in your codebehind so as not to clutter up your HTML and C# code.
That way you could do something like:
String popupUrl = String.Format("Popup.aspx?Data1={0}&Data2={1}",
textbox1.Text,textbox2.Text);
This will also allow you to do any sanitization checks on the values from the textboxes before you start passing those values around.

Any Reason Why IsNumeric() Fails On A Number?

I currently have this line of code which has been working for the past 6 months:
If IsNumeric(txtProductID.Text) Then
...do stuff
Else
Dim msg As String = "Error!"
End If
All of the sudden, no matter what kind of entry is put in txtProductID (including plain numbers), it fails! Is there reason for me to be going crazy over this?
Kind of a shot in the dark, but one thing to watch for is that maybe someone wrote a private method called IsNumeric within the same class. Are you sure that the code above is executing Microsoft.VisualBasic.IsNumeric()? If you put your cursor on IsNumeric and hit F12 where does the definition point to?
Try Trim()ing the string before passing it into the function. In addition, rather than using a VB-specific function like IsNumeric, you might try an approach like this:
Dim input as Integer
If Integer.TryParse(txtProductID.Text, input) Then
....do stuff with input
Else
Dim msg as String = "Error!"
End if
If your number is a decimal number, there are corresponding functions on Double and Single as well.
As to the particular reason that IsNumeric is failing, I couldn't tell you. I can tell you, though, that I've always found it helpful to stick to BCL-compliant functions that are language-agnostic rather than language-specific, like IsNumeric, Str, etc.
ugh... i'm an idiot... thanks for your help guys, but apparently i was clearing my whole form before accepting input, so "" will never pass as "IsNumeric". Please don't look at this question again. I feel ill.
Thanks again for your help.

Resources