Showing posts with label vbnet. Show all posts
Showing posts with label vbnet. Show all posts

Tuesday, March 27, 2012

Date problem with ASP.NET but not in VB.NET Winform App

Edited by SomeNewKid. Please post code between<code> and</code> tags.


Hello, I have given up after 3 hours of trying to get a DBNull.Value to be inserted into SQL 2000 DB. Below is the code that exists in an object that I use both from a VB.NET Windows application and from an ASP.NET application. (same exact compiled object dll). Has ran fine with windows application for almost a year - still does. Use the same object in my new ASP.NET application and I get a SQL Overflow error that says date must be between 1753 12 am etc etc... when trying to insert null using the code below.

This one has me stumped. The lines below where I set the date are basically this (what the logic equates to):

oDR.Item("CT_CustomDate1") = DBNull.Value
oDR.Item("CT_CustomDate2") = DBNull.Value

Error gets thrown when this line is executed:

oDA.Update(oDT)

Also, the CT_CustomDate1 & 2 fields in the SQL Server 2000 DB are of type SmallDateTime and the variables in object used in code function below are Date. I've tried every combination of Date, DateTime, SmallDateTime, etc. etc. to no avail.

Thanks for you help!

 Public Function AddNew(ByVal sGUID As String) As Boolean
'Saves all new contact information to database
Dim sSQL As String
sSQL = "SELECT * FROM Contact WHERE 1=2"
Dim oConn As New SqlConnection(sConnectionString)
Dim oDT As New DataTable
Dim oDA As New SqlDataAdapter(sSQL, oConn)
Dim oDR As DataRow
Dim bSuccess As Boolean

Try
oDA.Fill(oDT)

Dim dataCommandBuilder As New SqlCommandBuilder(oDA)
oDA.InsertCommand = dataCommandBuilder.GetInsertCommand
oDA.DeleteCommand = dataCommandBuilder.GetDeleteCommand
oDA.UpdateCommand = dataCommandBuilder.GetUpdateCommand

oDR = oDT.NewRow

