davidcahan
asked on
Public Declaration of SQLBulkCopy and csvDataReader
LearnedOne
I have an interesting situation. I have a sub routine that loops through all the dropdownlists on my page and then adds properties to both my sqlbulkcopy object and my csvDataReader object. Or at least that is what i would like it to do. Here's some of my code:
Public Sub ProcessMappings(ByVal _c As Control)
For Each c In _c.Controls
If (TypeOf c Is DropDownList) Then
ddl = CType(c, DropDownList)
If InStr(ddl.ID, "ddlMap") > "0" Then
If ddl.SelectedValue <> "" Then
bulkCopy.ColumnMappings.Ad d(Mid(ddl. ID, InStr(ddl.ID, "p") + 1, Len(Len(ddl.ID) - InStr(ddl.ID, "p"))), ddl.SelectedValue)
End If
csvData.Columns.Add("varch ar")
End If
End If
If c.HasControls() Then
ProcessMappings(c)
End If
Next
End Sub
On submit i want to do the following:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim Delimeter As String
If (Request.QueryString("Data Source") = "CSV") Then
Delimeter = ","
ElseIf (Request.QueryString("Data Source") = "TAB") Then
Delimeter = Chr(9)
Else
Delimeter = Chr(9)
End If
Dim filepath As String = Server.MapPath(".") & "\LeadImports\" & Request.QueryString("FileN ame")
Dim objConn As SqlConnection = New SqlConnection(System.Confi guration.C onfigurati onManager. AppSetting s("strConn 1"))
Dim csvData As New CsvDataReader(filepath, Delimeter, Encoding.Default)
Dim bulkCopy As New SqlBulkCopy(objConn)
If Request.QueryString("HasHe aders") = "True" Then
csvData.Settings.HasHeader s = True
End If
csvData.Settings.Delimiter = Request.QueryString("DataS ource")
objConn.Open()
bulkCopy.DestinationTableN ame = "IndividualImportStaging"
bulkCopy.BulkCopyTimeout = 1000000000
ProcessMappings(Me)
'' call WriteToServer which starts import
bulkCopy.WriteToServer(csv Data)
objConn.Close()
End Sub
The problem is when i try to declare my sqlbulkcopy and csvDataReader instances as public variables at the top of the page: 1) I can't access the connection property for either, 2) they seem to choke on the few properties i am able to access.
Any solutions? I know this isn't hard I'm just stuck for exactly how .NET wants this.
I have an interesting situation. I have a sub routine that loops through all the dropdownlists on my page and then adds properties to both my sqlbulkcopy object and my csvDataReader object. Or at least that is what i would like it to do. Here's some of my code:
Public Sub ProcessMappings(ByVal _c As Control)
For Each c In _c.Controls
If (TypeOf c Is DropDownList) Then
ddl = CType(c, DropDownList)
If InStr(ddl.ID, "ddlMap") > "0" Then
If ddl.SelectedValue <> "" Then
bulkCopy.ColumnMappings.Ad
End If
csvData.Columns.Add("varch
End If
End If
If c.HasControls() Then
ProcessMappings(c)
End If
Next
End Sub
On submit i want to do the following:
Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim Delimeter As String
If (Request.QueryString("Data
Delimeter = ","
ElseIf (Request.QueryString("Data
Delimeter = Chr(9)
Else
Delimeter = Chr(9)
End If
Dim filepath As String = Server.MapPath(".") & "\LeadImports\" & Request.QueryString("FileN
Dim objConn As SqlConnection = New SqlConnection(System.Confi
Dim csvData As New CsvDataReader(filepath, Delimeter, Encoding.Default)
Dim bulkCopy As New SqlBulkCopy(objConn)
If Request.QueryString("HasHe
csvData.Settings.HasHeader
End If
csvData.Settings.Delimiter
objConn.Open()
bulkCopy.DestinationTableN
bulkCopy.BulkCopyTimeout = 1000000000
ProcessMappings(Me)
'' call WriteToServer which starts import
bulkCopy.WriteToServer(csv
objConn.Close()
End Sub
The problem is when i try to declare my sqlbulkcopy and csvDataReader instances as public variables at the top of the page: 1) I can't access the connection property for either, 2) they seem to choke on the few properties i am able to access.
Any solutions? I know this isn't hard I'm just stuck for exactly how .NET wants this.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER