|
The DateAdd and DateSerial functions are VB-specific methods intended to provide some level of backwards-compatibility with VB6. They're not particularly efficient, and don't offer anything that the built-in methods on the DateTime type can't do. You should try to avoid using them in any new code.
There's a slight quirt with DateSerial where the month and day parameters can be outside of the valid range - for example, your DateStop passes 0 as the day parameter. If required, you can replicate this behaviour by starting with the 1st January and adding the months and days:
DateSerial(year, month, day)
New DateTime(year, month, day)
New DateTime(year, month, 1).AddDays(day - 1)
New DateTime(year, 1, 1).AddMonths(month - 1).AddDays(day - 1)
For DateAdd , the equivalent depends on the date interval you're adding:
dateValue.AddYears(number)
dateValue.AddMonths(number * 3)
dateValue.AddMonths(number)
dateValue.AddDays(number)
dateValue.AddDays(number * 7)
dateValue.AddHours(number)
dateValue.AddMinutes(number)
dateValue.AddSeconds(number)
For the periods you're using:
Dim todayStart As DateTime = DateTime.Today
Dim todayEnd As DateTime = todayStart.AddDays(1).AddSeconds(-1)
Dim monthStart As New DateTime(DateTime.Today.Year, DateTime.Today.Month, 1)
Dim monthEnd As DateTime = monthStart.AddMonths(1).AddSeconds(-1)
Dim yearStart As New DateTime(DateTime.Today.Year, 1, 1)
Dim yearEnd As DateTime = yearStart.AddYears(1).AddSeconds(-1)
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I didn't know that about DateSerial, it was giving me strange behavior.
I'll rewrite my code to use addMonths, addYears, etc.
And your samples give me strong examples to reference.
Thanks Dave!
|
|
|
|
|
I am developing a Windows Forms application with Visual Studio 2010 and VB.net.
On the "load" of a form I inserted the following code that takes data from a Firebird DB and I would populate a "datagridview" but I can not:
Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown
Try
Dim cn As New OdbcConnection("dsn=PHOENIXDB;uid=SYSDBA;pwd=masterkey;")
Dim mystring As String = "Select * from VW_SSN_REGIONI"
Dim cmd As OdbcCommand = New OdbcCommand(mystring)
Dim daSSNregioni As New OdbcDataAdapter
Dim dsSSNregioni As New DataSet
cn.Open()
daSSNregioni.SelectCommand = cmd
daSSNregioni.SelectCommand.Connection = cn
daSSNregioni.Fill(dsSSNregioni, "SSN_REGIONI")
Me.DataGridViewX1.DataSource = dsSSNregioni
cn.Close()
Catch ex As NullReferenceException
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Connection Error !!")
Catch ex As Exception
MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly, "Connection Error !!")
End Try
End Sub
Thanks
|
|
|
|
|
change
Me.DataGridViewX1.DataSource = dsSSNregioni
to
Me.DataGridViewX1.DataSource = dsSSNregioni.Tables(0)
Every day, thousands of innocent plants are killed by vegetarians.
Help end the violence EAT BACON
|
|
|
|
|
Thanks.... all ok
|
|
|
|
|
Attempted to read or write protected memory. This is often an indication that other memory is corrupt in vb.net
|
|
|
|
|
You probably have some parameter mismatch in some P/Invoke function call. But since you decided not to share any kind of information about your code, or where the error is occurring, we can only guess.
You can lead a developer to CodeProject, but you can't make them think.
The Theory of Gravity was invented for the sole purpose of distracting you from investigating the scientific fact that the Earth sucks.
|
|
|
|
|
Considering that there is about a thousand different causes for an error like this, it's impossible to tell you anything useful without a lot more information from you.
|
|
|
|
|
This is a stumper.
In my SQL Linq, I Projected the results into a class in IEnumerable(of Class)
One of the columns is ResponseDate, Which is a DateTime in the class I project into,
a DateTime in the Model
But on the VB Side, but I go to use the class as an Object
For m As Object in Messages
.Text = m.ResponseDate.ToString("MM/dd/yy H:mm")
is not a valid integer.
This I get is not a valid DateTime
.Text = DateTime.ParseExact(m.ResponseDate, "MM/dd/yy H:mm", CultureInfo.InvariantCulture)
I'm not sure what to think on this. I thought by projecting into the class, that it would keep it's type and cast correctly.
|
|
|
|
|
The problem is you told VB to treat m as an Object, which doesn't have a ResponseDate property.
|
|
|
|
|
OK, I used the as class name this time.
Had trouble with it in the Movie Program, I'll revisit that tommorrow.
Thanks!
|
|
|
|
|
Good day,
I have a requirement to log vehicle CANBUS data from a Lawicell USB CANBUS adapter. My old XP (VB6) laptop had "Ports (Comm and LPT)" settings in the device manager that i could access and change to make sure the comm port in use by the adapter was available and one of ports 1-4 as this is the range of ports my bit of code looks for. On my new Win7 (VS2008) laptop "Ports" are not shown even though the CANUSB adapter works just fine.
The code i'm using (which worked o.k. on the old pc) uses the System.IO.Ports.SerialPort.GetPortNames method to look for comm ports, but this is causing problems on the new laptop as there don't appear to be any comm ports (when debugging, the method shows 0 ports available even with the adapter plugged in and otherwise working) and so it shows an error.
My question is what method/code could i use to either detect the adapter without using the serial port name method, or, given that i can probably find its ID and location and that the code and adapter won't be used on any other PC can i code specifically for that adapter on this single PC? If so, how.
I tried to use slelong's USB library code A USB Library to Detect USB Devices[^]
but this use's a USBClassLibrary class that isn't available in VS2008.
Many thanks,
|
|
|
|
|
|
Thanks for the prompt reply. I've now recovered from the heart failure that link gave me
Wish i'd bought a set of waders, i think i needed them.
But it did sort of answer one question i.e. the code in the link was aiming to show ALL connected devices and then find their ID's. I already know the device ID and its location so can i just hardcode for that one device on this specific pc and if so how do i get the data from it like i can if it's a serial port?
That code showed how to find the device but not how to 'talk' to it.
|
|
|
|
|
|
hahaha. Right, i'll take that as "it'll be easier to rebuild the old XP PC and use that".
Thanks anyway.
|
|
|
|
|
So I wrote this in SQL Linq for inserting a record. But I think I need a function in my DAL in order to add the record before saving it.
Using context As New CRMContext()
Dim pResults As New CRM_MESSAGES With
{
.Subject = sCM.m_Contact_Subject
}
context.SaveChanges()
So in my context, a class in my DAL DLL, I wrote a SaveChanges. do I need to write another function in order to insert the new row, and then save. Would this be an Overrides function? I looked for some samples, haven't found one yet. Having trouble using the right keywords for this.
Public Class CRMContext
Inherits DbContext
Public Sub New()
MyBase.New("DefaultConnection")
Database.SetInitializer(New CreateDatabaseIfNotExists(Of CRMContext))
Entity.Database.SetInitializer(Of CRMContext)(Nothing)
End Sub
Public Property CRM_MESSAGES As DbSet(Of CRM_MESSAGES)
Public Overrides Function SaveChanges() As Integer
ChangeTracker.DetectChanges()
'Call IPrepareForSave on entities that support it...
Return MyBase.SaveChanges()
End Function
|
|
|
|
|
Looked up DBContext, and added a couple of Imports to my Context
Imports System.Data.Entity.Infrastructure
Imports System.Data.Entity.Core.Objects
Not sure if that's what provided the Add Function, I would have to go back and remove the imports to figure that out.
but it was
context.model.add(new copy of model)
context.SaveChanges()
I moved the try catch around the Add and save, but I think I should just dump the Try and Catch all together.
|
|
|
|
|
I am new to vb .net and working on VS 2012. I have a datagridview which displays the rows of an ms-access 2007 database table. I created a form (with TeztBoxes and Add/Update/Cancel buttons - all dynamically (using the language)) which shows when a row of the datagridview is selected. So far, so good.
Now, I want the values of the fields (of the selected row) to be displayed in the textboxes and any changes made to the textboxes to be added/updated in the database, depending upon the button clicked. How can it be done? That is, how to: 1. Fill in the textboxes with row values, and, 2. Update/Add the changes to the database?
Basically, the REAL PROBLEM is REFERENCING the dynamically created controls on the form! I am getting nullreferenceexceptions! Please help!
Many thanks!
|
|
|
|
|
So.... you have a problem with your code and expect that we can see your code somehow and tell you what is wrong with it?
You can lead a developer to CodeProject, but you can't make them think.
The Theory of Gravity was invented for the sole purpose of distracting you from investigating the scientific fact that the Earth sucks.
|
|
|
|
|
I am sorry indeed! I didn't post the code as it is quite long. However, I have shortened it to a good extent now.
Here is my code:
====================================================
Private Sub DGVClients_RowHeaderMouseClick(sender As Object, e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles DGVClients.RowHeaderMouseClick
MessageBox.Show("You have selected row No. " & e.RowIndex.ToString())
Dim txtClientId As New TextBox
With txtClientId
.Size = New System.Drawing.Size(150, 20)
.Location = New System.Drawing.Size(150, 20)
End With
Dim txtClientName As New TextBox
With txtClientName
.Size = New System.Drawing.Size(150, 20)
.Location = New System.Drawing.Size(150, 45)
End With
Dim txtClientAddress As New TextBox
With txtClientAddress
.Size = New System.Drawing.Size(200, 20)
.Location = New System.Drawing.Size(150, 75)
End With
Dim lblCId, lblCName, lblCAdd As Label
lblCName = New Label
lblCId = New Label
lblCAdd = New Label
With lblCId
.Text = "Client Id:"
.Font = New Font("Arial", 12)
.Location = New System.Drawing.Size(20, 20)
.Show()
End With
With lblCName
.Text = "Client Name:"
.Font = New Font("Arial", 12)
.Location = New System.Drawing.Size(20, 45)
.Show()
End With
With lblCAdd
.Text = "Client Address:"
.Font = New Font("Arial", 12)
.Location = New System.Drawing.Size(20, 75)
.Show()
End With
Dim btnAddC As Button
btnAddC = New Button
With btnAddC
.Text = "Add Client"
.Font = New Font("Arial", 10)
.Size = New System.Drawing.Size(100, 35)
.Location = New System.Drawing.Size(20, 260)
End With
AddHandler btnAddC.Click, AddressOf Me.AddNewClient
Dim curForm As Form = New Form
With curForm
'Definition of the form
.Text = "Add / Update Client"
.Size = New System.Drawing.Size(400, 350)
.StartPosition = FormStartPosition.CenterScreen
.Show()
.Controls.Add(lblCId)
.Controls.Add(txtClientId)
.Controls.Add(lblCName)
.Controls.Add(txtClientName)
.Controls.Add(lblCAdd)
.Controls.Add(txtClientAddress)
.Controls.Add(lblCCity)
.Controls.Add(btnAddC)
End With
End Sub
Friend Sub AddNewClient(ByVal sender As System.Object, ByVal e As System.EventArgs)
'MessageBox.Show("Button AddClient clicked!")
MsgBox(txtClientId.ToString)
End Sub
=======================================================
It is the "Sub AddNewClient()" that I am struck up in. The controls of the created form are inaccessible in the buttonClick event handling sub. Also, if I DirectCast the controls, nullrefernceexception is thrown.
I appreciate if you could please sort it out for me.
Many thanks!
modified 12-Jul-15 2:32am.
|
|
|
|
|
I would suggest the following :
1. : I would not use the RowHeaderMouseClick - better use the CellClick-Event
2. : For editing I prefer a Dialog - perhaps startet by a ContextMenue or by right MouseButton in combination with the CellMouseClick-Event. In this Dialog you could manipulate the Cell-Values you want and write it back with a Button-Click. Inportant : every time you know which row/column you are acessing ...
3. : to add a new Entry I prefer a similar way like decribted. Perhaps you start this also by a ContextMenue or with an external Button - but the data is entered inside a Dialog. With closing the Dialog you decide by Button-Click what should happen with the entered data ...
|
|
|
|
|
@Ralff Meier
Could you please post some "code" (example) for it? Better still, if you could please modify my code as stated above. Thanks.
|
|
|
|
|
Are you familiar with creating Dialogs ?
I could guide you in creating what is necessary.
At first :
- a Dialog is a Form. On this Form you have placed the TextBoxes as needed by yourself. On the bottom of the Form (for example) you place 2 Buttons ("Accept Changes" and "Abort"). This Dialog also has Properties, with which you give the necessary data from the calling method to the Dialog (and back again). With the 2 Buttons you give a DialogResult to the calling method which says, that the changes are valid (or not).
Then :
The calling method (performed by your Click-Event) instances the Dialog, gives it the necessary data and shows it. When finishing the Dialog you get your DialogResult which tells you, if the data has to be written back or what else.
I prefer, that you first start by yourself and I help when Help is needed ...
I think, the first steps (creating the Dialog) could be gone by you ...
|
|
|
|
|
Sorry Ralf, but I haven't yet worked on Dialogs. However, I would try it once this issue is resolved. I can't start it afresh as the existing code I am working upon is quite huge for me to handel at the moment!
If you could please suggest an edit for my EXISTING code, it would be of great help! Thanks again!
|
|
|
|
|