Avatar of Larry Brister
Larry BristerFlag for United States of America asked on

ASP.NET Import VB Code

I have a script that is importing data from a csv file fine...with the exception of some (user) typos in a date column.

The column is supposed to be mm/dd/yyyy format.

I'm calling a SQL Server stored procedure ...how do I strip / replace any invalid characters?  Or at the very least, give a message that points to the error row and what is invalid?

partial code is attached...
blah...blah...blah...code
 
Dim row As DataRow
  For Each row In dt.Rows
   Dim checkin As String = ""
   blah...
 
   checkin = row(0).ToString
   blah...
 
 Dim objConn1 As SqlConnection = New SqlConnection System.Configuration.ConfigurationManager.AppSettings("strConnS"))
 Dim oCom1 As SqlCommand = New SqlCommand
 oCom1.Connection = objConn1
 oCom1.CommandText = "impdata"
 oCom1.CommandType = CommandType.StoredProcedure
 oCom1.Parameters.Add(New SqlParameter("@checkin", SqlDbType.DateTime)).Value = checkin
    blah...
 objConn1.Open()
 oCom1.ExecuteNonQuery()
 objConn1.Close()
 objConn1.Dispose()
 lblMessage.Text = "Successfully Uploaded File: " & file.GetNameWithoutExtension() & file.GetExtension()
 specialType.SelectedValue = "0"
 
Next

Open in new window

.NET ProgrammingEditors IDEsMicrosoft Development

Avatar of undefined
Last Comment
Murtie

8/22/2022 - Mon
Dirk Haest

You could trap the right exception this way:

try
   oCom1.ExecuteNonQuery()
catch ex as exception

end try
ASKER
Larry Brister

Dhaest:
  And how do I output that in a message, textbox, label..etc.
ASKER CERTIFIED SOLUTION
Dirk Haest

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Murtie


You could use Try Catch to log errors.

Or try to convert the string into a dateTime data type and see if it goes through. otherwise it would throw an error.

I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck