Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.
Private Sub CreateDualEnrollmentFile()
' =========================================================
' Create file
' =========================================================
Dim Myconn As SqlConnection
Dim Mycomm As SqlCommand
Dim dtr As SqlDataReader
Dim dadapter As SqlDataAdapter
Dim ds As DataSet
Dim drow As DataRow
Dim vStamp As DateTime = Now()
Dim vStampForm = vStamp.ToString.Format("{0:MM-dd-yy_HH_mm_ss_tt}", DateTime.Now)
Dim sql
sql = "SELECT * FROM NSU_Dual_Enrollment_Application WHERE ([Stamp] > '" & gLastDate & "') ORDER BY Stamp ;"
Dim vFileBody
Myconn = New SqlConnection(ConfigurationSettings.AppSettings("gDataSource"))
Mycomm = New SqlCommand(sql, Myconn)
dadapter = New SqlDataAdapter
dadapter.SelectCommand = Mycomm
ds = New DataSet
dadapter.Fill(ds, "NSU_Dual_Enrollment_Application")
vFileBody = vFileBody & "FirstName" & "," & "MiddleName" & "," & "LastName" & "," & "SSN" & "," & "DOB" & "," & "Street" & "," & "City" & "," & "State" & ","
vFileBody = vFileBody & "Zip" & "," & "Phone" & "," & "CellPhone" & "," & "Email" & ","
vFileBody = vFileBody & "HighSchool" & "," & "HSGradDate" & "," & "PrevCollege" & ","
vFileBody = vFileBody & "IntendedMajor" & "," & "Comments" & "," & "Stamp" & "," & vbCrLf
If ds.Tables("NSU_Dual_Enrollment_Application").Rows.Count = 0 Then
lblTextFile.Text = "No new applications were found."
Else
For Each drow In ds.Tables("NSU_Dual_Enrollment_Application").Rows
vFileBody = vFileBody & Chr(34) & drow("FirstName") & Chr(34) & "," & Chr(34) & drow("MiddleName") & Chr(34) & "," & Chr(34) & drow("lastName") & Chr(34) & ","
vFileBody = vFileBody & drow("SSN") & "," & drow("DOB") & "," & Chr(34) & drow("Street") & Chr(34) & "," & Chr(34) & drow("City") & Chr(34) & "," & Chr(34) & drow("State") & Chr(34) & "," & Chr(34) & drow("Zip") & Chr(34) & ","
vFileBody = vFileBody & drow("Phone") & "," & drow("CellPhone") & "," & drow("Email") & "," & Chr(34) & drow("HighSchool") & Chr(34) & ","
vFileBody = vFileBody & Chr(34) & drow("HSGradDate") & Chr(34) & "," & Chr(34) & drow("PrevCollege") & Chr(34) & "," & Chr(34) & drow("IntendedMajor") & Chr(34) & "," & Chr(34) & drow("Comments") & Chr(34) & ","
vFileBody = vFileBody & drow("Stamp") & "," & vbCrLf
Next
Myconn.Close()
'Create and save info in a text file.
Dim vFileName = "EnrollmentData/DualEnrollmentReport_" & vStampForm & ".csv"
'& Now() & "_" & "Scholarship_Report.txt"
Try
Dim FILENAME As String = Server.MapPath(vFileName)
Dim objStreamWriter As StreamWriter
objStreamWriter = File.CreateText(FILENAME)
objStreamWriter.WriteLine(vFileBody)
objStreamWriter.Close()
lblTextFile.Visible = True
lblTextFile.Text = "File written and located <a href='" & vFileName & "'TARGET=_BLANK>here</a>."
UpdateAdmissionsLastDateRan()
btnText.Visible = False
Catch Ex As Exception
lblTextFile.Visible = True
lblTextFile.Text = "An error occurred attempting to produce the text file." & Ex.Message & sql
End Try
End If
End Sub
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
Have a better answer? Share it in a comment.
The problem is on the client machine. It sounds as if Excel is the default application for CSV files and is therefore getting involved in the save operation.
I don't think there is anything you can do in your application to affect this behaviour.
The only solution I can see (without requiring changes on the client machines) is to use a .TXT extension for your file to take Excel out of the equation.