oDR.Item("CT_GUID") = sGUID
oDR.Item("CT_CompanyGUID") = CTCompanyGUID
oDR.Item("CT_Prefix") = CTPrefix
oDR.Item("CT_FirstName") = CTFirstName
oDR.Item("CT_MiddleName") = CTMiddleName
oDR.Item("CT_LastName") = CTLastName
oDR.Item("CT_Suffix") = CTSuffix
oDR.Item("CT_Title") = CTTitle
oDR.Item("CT_ContactAddress") = CTContactAddress
oDR.Item("CT_Address1") = CTAddress1
oDR.Item("CT_Address2") = CTAddress2
oDR.Item("CT_City") = CTCity
oDR.Item("CT_State") = CTState
oDR.Item("CT_Zip") = CTZip
oDR.Item("CT_Country") = CTCountry
oDR.Item("CT_InternationalPhone") = CTInternationalPhone
oDR.Item("CT_Phone") = CTPhone
oDR.Item("CT_Fax") = CTFax
oDR.Item("CT_Cell") = CTCell
oDR.Item("CT_AltPhone") = CTAltPhone
oDR.Item("CT_Assistant") = CTAssistant
oDR.Item("CT_AssistantPhone") = CTAssistantPhone
oDR.Item("CT_NoEmail") = CTNoEmail
oDR.Item("CT_EmailAddress") = CTEmailAddress
oDR.Item("CT_Confidential") = CTConfidential
oDR.Item("CT_TypeGUID") = CTTypeGUID
oDR.Item("CT_Designation") = ""
oDR.Item("CT_LastUpdate") = CTLastUpdate
oDR.Item("CT_UpdatedByWho") = CTUpdatedByWho
oDR.Item("CT_Location") = CTLocation
oDR.Item("CT_CustomBit1") = CTCustomBit1
oDR.Item("CT_CustomBit2") = CTCustomBit2
oDR.Item("CT_CustomBit3") = CTCustomBit3
oDR.Item("CT_CustomBit4") = CTCustomBit4
oDR.Item("CT_CustomBit5") = CTCustomBit5
oDR.Item("CT_CustomBit6") = CTCustomBit6
oDR.Item("CT_CustomBit7") = CTCustomBit7
oDR.Item("CT_CustomStr1") = CTCustomStr1
oDR.Item("CT_CustomStr2") = CTCustomStr2
oDR.Item("CT_CustomStr3") = CTCustomStr3
oDR.Item("CT_CustomStr4") = CTCustomStr4
oDR.Item("CT_CustomStr5") = CTCustomStr5
oDR.Item("CT_CustomStr6") = CTCustomStr6
oDR.Item("CT_CustomStr7") = CTCustomStr7
oDR.Item("CT_CustomStr8") = CTCustomStr8
oDR.Item("CT_CustomStr9") = CTCustomStr9
oDR.Item("CT_CustomStr10") = CTCustomStr10
oDR.Item("CT_CustomStr11") = CTCustomStr11
oDR.Item("CT_CustomStr12") = CTCustomStr12
oDR.Item("CT_CustomStr13") = CTCustomStr13
oDR.Item("CT_CustomPhone1") = CTCustomPhone1
oDR.Item("CT_CustomPhone2") = CTCustomPhone2
oDR.Item("CT_CustomPhone3") = CTCustomPhone3
oDR.Item("CT_CustomPhone4") = CTCustomPhone4
oDR.Item("CT_CustomDate1") = IIf(CTCustomDate1 = #12:00:00 AM#, DBNull.Value, CTCustomDate1)
oDR.Item("CT_CustomDate2") = IIf(CTCustomDate2 = #12:00:00 AM#, DBNull.Value, CTCustomDate2)
'oDR.Item("CT_CustomDate1") = CDate(#1/1/1753#)
'oDR.Item("CT_CustomDate2") = CDate(#1/1/1753#)
oDR.Item("CT_CustomAmount1") = CTCustomAmount1
oDR.Item("CT_CustomAmount2") = CTCustomAmount2
oDR.Item("CT_CustomType1GUID") = CTCustomType1GUID
oDR.Item("CT_CustomType2GUID") = CTCustomType2GUID
oDR.Item("CT_CustomCompany1GUID") = IIf(CTCustomCompany1GUID = "00", DBNull.Value, CTCustomCompany1GUID)
oDR.Item("CT_CustomCompany2GUID") = IIf(CTCustomCompany2GUID = "00", DBNull.Value, CTCustomCompany2GUID)
oDR.Item("CT_CustomContact1GUID") = IIf(CTCustomContact1GUID = "00", DBNull.Value, CTCustomContact1GUID)

oDT.Rows.Add(oDR)
oDA.Update(oDT)
CTGUID = sGUID
bSuccess = True
Catch err As Exception
MsgBox("Error saving new contact..." & vbCrLf & vbCrLf & err.Message, MsgBoxStyle.Critical + MsgBoxStyle.OKOnly, "Database Error")
bSuccess = False
End Try

AddNew = bSuccess

End Function

Are you sure your ASP.NET application are referencing the same assemblies as the
WinForm?
Are they running under the same Framework version?
DataAccessApplicationBlock?

Regards
Fredriksql

Monday, March 19, 2012

Date insert from VB.net problem

Hey there,

I'm trying to add rows to a table and pass a date value through. I use Format(Date.now, "dd MMM yyyy") however when I check in SQL Server Managment Studio it has swapped the day and month round (e.g. input of "7/9/2006" will be "9/7/2006" on the actual table. Actually the output is 2006-07-09 00:00:00:00.) I've put the full source code below.

Can anyone help with this?

Cheers,

Danny

Imports System.Data

Imports System.Data.SqlClient

Public Class Form1

Dim dtdate As Date

Dim cn As New SqlConnection("server=*********;database=********;USER ID=*********;password=*********")

Dim objCommand As New SqlCommand("", cn)

Dim ws As New wrtRefID.Service

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

dtdate = Now

AddHistory("ActivityID", 1, "AccountID", "AccountName", "ContactName", "Category", Microsoft.VisualBasic.Format(dtdate, "dd MMM yyyy"), 2, "Description", "UserName", Microsoft.VisualBasic.Format(dtdate, "dd MMM yyyy"), "CreateUser", "Notes", "LongNotes")

MessageBox.Show("DONE")

End Sub

Private Sub AddHistory(ByVal ActivityID As String, ByVal Type As Integer, ByVal AccountID As String, _

ByVal AccountName As String, ByVal ContactName As String, ByVal Category As String, ByVal StartDate As Date, _

ByVal Duration As Integer, ByVal Description As String, ByVal UserName As String, _

ByVal CreateDate As Date, ByVal CreateUser As String, ByVal Notes As String, ByVal LongNotes As String)

cn.Open()

objCommand.CommandText = "INSERT INTO HISTORY (HISTORYID, ACTIVITYID, Type, ACCOUNTID, ACCOUNTNAME, CONTACTNAME, " & _

"CATEGORY, STARTDATE, DURATION, DESCRIPTION, USERNAME, CREATEDATE, CREATEUSER, NOTES, LONGNOTES) " & _

"VALUES ('" & ws.MCS2_SLXID("HISTORY") & "', " & "'" & ActivityID & "', '" & Type & "', '" & AccountID & "', '" & AccountName & "', '" & ContactName & _

"', '" & Category & "', '" & StartDate & "', '" & Duration & "', '" & Description & "', '" & UserName & _

"', '" & CreateDate & "', '" & CreateUser & "', '" & Notes & "', '" & LongNotes & "')"

' MessageBox.Show(Format(dtdate, "dd MMM yyyy"))

TextBox1.Text = objCommand.CommandText

objCommand.ExecuteNonQuery()

cn.Close()

End Sub

End Class

Before explaining how to fix the date issue, it's important

to point out that your code may be vulnerable to a damaging

SQL injection attack. If the USER ID here has permission

to do more than insert rows into the table HISTORY, and if

you are not validating input in any way on the client side,

you are in trouble. A malicious user of your application

could enter into LongNotes, for example, a single quote

character followed by any SQL statement he or she wants to

run (like DELETE FROM HISTORY, or much worse, if this session

is opened by the sa account). See this article for more

information: www.sommarskog.se/dynamic_sql.html, or search

the web for "SQL injection."

Please, please, please, always use parameterized queries

or SQL stored procedures (and in the latter case, not

ones that just concatenate user input themselves).

You may be validating input on the client side or using

an account with limited privilege, in which case you should

at least add a comment in your code to note that this code

relies on security to be implemented elsewhere. However,

in my opinion, client-side-only security for this sort

of thing is not sufficient. Just to give one example,

some web sites will enforce what is typed into a form,

but overlook the fact that the form may not be the only

place to supply parameter values - direct specification

in the URL might be, too, and those values won't be

validated.

The best way to pass a date is through a parameterized

query or stored procedure, in which case you don't have

to use any format - you just pass it as a datetime value,

such as a SqlDateTime.

If you must pass your date as a string (and it's unlikely you

must, but it's a quicker, if more reckless, fix from what

you have now than to address the security and use parameters),

use the one date-only string format that SQL Server will

interpret consistently across culture settings: 'yyyymmdd'.

Be sure it's the string 'yyyymmdd', and not the integer yyyymmdd.

The only other safe format is the full datetime format

'YYYY-MM-DDTHH:MM:SS.mmm' (including the T character).

Vulnerable code like this is all too common, and I want

to say something whenever I see it.

Steve Kass

Drew University

http://www.stevekass.com

JediDanny@.discussions.microsoft.com wrote:

> This post has been edited either by the author or a moderator in the

> Microsoft Forums: http://forums.microsoft.com

>

> Hey there,

>

> I'm trying to add rows to a table and pass a date value through. I use

> Format(Date.now, "dd MMM yyyy") however when I check in SQL Server

> Managment Studio it has swapped the day and month round (e.g. input of

> "7/9/2006" will be "9/7/2006" on the actual table. Actually the output

> is 2006-07-09 00:00:00:00.) I've put the full source code below.

>

> Can anyone help with this?

>

> Cheers,

>

> Danny

>

>

>

> Imports System.Data

>

> Imports System.Data.SqlClient

>

> Public Class Form1

>

> Dim dtdate As Date

>

> Dim cn As New SqlConnection("server=*********;database=********;USER

> ID=*********;password=*********")

>

> Dim objCommand As New SqlCommand("", cn)

>

> Dim ws As New wrtRefID.Service

>

> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

> System.EventArgs) Handles Button1.Click

>

> dtdate = Now

>

> AddHistory("ActivityID", 1, "AccountID", "AccountName", "ContactName",

> "Category", Microsoft.VisualBasic.Format(dtdate, "dd MMM yyyy"), 2,

> "Description", "UserName", Microsoft.VisualBasic.Format(dtdate, "dd MMM

> yyyy"), "CreateUser", "Notes", "LongNotes")

>

> MessageBox.Show("DONE")

>

> End Sub

>

> Private Sub AddHistory(ByVal ActivityID As String, ByVal Type As

> Integer, ByVal AccountID As String, _

>

> ByVal AccountName As String, ByVal ContactName As String, ByVal Category

> As String, ByVal StartDate As Date, _

>

> ByVal Duration As Integer, ByVal Description As String, ByVal UserName

> As String, _

>

> ByVal CreateDate As Date, ByVal CreateUser As String, ByVal Notes As

> String, ByVal LongNotes As String)

>

> cn.Open()

>

> objCommand.CommandText = "INSERT INTO HISTORY (HISTORYID, ACTIVITYID,

> Type, ACCOUNTID, ACCOUNTNAME, CONTACTNAME, " & _

>

> "CATEGORY, STARTDATE, DURATION, DESCRIPTION, USERNAME, CREATEDATE,

> CREATEUSER, NOTES, LONGNOTES) " & _

>

> "VALUES ('" & ws.MCS2_SLXID("HISTORY") & "', " & "'" & ActivityID & "',

> '" & Type & "', '" & AccountID & "', '" & AccountName & "', '" &

> ContactName & _

>

> "', '" & Category & "', '" & StartDate & "', '" & Duration & "', '" &

> Description & "', '" & UserName & _

>

> "', '" & CreateDate & "', '" & CreateUser & "', '" & Notes & "', '" &

> LongNotes & "')"

>

> ' MessageBox.Show(Format(dtdate, "dd MMM yyyy"))

>

> TextBox1.Text = objCommand.CommandText

>

> objCommand.ExecuteNonQuery()

>

> cn.Close()

>

> End Sub

>

> End Class

>

>

|||

It's doing exactly what you told it to do.

Your format string is saying day + month + year (ddMMyyyy) This is not wrong. You're reading it wrong.

If you want it to say month + day + year then write the format string thataway (MMddyyyy)

Adamus

|||

NNTP User wrote:

Before explaining how to fix the date issue, it's important

to point out that your code may be vulnerable to a damaging

SQL injection attack. If the USER ID here has permission

to do more than insert rows into the table HISTORY, and if

you are not validating input in any way on the client side,

you are in trouble. A malicious user of your application

could enter into LongNotes, for example, a single quote

character followed by any SQL statement he or she wants to

run (like DELETE FROM HISTORY, or much worse, if this session

is opened by the sa account). See this article for more

information: www.sommarskog.se/dynamic_sql.html, or search

the web for "SQL injection."

Please, please, please, always use parameterized queries

or SQL stored procedures (and in the latter case, not

ones that just concatenate user input themselves).

You may be validating input on the client side or using

an account with limited privilege, in which case you should

at least add a comment in your code to note that this code

relies on security to be implemented elsewhere. However,

in my opinion, client-side-only security for this sort

of thing is not sufficient. Just to give one example,

some web sites will enforce what is typed into a form,

but overlook the fact that the form may not be the only

place to supply parameter values - direct specification

in the URL might be, too, and those values won't be

validated.

The best way to pass a date is through a parameterized

query or stored procedure, in which case you don't have

to use any format - you just pass it as a datetime value,

such as a SqlDateTime.

If you must pass your date as a string (and it's unlikely you

must, but it's a quicker, if more reckless, fix from what

you have now than to address the security and use parameters),

use the one date-only string format that SQL Server will

interpret consistently across culture settings: 'yyyymmdd'.

Be sure it's the string 'yyyymmdd', and not the integer yyyymmdd.

The only other safe format is the full datetime format

'YYYY-MM-DDTHH:MM:SS.mmm' (including the T character).

Vulnerable code like this is all too common, and I want

to say something whenever I see it.

Steve Kass

Drew University

http://www.stevekass.com

JediDanny@.discussions.microsoft.com wrote:

> This post has been edited either by the author or a moderator in the

> Microsoft Forums: http://forums.microsoft.com

>

> Hey there,

>

> I'm trying to add rows to a table and pass a date value through. I use

> Format(Date.now, "dd MMM yyyy") however when I check in SQL Server

> Managment Studio it has swapped the day and month round (e.g. input of

> "7/9/2006" will be "9/7/2006" on the actual table. Actually the output

> is 2006-07-09 00:00:00:00.) I've put the full source code below.

>

> Can anyone help with this?

>

> Cheers,

>

> Danny

>

>

>

> Imports System.Data

>

> Imports System.Data.SqlClient

>

> Public Class Form1

>

> Dim dtdate As Date

>

> Dim cn As New SqlConnection("server=*********;database=********;USER

> ID=*********;password=*********")

>

> Dim objCommand As New SqlCommand("", cn)

>

> Dim ws As New wrtRefID.Service

>

> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

> System.EventArgs) Handles Button1.Click

>

> dtdate = Now

>

> AddHistory("ActivityID", 1, "AccountID", "AccountName", "ContactName",

> "Category", Microsoft.VisualBasic.Format(dtdate, "dd MMM yyyy"), 2,

> "Description", "UserName", Microsoft.VisualBasic.Format(dtdate, "dd MMM

> yyyy"), "CreateUser", "Notes", "LongNotes")

>

> MessageBox.Show("DONE")

>

> End Sub

>

> Private Sub AddHistory(ByVal ActivityID As String, ByVal Type As

> Integer, ByVal AccountID As String, _

>

> ByVal AccountName As String, ByVal ContactName As String, ByVal Category

> As String, ByVal StartDate As Date, _

>

> ByVal Duration As Integer, ByVal Description As String, ByVal UserName

> As String, _

>

> ByVal CreateDate As Date, ByVal CreateUser As String, ByVal Notes As

> String, ByVal LongNotes As String)

>

> cn.Open()

>

> objCommand.CommandText = "INSERT INTO HISTORY (HISTORYID, ACTIVITYID,

> Type, ACCOUNTID, ACCOUNTNAME, CONTACTNAME, " & _

>

> "CATEGORY, STARTDATE, DURATION, DESCRIPTION, USERNAME, CREATEDATE,

> CREATEUSER, NOTES, LONGNOTES) " & _

>

> "VALUES ('" & ws.MCS2_SLXID("HISTORY") & "', " & "'" & ActivityID & "',

> '" & Type & "', '" & AccountID & "', '" & AccountName & "', '" &

> ContactName & _

>

> "', '" & Category & "', '" & StartDate & "', '" & Duration & "', '" &

> Description & "', '" & UserName & _

>

> "', '" & CreateDate & "', '" & CreateUser & "', '" & Notes & "', '" &

> LongNotes & "')"

>

> ' MessageBox.Show(Format(dtdate, "dd MMM yyyy"))

>

> TextBox1.Text = objCommand.CommandText

>

> objCommand.ExecuteNonQuery()

>

> cn.Close()

>

> End Sub

>

> End Class

>

>

WTF? lol|||

NNTP User wrote:

Before explaining how to fix the date issue, it's important

to point out that your code may be vulnerable to a damaging

SQL injection attack. If the USER ID here has permission

to do more than insert rows into the table HISTORY, and if

you are not validating input in any way on the client side,

you are in trouble. A malicious user of your application

could enter into LongNotes, for example, a single quote

character followed by any SQL statement he or she wants to

run (like DELETE FROM HISTORY, or much worse, if this session

is opened by the sa account). See this article for more

information: www.sommarskog.se/dynamic_sql.html, or search

the web for "SQL injection."

Please, please, please, always use parameterized queries

or SQL stored procedures (and in the latter case, not

ones that just concatenate user input themselves).

You may be validating input on the client side or using

an account with limited privilege, in which case you should

at least add a comment in your code to note that this code

relies on security to be implemented elsewhere. However,

in my opinion, client-side-only security for this sort

of thing is not sufficient. Just to give one example,

some web sites will enforce what is typed into a form,

but overlook the fact that the form may not be the only

place to supply parameter values - direct specification

in the URL might be, too, and those values won't be

validated.

The best way to pass a date is through a parameterized

query or stored procedure, in which case you don't have

to use any format - you just pass it as a datetime value,

such as a SqlDateTime.

If you must pass your date as a string (and it's unlikely you

must, but it's a quicker, if more reckless, fix from what

you have now than to address the security and use parameters),

use the one date-only string format that SQL Server will

interpret consistently across culture settings: 'yyyymmdd'.

Be sure it's the string 'yyyymmdd', and not the integer yyyymmdd.

The only other safe format is the full datetime format

'YYYY-MM-DDTHH:MM:SS.mmm' (including the T character).

Vulnerable code like this is all too common, and I want

to say something whenever I see it.

Steve Kass

Drew University

http://www.stevekass.com

JediDanny@.discussions.microsoft.com wrote:

> This post has been edited either by the author or a moderator in the

> Microsoft Forums: http://forums.microsoft.com

>

> Hey there,

>

> I'm trying to add rows to a table and pass a date value through. I use

> Format(Date.now, "dd MMM yyyy") however when I check in SQL Server

> Managment Studio it has swapped the day and month round (e.g. input of

> "7/9/2006" will be "9/7/2006" on the actual table. Actually the output

> is 2006-07-09 00:00:00:00.) I've put the full source code below.

>

> Can anyone help with this?

>

> Cheers,

>

> Danny

>

>

>

> Imports System.Data

>

> Imports System.Data.SqlClient

>

> Public Class Form1

>

> Dim dtdate As Date

>

> Dim cn As New SqlConnection("server=*********;database=********;USER

> ID=*********;password=*********")

>

> Dim objCommand As New SqlCommand("", cn)

>

> Dim ws As New wrtRefID.Service

>

> Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As

> System.EventArgs) Handles Button1.Click

>

> dtdate = Now

>

> AddHistory("ActivityID", 1, "AccountID", "AccountName", "ContactName",

> "Category", Microsoft.VisualBasic.Format(dtdate, "dd MMM yyyy"), 2,

> "Description", "UserName", Microsoft.VisualBasic.Format(dtdate, "dd MMM

> yyyy"), "CreateUser", "Notes", "LongNotes")

>

> MessageBox.Show("DONE")

>

> End Sub

>

> Private Sub AddHistory(ByVal ActivityID As String, ByVal Type As

> Integer, ByVal AccountID As String, _

>

> ByVal AccountName As String, ByVal ContactName As String, ByVal Category

> As String, ByVal StartDate As Date, _

>

> ByVal Duration As Integer, ByVal Description As String, ByVal UserName

> As String, _

>

> ByVal CreateDate As Date, ByVal CreateUser As String, ByVal Notes As

> String, ByVal LongNotes As String)

>

> cn.Open()

>

> objCommand.CommandText = "INSERT INTO HISTORY (HISTORYID, ACTIVITYID,

> Type, ACCOUNTID, ACCOUNTNAME, CONTACTNAME, " & _

>

> "CATEGORY, STARTDATE, DURATION, DESCRIPTION, USERNAME, CREATEDATE,

> CREATEUSER, NOTES, LONGNOTES) " & _

>

> "VALUES ('" & ws.MCS2_SLXID("HISTORY") & "', " & "'" & ActivityID & "',

> '" & Type & "', '" & AccountID & "', '" & AccountName & "', '" &

> ContactName & _

>

> "', '" & Category & "', '" & StartDate & "', '" & Duration & "', '" &

> Description & "', '" & UserName & _

>

> "', '" & CreateDate & "', '" & CreateUser & "', '" & Notes & "', '" &

> LongNotes & "')"

>

> ' MessageBox.Show(Format(dtdate, "dd MMM yyyy"))

>

> TextBox1.Text = objCommand.CommandText

>

> objCommand.ExecuteNonQuery()

>

> cn.Close()

>

> End Sub

>

> End Class

>

>

Gotta lay off the crackpipe Steve.