|
I want that my program works regardless of how user configure the system date format.
So I choose the format dd/MM/yyyy , and the user should input the date in this format.
3/4/2001 should be interpreted as April 3 2001.
26/11/2018 is November 26
11/26/2018 - is invalid
|
|
|
|
|
desanti wrote:
3/4/2001 should be interpreted as April 3 2001.
26/11/2018 is November 26
11/26/2018 - is invalid
So, any date that can be interpreted as M/D/Y should be. Anything that fails is to be assumed D/M/Y, and if that fails, it is invalid.
No, I will not participate. Your data is all in the same format, or the entire dataset must be considered invalid.
There's a good reason why DateTime.TryParse Method (System) | Microsoft Docs[^] only accepts a single provider. If you don't know what the format is, and start guessing like you do, you're bound to end up with multiple small disasters.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Maybe we are misunderstanding each other.
The situation is simple :
The user write a date in a textbox.
I want that the user write the date in the format : day/month/year.
So I want a way to check if what the user has wrote can be converted to a date in that format :
- 01/05/2015 - is ok because 01 is a valid for day number , 05 is ok because is valid for month number , 2015 is ok because is valid for year number.
- 25/12/2018 - is ok because 25 is a valid for day number , 12 is ok because is valid for month number , 2018 is ok because is valid for year number.
- 12/25/2018 - is wrong because 12 is a valid for day number , 25 is wrong because is not valid for month number , 2018 is ok because is valid for year number. So it's wrong !!!
What is the problem with this logic ?????
|
|
|
|
|
|
Ok , what about the case when i'm reading the strings from a text File ?
|
|
|
|
|
|
Dim source As String() = {"01/05/2015", "25/12/2018", "12/25/2018"}
Sub Main()
For Each dateCandidate As String In source
Dim dateResult As Date
If Date.TryParseExact(
dateCandidate, "d/M/yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
dateResult) Then
Console.WriteLine("Found {0} as valid.", dateResult)
Else
Console.WriteLine("Found {0} to be invalid.", dateCandidate)
End If
Next
Console.ReadKey()
End Sub
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Thank you , Your code works with these examples and many others.
but as I've said , I read values from a text file . Sometimes the string that come from text file is like this :
25/12/2018 00:01:00
This is correct because the date part is correct , but for your code this is invalid.
|
|
|
|
|
desanti wrote: This is correct because the date part is correct , but for your code this is invalid. I wrote it that way; if you want different behaviour, look the method up on MSDN.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hello everybody. Very silly problem but it's driving me a bit crazy exactly because of its simplicity... Back to VB after 9 years so my apologies in advance if this will turn out being something very dumb. I'm blind to it... shame on me!
In short, the target is to manage multiple screens by placing an MDI container on each and allowing the user to move the mdiChildren from one Parent to another, so that they can be moved on different screens without losing MDI structure.
For the moment it's just academic, so we won't focus on how odd this could seem (it's a control application, not a document editor) or on the fact that one of the mdiParentForms will be a different class because it will be the main one with different controls on it.
As I said this is just a very rough test to check the basic principle.
I built a test project in which I create one instance per each screen in the Startup application event. Reference to all parent created is held in a list declared at a global level
Public Module Module1
Public mps As New List(Of frmMdiParent)
End Module
Partial Friend Class MyApplication
Private Sub MyApplication_Startup(sender As Object, e As StartupEventArgs) Handles Me.Startup
For i As Integer = 0 To Screen.AllScreens.Count - 1
Dim p As New frmMdiParent
p.Location = Screen.AllScreens(i).Bounds.Location
p.StartPosition = FormStartPosition.Manual
p.WindowState = FormWindowState.Maximized
p.Show()
mps.Add(p)
Next i
End Sub
End Class
Just for short, each instantiated mdiParent form creates one child of its own in the Load event.
Public Class frmMdiParent
Private Sub frmMdiParent_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Try
mps.Remove(Me)
Catch : End Try
End Sub
Private Sub frmMdiParent_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim f As New frmChild
f.MdiParent = Me
f.Show()
End Sub
End Class
Each child's caption is set to tickcount for visual disambiguation. Each child has a button named btnSwitch to cycle through parents (again, not refined... just base test).
On click I look for the very next mdiParent form in the list and change the form's mdiParent to that object (variable index is declared private at form level):
Private Sub btnSwitch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSwitch.Click
'not very refined, first shot will be a dumb shot.
'Known, just a very rough test for testing the method
If Index<= mps.Count - 2 Then
Index += 1
Else
Index = 0
End If
Me.MdiParent = mps(Index)
End Sub
Till now everything looks working fine. I have two screens, on both a full-screen mdi parent is displayed and inside each an mdiChild is created, which actually belongs to it.
When I click the btnSwitch command, the child is moved to the other mdiParent, then if I click again it's moved back, and it's switching between the two parents correctly.
Both children originally created in each parent actually do work properly.
- BUT -
I then need to access the mdiParent from the child (e.g. for managing toolstrip items etc). It seems obvious to me to access me.MDIParent property. And... there is the surprise: if I reference from inside the child window class
me.MDIParent property, it returns NOTHING.
I checked every single step of the few lines, and the references returned by the list are valid.
Never nothing.
I added some code and some visual information to try to get a better view of the problem.
It turns out, when the child turns to another parent (which is successfully made by assigning me.MDIParent ), then querying the same property return nothing! I changed the code to dumb-proof (I thought) debug, as follows (what I display on screen has been checked tens of times in the debug, but I just wanted to put it the most clear way)
The funny thing is:
I have an assignation instrucion Me.MdiParent = mps(index) and I checked a lto of times the reference mps(index) is valid, correct and not nothing. So I'm setting a NON NOTHING reference. By the way, the effect on screen is the expected one.
In the very next instruction I read the same property just assigned and it's NOTHING. WTF?!??
Public Class frmChild
Dim index As Integer = 0
Private Sub child_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
For i As Integer = 0 To mps.Count - 1
If mps(i) Is Me.MdiParent Then index = i
Next i
Me.Text = Environment.TickCount 'Just for visual discrimination
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
'Added a second button to do something which references the parent.
'as long as the form has just been created on startup in its original parent,
'everything works. Then the parent is alway snothing (even if returning to original one)
'
Me.MdiParent.WindowState = FormWindowState.Normal '<-NullReferenceException
End Sub
Private Sub btnSwitch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSwitch.Click
'not very refined, first shot will be a dumb shot.
'Known, just a very rough test for testing the method, not a refined app
If index <= mps.Count - 2 Then
index += 1
Else
index = 0
End If
'Checked in debug, it's NEVER nothing.
'Just put a stop clause in case I was blind! Never Stopping, as expected
If mps(index) Is Nothing Then Stop
Me.MdiParent = mps(index)
'Application.DoEvents()
'Just do prove myself I'm not getting confused.
'Yes, I just set the parent to something NOT noting and
'the mdiParent IS nothing! WTF??!!
If Me.MdiParent Is Nothing Then Stop
'AND THE FUN PART IS: the form actually keeps moving from one MDIContainer to another!!!
'Show me visually whether it is nothing or not!
Me.Label1.Text = If(Me.MdiParent Is Nothing, "NOTHING", "OK") 'After first click always displays NOTHING
Me.Label2.Text = index 'always displaying correct index
End Sub
End Class
Tried in both VB2008/framework 3.5 and VS2017/Framework 4.6.1 under Win7/64/Pro
I can't believe I'm struggling with something that stupid! I'm sure it is something stupid, but I'm too blind to see it. (OK, there might be some workarounds, but I really want to understand what I'm doing wrong!) Am I missing something? Could anyone help please? Thanks a lot in advance.
modified 24-Nov-18 4:45am.
|
|
|
|
|
Hello !
I'm using vb.net with EF 6 and sql server 2008R2.
I have one table on database
Myobject ( id , code , name)
I want to add new records but verifying if an object with the same code exist. These records that have the same code are not added.
Dim mynewobject as Myobject
For I=1 to 100
newcode= InputBox("Input code", "Value")
newname= InputBox("Input Name", "Value")
if IsNothing(context.Myobjects.Where(Function(t1) t1.code=newcode).Firstordefault) then
mynewobject=New Myobject
mynewobject.code=newcode
mynewobject.name=newname
context.Myobjects.Add(mynewobject)
end if
Next
context.savechanges
Now let's suppose that i want to add these records :
Id Code Name
1 cd1 Name1
2 cd2 Name2
3 cd1 Name3
The third record should not be added because has the same code with the first record that has been added before.
But when i execute my code , the third record is added too.
What can i do ?
Thank you !
|
|
|
|
|
Try:
If context.Myobjects.All(Function(t1) t1.code <> newcode) AndAlso context.Myobjects.Local.All(Function(t1) t.code <> newcode) Then
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
desanti wrote: What can i do ?
Add a constraint in SQL server to prevent it.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Programming by error AAaaahhhhhhhhh.
I agree that a constraint is valid but the problem is known in the business layer and should be trapped there, the DB constraint should only be insurance.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Mycroft Holmes wrote: Programming by error AAaaahhhhhhhhh. Adding constraints is done to keep the data correct and consistent. It is not "programming by error", it is about preventing them.
Mycroft Holmes wrote: I agree that a constraint is valid but the problem is known in the business layer and should be trapped there, the DB constraint should only be insurance. There "should be" a decent database-design to begin with.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Eddy Vluggen wrote: It is not "programming by error", it is about preventing them. Certainly this is true of a competent developer but I have seen may apps where they are trapping the error returned from the database and then dealing with the issue in the BL instead of dealing with the error before it hits the database.
Never underestimate the power of human stupidity -
RAH
I'm old. I know stuff - JSOP
|
|
|
|
|
Mycroft Holmes wrote: Certainly this is true of a competent developer but I have seen may apps where they are trapping the error returned from the database and then dealing with the issue in the BL instead of dealing with the error before it hits the database. Aw, the horror.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Hello !
I'm using vb.net 2017 with EF 6 and Sql Server 2008 R2.
I'm writing a code to read data from an excel file and after to save on the database.The excel file will have very large number of records. ( maybe 50.000 or even more ).
When is better to call context.savechanges :
- After adding each record
- After adding a number of n records ( !! If yes , what is the best number )
- After adding all records
Thank you !
|
|
|
|
|
desanti wrote: If yes , what is the best number 42.
Ask yourself what happens if you don't save; if the thing crashes, you have to start anew. Saving means rewriting the Excel-file, since it is not a DB - and that will take some time.
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
Quote: Saving means rewriting the Excel-file, since it is not a DB
Sorry ,i'm reading from excel file not writing.
|
|
|
|
|
In a single transaction
Bastard Programmer from Hell
If you can't read my code, try converting it here[^]
"If you just follow the bacon Eddy, wherever it leads you, then you won't have to think about politics." -- Some Bell.
|
|
|
|
|
As always, it depends on business rules.
Should all the records in the Excel sheet be treated individually? Should they be treated as one unit? What happens if you're app crashed or is otherwise interrupted during the write to the database? What do you track, if anything, to recover? Do you start over and rewrite the records you already wrote in the database? Do you try to resume from the last successfully written record?
You COULD save after every record. It'll slow things down but you'll get a verified write on each and every one.
What's the "best" number of records? It's impossible for anyone to say. Again, it depends on a ton of factors. There is no "one value" to rule them all.
You COULD save after the entire set is created. That will result in a long save operation that'll return a single error if there is a problem with any record in the operation.
It's impossible for anyone, other than yourself, to answer your question.
|
|
|
|
|
The problem is that or i should write all the records from Excel to database , or if a single record has problems no records will be saved.
I will make this proces in 2 steps :
1. I read one by one all the records from Excel and test them if they are correct and can be saved to database.If one of the record is ok , I add it on entity. If one record has problems , the context is disposed , and the user has to correct the problems and to start testing again the excel data.
2.If the test procedure goes without error , I call savechanges.
So , i call savechanges after all objects are added to entity.
But i don't know if this is safe , so i want your opinion and i should changes the code that I've described before.
|
|
|
|
|
|
I have an application that runs access database with about 50 tables but i want to search all the tables with Refno since the tables all have it, and i don't want to use the inner join because if it do and some tables don't have data it wont populate, so how do i search all the tables and display on crystal report the code i use for the search is below
thank you
Imports System.Data.OleDb
Imports CrystalDecisions.CrystalReports.Engine
Public Class Qmatereportplus
Private Sub btqgetreport_Click(sender As Object, e As EventArgs) Handles btqgetreport.Click
Getreportdisplay.ReportSource = Application.StartupPath + "\Qmateplusreport.rpt"
Getreportdisplay.SelectionFormula = "{excavationbkfill.Refno} = '" & txtdisplaydetails.Text.ToString() & "'"
Getreportdisplay.Refresh()
Getreportdisplay.RefreshReport()
End Sub
End Class
|
|
|
|
|