|
I have textbox for inserting date called startdate
i want when user insert date get last day of month in another textbox
forexample startdate: 23/09/2018 "my format is dd/mm/yyyy"
lastdate: 30/09/20
|
|
|
|
|
Have some google foo Google Search[^]
You should learn to do some basic research, it is MUCH quicker.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
It's quite easy. See:
System.Globalization.CultureInfo ci = new System.Globalization.CultureInfo("en-US");
DateTime dt1 = DateTime.ParseExact("23/09/2018", "dd/MM/yyyy", ci);
DateTime dt2 = new DateTime(dt1.Year, dt1.Month,1).AddMonths(1).AddDays(-1);
Console.WriteLine("{0} => {1}", dt1.ToString("yyyy-MM-dd"), dt2.ToString("yyyy-MM-dd"));
For further details, please see:
DateTime Constructor (System) | Microsoft Docs
How to: convert strings to DateTime | Microsoft Docs
Good luck!
[EDIT]
Please, read OriginalGriff's comment to my answer. In my code is one bug, which you have to handle.
Thanks, OG!
modified 24-Sep-18 4:06am.
|
|
|
|
|
Um...
DateTime dt2 = new DateTime(dt1.Year, dt1.Month+1,1).AddDays(-1); Did you test that in December?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Good point!
No. Should i?
This part belongs to OP
[EDIT]
Corrected!
Thanks, OG!
|
|
|
|
|
You're welcome!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Marciej has the right idea, but his method has a problem: it will fail in December, because it tries to create a date in the 13th month of the year.
Have a look at this: DateTime Extensions to Make Some Simple Tasks a Little More Readable[^] - it includes a "Last day of month" function.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
|
Absolut Beginner can someone explain why i get stackoverflow on this code and how to avoid this ?
Stackoverflow error
After adding this lines to my code i get the stack overflow
static void Udskriv2() {
Console.WriteLine("Start Udskriv 2");
/Udskriv1();
Console.WriteLine("Slut Udskriv 2");
using System;
using System.Collections.Generic;
using System.Text;
namespace Metodekald_H4
{
class Program
{
static void Main(string[] args) {
Console.WriteLine("Start Main");
Udskriv1();
Console.WriteLine("Slut Main");
}
static void Udskriv1() {
Console.WriteLine("Start Udskriv 1");
Udskriv2();
Console.WriteLine("Slut Udskriv 1");
}
static void Udskriv2() {
Console.WriteLine("Start Udskriv 2");
/Udskriv1();
Console.WriteLine("Slut Udskriv 2");
}
static void Udskriv3() {
Console.WriteLine("Start Udskriv 3");
Console.WriteLine("Slut Udskriv 3");
}
}
}
|
|
|
|
|
Because Udskriv1 calls Udskriv2 , and Udskriv2 calls Udskriv1
That's called recursion, and it means that because every time a method gets called it ends up being called again before it exits - and because each call needs space on some memory called "the stack" to hold the address it has to go back to when it returns the stack fills up very quickly and you get an error because it has nowhere else it can put addresses.
In this case, it's "indirect recursion" because an intermediate method is involved, but exactly teh same thing happens, just slightly quicker.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Thanks, now i see whats going on.
I have to find a solution to avoid this error, could it be to make a little pause in the code to let the code be finish before it starts again ?
in the school assignment i have on this i have to find a solution´S ! on how to deal with this.
|
|
|
|
|
No, a pause won't make any difference.
This is going to be difficult to explain without being able to tell when your eyes glaze over, but I'll try.
Suppose you have a stack of coins and a piece of paper. Each time you turn the paper over, you do exactly what it says.
So you turn the paper and it says:
1) Put a coin on the stack.
2) Turn over the paper. So you do.
The stack grows one bigger, and you turn the paper. This time it says:
1) Put a coin on the stack.
2) Turn over the paper. So you do.
The stack grows one bigger, and you turn the paper. You know what it says, because you are back where you started!
Each time you turn the paper, the stack of coins gets bigger, and bigger.
At some point, it reaches the ceiling, and you can't add another - but you try to force a coin on, and the whole pile falls to the ground with a crash!
That's what happens with unbounded recursive methods - you keep on calling it, and the stack gets bigger, and bigger until it runs out of space and your application crashes.
Adding a delay doesn't "fix the problem", it just means it takes longer in real-world time to get there, just as it does with the pile of coins. Even if you change one side of the paper to read
1) Put a coin on the stack.
2) Wait 30 seconds.
3) Turn over the paper. The same problem will happen, the stack will still collapse, but it's happen later on in the day.
The way to fix this is: either get rid of the recursion, or add a boundary condition which lets you out of the method without the recursive call. There really is no other solution!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Wery good expl. !!!
For a minute ago, i talked with another student, he was at the same course last year!
He had in his soulution written that a programmer should not write more code to solve problem as this.
Just remove the code causing this !
Thanks again !
/Thomas
|
|
|
|
|
You're welcome!
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi all i have been trying to post files for a third party tool through rest api with MultipartFormDataContent. Even though i am writing files to MultipartFordatacontent in byte arry i am receiving error saying "400 bad request no filedata section found in multipart post request".
Request:
Method: POST, RequestUri: 'url', Version: 1.1, Content: System.Net.Http.MultipartFormDataContent, Headers:
{
Content-Type: multipart/form-data; boundary="4c10d3d0-f2ac-467e-9b46-3ca172eb646d"
Content-Length: 185
}
--c4757eb4-aefd-450a-a5cc-e879ac7f37ce
Content-Disposition: form-data; name=filedata; filename=pp.txt
Content-Type: text/plain
test file
--c4757eb4-aefd-450a-a5cc-e879ac7f37ce--
Response:
StatusCode: 400, ReasonPhrase: 'Bad Request', Version: 1.1, Content: System.Net.Http.StreamContent, Headers:
{
x-transaction-id: 99999_fileuploaddownload_staging_1537571048380:f4e44b3ca701:28:jm91oq79:10113
vary: accept-encoding
Connection: keep-alive
Cache-Control: no-cache
Date: Fri, 21 Sep 2018 23:04:08 GMT
Content-Length: 104
Content-Type: application/json; charset=utf-8
}
{"statusCode":400,"error":"Bad Request","message":"No filedata section found in multipart POST request"}
|
|
|
|
|
This is a web development issue, nothing to do with C#.
|
|
|
|
|
hello
i stored image in sql server DB by follow command:
command.Parameters.Add("@attach", SqlDbType.VarBinary).Value = bytFile;
but now i want to do it by
Parameters.AddWithValue
command.Parameters.AddWithValue("@attach", bytFile);
but it dont work .
thanks
|
|
|
|
|
"It dont work" is probably the most useless problem report we get - and we get it a lot. It tells us nothing about what is happening, or when it happens.
So tell us what it is doing that you didn't expect, or not doing that you did.
Tell us what you did to get it to happen.
Tell us any error messages.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
i have create code like this right now,it is small game. it will show grid pictureboxes when user enter rows and columns in textboxes. also i have several button with image. when i click the button,that mean i choose the image on that button and when i click the picturebox, that image will showing on that picturebox.also user can click button(image) to change the image on picture box.
i don't know how to modify my picturebox image with button.
private void btnGenerate_Click(object sender, EventArgs e)
{
panel3.Controls.Clear();
int rows = Convert.ToInt32(txtRows.Text);
int columns = Convert.ToInt32(txtColumns.Text);
PictureBox[,] map = new PictureBox[rows,columns];
for (int j = 0; j < columns; j++)
{
for (int i = 0; i < rows; i++)
{
System.Threading.Thread.Sleep(50);
map[i, j] = new PictureBox();
map[i, j].Width = 45;
map[i, j].Height = 45;
map[i, j].BackColor = Color.NavajoWhite;
if (i == 0 && j == 0)
{
map[i, j].Location = new Point(100, 100);
}
map[i, j].Location = new Point(45 * i + 100, 45 * j + 100);
map[i, j].BorderStyle = BorderStyle.Fixed3D;
panel3.Controls.Add(map[i, j]);
}
}
}
|
|
|
|
|
Message Removed
modified 2-Oct-18 16:27pm.
|
|
|
|
|
I want to perform some communication between two child forms that are each opened from a parent form (separate button to show each form). I want to change the visibility and value of a numericalUpDown and also the visibility and text of a textbox.
I have used properties, the get part is working perfectly, but the set part works only on the child.show() event, e.g. when I click on Child1Form.button to show the Child2Form the "set" part works, but if I do not show Child2Form, it does not work. and i also do not want to use from setting files.
I used form event handling. The method has gotten fired and the message has shown correctly, but the changes in text and visibility have not.
On the parents side where I call the publisher form, means Child3 form:
Child3 child3 = new Child3();
Child1 child1 = new Child1();
child3.Child1Button2Clicked += child1.child3_Child1Button2Clicked;
child3.Show();
On the subscriber(listener) side:
Event Handling between two child forms that each open from the same parent (Two child - one parent).
public void child3_Child1Button2Clicked(object sender, FormsCommunication e)
{
NumericalUD1.Visible = e.NumericalUDPass;
textBox1.Text = e.textBoxPass;
MessageBox.Show("it got fire" + ": " + e.NumericalUDPass.ToString() + ": " + e.textBoxPass.ToString());
}
On the publisher side:
public event EventHandler<FormsCommunication> Child1Button2Clicked;
//button click event to set the values and fire the method.
private void Child3Btn1_Click(object sender, EventArgs e)
{
FormsCommunication formsCommunication = new
FormsCommunication("2782",Convert.ToBoolean(false));
OnChild1Button2Clicked(formsCommunication);
}
protected virtual void OnChild1Button2Clicked(FormsCommunication e)
{
Child1Button2Clicked?.Invoke(this, e);
}
//the constructor in the EventArgs Class:
public FormsCommunication(string textBox, bool NumericalUD)
{
textBoxPass = textBox;
NumericalUDPass = NumericalUD;
}
public string textBoxPass { get; private set; }
public bool NumericalUDPass { get; private set; }
|
|
|
|
|
Do yourself a favour and scrap that - form2 shouldn't even know form 3 exists, much less try to modify it's behaviour.
See here: Transferring information between two forms, Part 3: Child to Child[^] Form1 needs to know they both exist because it opens them - so all communication goes via the parent.
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
modified 20-Sep-18 3:28am.
|
|
|
|
|
Thank you so much for your help.
Is it safe to use from setting files in the user scope and share it between two text boxes for example?
|
|
|
|
|
Why would you want to do something that cumbersome?
Perhap you need to explain in more detail exactly what you are trying to do?
Sent from my Amstrad PC 1640
Never throw anything away, Griff
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
AntiTwitter: @DalekDave is now a follower!
|
|
|
|
|
Hi, thanks a million for your reply.
Sometimes there are many data that i want to share between two forms. So it is easier to use from properties.settings.default (for me). that works very good and i can save and retrieve my data in the most simple way. So sometimes i prefer to use from them. setting files with user scope, are accessible all over the program directly. So i am doubtful about their security hazards. That is why i have asked the question.
|
|
|
|
|