473,387 Members | 1,573 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

StringBuilder problem

I found code on the web which takes data from an MS-Access table (my test
table has three rows by five fields) copies it to the Clipboard then into a
range within MS-Excel. Data is stored in a StringBuilder object and should
paste into Excel via Clipboard.SetDataObject. When it comes time to paste
the StringBuilder object is empty. If I strip all database code out and
place the code in another project it works fine. Any ideas? Thanks for your
time, Kevin
Problem code
Private Sub ConvertIt(ByVal aWorksheet As Excel.Worksheet, ByVal aRange As
Excel.Range)
Dim sbExcelData As New System.Text.StringBuilder
Dim Cmd As OleDbCommand
Dim Reader As OleDbDataReader
Dim Con As OleDbConnection

sbExcelData.Length = 2000
Try
Con = New OleDbConnection("Provider=Microsoft.Jet.OleDb.4.0; data
source=" & GetAppPath() & "\test.mdb")
Cmd = New OleDbCommand("SELECT * FROM [tbContacts]", Con)
Con.Open()
Try
Try
Reader = Cmd.ExecuteReader()

While Reader.Read()
'sbExcelData.Append(Reader("ID").ToString)
'sbExcelData.Append(vbTab)
'sbExcelData.Append(Reader("Company Name"))
'sbExcelData.Append(vbTab)
'sbExcelData.Append(Reader("Contact Name"))
'sbExcelData.Append(vbTab)
'sbExcelData.Append(Reader("Address"))
'sbExcelData.Append(vbTab)
'sbExcelData.Append(vbCrLf)

sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append(vbCrLf)
End While
Catch ex As OleDbException
MessageBox.Show("Problem with executing the query" & vbCrLf &
ex.Message & vbCrLf & "Can not continue")
End Try
Finally
Con.Close()
End Try
Catch ex As OleDbException
MessageBox.Show("DB Error")
Exit Sub
End Try
MessageBox.Show("theExcelData=" & sbExcelData.ToString)
Clipboard.SetDataObject(sbExcelData.ToString)

aRange.Select()
aWorksheet.Paste()
End Sub

Stripped out code that holds StringBuilder values
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button2.Click
Dim sbExcelData As New System.Text.StringBuilder
Dim iInsance As Integer
sbExcelData.Length = 2000

For iInsance = 1 To 10
sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append("1")
sbExcelData.Append(vbTab)
sbExcelData.Append(vbCrLf)
Next
Console.WriteLine("theExcelData=" & sbExcelData.ToString)
Clipboard.SetDataObject(sbExcelData.ToString)
End Sub
Downloadable project
http://home.teleport.com/~kevininstructor/dotnet/sb.zip


Jul 21 '05 #1
1 2255
Never mind I see that the statement setting the length for the StringBuilder
Obj was the problem.
Jul 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Mo | last post by:
I am having problem with marshaling struct in C#. //the original C++ struct typedef struct _tagHHP_DECODE_MSG { DWORD dwStructSize; // Size of decode structure. TCHAR ...
3
by: Salvador | last post by:
Hi, I am using an old Win32 DLL that expects a LPBYTE as a parameter. The Stringbuilder appears to be working, the problem is that the component is sending me a string with NULL characters in the...
2
by: Peter | last post by:
Hi, A newbie question .. I want to use an array of length 4 while each array element is a string of 40 chars. I typed .. StringBuilder title = new StringBuilder(40);
5
by: Christof Nordiek | last post by:
Hi, in my Application i have to make a Copy of a StringBuilder, so that, if the new old instance is changed, the you instance will not be affected. I could do something like new...
7
by: Arnd Iffland | last post by:
Hi, i want to use a StringBuilder to create a large string. After instatiation, the MaxCapacity is 2.147.483.647, what is large enough for my purpose. During my procedure i get a...
5
by: =?Utf-8?B?SGFycnkgVg==?= | last post by:
I'm using a StringBuilder to build a SelectCommand string that returns a number of records. If that number is >0, I want to fill an arraylist with fields from the records for a listbox. Is it...
8
by: Egghead | last post by:
Hi all, A very weird problem here. The toString method does not convert the whole StringBuilder elements. I find out it is because there are a series of this " " inside the stringbuilder....
8
by: Soulless | last post by:
I am trying to implement the stringbuilder class by taking a LARGE multiline text box and looping through the lines and build a string and include \r\n in it. I get : Index was outside the...
7
by: rhkodiak | last post by:
I was in an interview question what is the most efficient way to sort a string for example "010101", the result would then be "000111", and write the code on how to do this. I wrote the code for...
4
by: raylopez99 | last post by:
I see that String and StringBuilder in C# / C++ do not have an easy way to set a string to null or zero length, once it is instantiated. Apparently some variant of the .NET languages do (reading...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.