dchau12
asked on
problem inserting data into excel from a sql server .net data set
Hi guys. I am using excel object to insert data from a .net data set into an excel workbook from vb.net. I am getting the following error when I try to insert a column that is over a certain limit:
"The field is too small to accept the amount of data you attempted to add"
How do I get around this? I am creating the excel file on the fly, so is there a way to programatically increase the field size in excel? Here is the code I am using to write to excel. Any help is greatly appreciated.
Ryan
'************************* ********** ********** ********** ********** *******
'Create path for satisfied
Dim ExcelPath As String
ExcelPath = "c:\Comerica Files\Comments\Service Delight - Date From " + Replace(Format(StartDate, "MM/dd/yy"), "/", "-") + " To " + Replace(Format(EndDate, "MM/dd/yy"), "/", "-") + ".xls"
'***************** Create the excel objects and Write column headers ***********************
'Name the file and location
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Applic ation")
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Branch"
oSheet.Range("B1").Value = "BranchName"
oSheet.Range("C1").Value = "TransNum"
oSheet.Range("D1").Value = "Name1"
oSheet.Range("E1").Value = "Name2"
oSheet.Range("F1").Value = "Comment"
oSheet.Range("G1").Value = "Date_1"
oSheet.Range("H1").Value = "Region"
oSheet.Range("I1").Value = "StudyID"
oBook.SaveAs(ExcelPath)
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
'************************* ********** ********** ********** ********
'******************** Populate the new workbook ************
'This data must be pu into memory because the connection is to the excel workbook, not the database
strqry = " SELECT Branch, BranchName, TransNum, Name1, Name2, " & _
" Comment, Date_1, Region, StudyID " & _
" FROM tbl_Comment " & _
" WHERE date_1 between '" & StartDate & "' and '" & EndDate & "' " & _
" and CommentType = 'SATISFIED'"
dsData = Utils.getSurvDataSet(strqr y)
'Establish a connection to the Excel data source.
Dim sConnectionString As String
sConnectionString = "Provider=Microsoft.Jet.OL EDB.4.0;" & _
"Data Source=" & ExcelPath & ";Extended Properties=Excel 8.0;"
Dim objConn As New System.Data.OleDb.OleDbCon nection(sC onnectionS tring)
objConn.Open()
'Add records to the table.
Dim objCmd As New System.Data.OleDb.OleDbCom mand()
objCmd.Connection = objConn
Dim x As Integer = 0
With dsData.Tables(0)
For x = 0 To dsData.Tables(0).Rows.Coun t - 1
objCmd.CommandText = " Insert into [Sheet1$] (Branch, BranchName, TransNum, Name1, " & _
" Name2, Comment, Date_1, Region, StudyID)" & _
" values ('" & Replace(.Rows(x).Item("Bra nch").ToSt ring, "'", "''") & "', " & _
" '" & Replace(.Rows(x).Item("Bra nchName"). ToString, "'", "''") & "', " & _
" '" & Replace(.Rows(x).Item("Tra nsNum").To String, "'", "''") & "', " & _
" '" & Replace(.Rows(x).Item("Nam e1").ToStr ing, "'", "''") & "', " & _
" '" & Replace(.Rows(x).Item("Nam e2").ToStr ing, "'", "''") & "', " & _
" '" & Replace(.Rows(x).Item("Com ment").ToS tring, "'", "''") & "', " & _
" '" & Replace(.Rows(x).Item("Dat e_1").ToSt ring, "'", "''") & "', " & _
" '" & Replace(.Rows(x).Item("Reg ion").ToSt ring, "'", "''") & "', " & _
" '" & Replace(.Rows(x).Item("Stu dyID").ToS tring, "'", "''") & "') "
objCmd.ExecuteNonQuery()
Next
End With
objConn.Close()
'************************* ********** ********** ********** ********** *******
"The field is too small to accept the amount of data you attempted to add"
How do I get around this? I am creating the excel file on the fly, so is there a way to programatically increase the field size in excel? Here is the code I am using to write to excel. Any help is greatly appreciated.
Ryan
'*************************
'Create path for satisfied
Dim ExcelPath As String
ExcelPath = "c:\Comerica Files\Comments\Service Delight - Date From " + Replace(Format(StartDate, "MM/dd/yy"), "/", "-") + " To " + Replace(Format(EndDate, "MM/dd/yy"), "/", "-") + ".xls"
'***************** Create the excel objects and Write column headers ***********************
'Name the file and location
Dim oExcel As Object
Dim oBook As Object
Dim oSheet As Object
'Start a new workbook in Excel.
oExcel = CreateObject("Excel.Applic
oBook = oExcel.Workbooks.Add
oSheet = oBook.Worksheets(1)
oSheet.Range("A1").Value = "Branch"
oSheet.Range("B1").Value = "BranchName"
oSheet.Range("C1").Value = "TransNum"
oSheet.Range("D1").Value = "Name1"
oSheet.Range("E1").Value = "Name2"
oSheet.Range("F1").Value = "Comment"
oSheet.Range("G1").Value = "Date_1"
oSheet.Range("H1").Value = "Region"
oSheet.Range("I1").Value = "StudyID"
oBook.SaveAs(ExcelPath)
oSheet = Nothing
oBook = Nothing
oExcel.Quit()
oExcel = Nothing
GC.Collect()
'*************************
'******************** Populate the new workbook ************
'This data must be pu into memory because the connection is to the excel workbook, not the database
strqry = " SELECT Branch, BranchName, TransNum, Name1, Name2, " & _
" Comment, Date_1, Region, StudyID " & _
" FROM tbl_Comment " & _
" WHERE date_1 between '" & StartDate & "' and '" & EndDate & "' " & _
" and CommentType = 'SATISFIED'"
dsData = Utils.getSurvDataSet(strqr
'Establish a connection to the Excel data source.
Dim sConnectionString As String
sConnectionString = "Provider=Microsoft.Jet.OL
"Data Source=" & ExcelPath & ";Extended Properties=Excel 8.0;"
Dim objConn As New System.Data.OleDb.OleDbCon
objConn.Open()
'Add records to the table.
Dim objCmd As New System.Data.OleDb.OleDbCom
objCmd.Connection = objConn
Dim x As Integer = 0
With dsData.Tables(0)
For x = 0 To dsData.Tables(0).Rows.Coun
objCmd.CommandText = " Insert into [Sheet1$] (Branch, BranchName, TransNum, Name1, " & _
" Name2, Comment, Date_1, Region, StudyID)" & _
" values ('" & Replace(.Rows(x).Item("Bra
" '" & Replace(.Rows(x).Item("Bra
" '" & Replace(.Rows(x).Item("Tra
" '" & Replace(.Rows(x).Item("Nam
" '" & Replace(.Rows(x).Item("Nam
" '" & Replace(.Rows(x).Item("Com
" '" & Replace(.Rows(x).Item("Dat
" '" & Replace(.Rows(x).Item("Reg
" '" & Replace(.Rows(x).Item("Stu
objCmd.ExecuteNonQuery()
Next
End With
objConn.Close()
'*************************
Post the structure of your table tbl_Comment
ASKER
I have debugged it down to the field 'comment'. It is a type VarChar of length 8,000. If the length of the data in the field is less than 50 characters it works fine. Over 50 characters bombs it out. Here is the structure:
2 CommentID int 4 0
0 TransNum int 4 1
0 date_1 datetime 8 1
0 Comment varchar 8000 1
0 CommentType varchar 50 1
0 Branch char 5 1
0 Name1 varchar 100 1
0 Name2 varchar 100 1
0 HomePhone varchar 10 1
0 DayPhone varchar 10 1
0 StudyID int 4 1
0 BranchName varchar 100 1
0 Region varchar 100 1
1 Market varchar 100 1
2 CommentID int 4 0
0 TransNum int 4 1
0 date_1 datetime 8 1
0 Comment varchar 8000 1
0 CommentType varchar 50 1
0 Branch char 5 1
0 Name1 varchar 100 1
0 Name2 varchar 100 1
0 HomePhone varchar 10 1
0 DayPhone varchar 10 1
0 StudyID int 4 1
0 BranchName varchar 100 1
0 Region varchar 100 1
1 Market varchar 100 1
ASKER
At any rate, I have decided to go with DTS packages to get the job done. Thank you so much for looking at this though.
Ryan
Ryan
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.