473,379 Members | 1,185 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,379 software developers and data experts.

Fetching & Inserting Data into a column of TEXT Data type in SQL server 2000 Using ASP.NET

Hello genious people,

I m trying to insert a LARGE text from Multiline Textbox into my
table
of sqlserver2000. I m using vs-2005.
Please note that I dont want to store blob data From FILE TO TABLE,
like storing IMAGE into DB.
I hav searched lots of articles on that but didn't get success.
I hav tried following code from somewhere i found.
But I m getting Error that
"Failed to convert parameter value from a Byte[] to a String." (This
Error is displayed by smart tag of VS-2005) And then when I press
F5 ,
my IE shows following Error.
/
************************************************** **************************
*****************

*************
Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object must implement
IConvertible.
Source Error:
Line 102: Try
Line 103: myConnection.Open()
Line 104: myCommand.ExecuteNonQuery() <---- Here Line is
highlighted by IE
Line 105: myConnection.Close()
Line 106: Response.Write("New TestText successfully
added!")
************************************************** **************************
*****************

*************/
Please tell me wht is wrong with my following code. & if any wrong
then pls.
Also pls tell me how to fetch all that data once at a time. Or
required ro use loop like . But dont know syntax. pls help me its
urgent for me.
Public Sub StoreTextInDB()
Dim intTextSize As Int64
'''''''Dim TextStream As Stream
'''''' Gets the Size of the Text
intTextSize = Len(Trim(txtDesc.Text))
Dim TextContent(intTextSize) As Byte
'************************************************* ********
' '''''Reads the Image
TextContent =
System.Text.Encoding.Unicode.GetBytes(Trim(txtDesc .Text))
'' ''TextStream = Request.InputStream
'' ''Dim intStatus As Integer
'' ''intStatus = TextStream.Read(TextContent, 0, intTextSize)
''
''************************************************ *********
'' '' Create Instance of Connection and Command Object
Dim szCon As String
szCon = "Data Source=localhost;uid=t;pwd=t;Initial
Catalog=myDb"
Dim myConnection As New SqlConnection(szCon)
Dim myCommand As New
SqlCommand("myStoredProcedure", myConnection)
'''' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure
Dim prmStudNo As New SqlParameter("@StudNo", SqlDbType.Int,
4)
prmStudNo.Value = 1
myCommand.Parameters.Add(prmStudNo)
'''''''' Add Parameters to SPROC
Dim prmTestText As New SqlParameter("@TestText",
SqlDbType.Text, 16)
prmTestText.Value = TextContent
myCommand.Parameters.Add(prmTestText)
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Response.Write("New TestText successfully added!")
Catch SQLexc As SqlException
Response.Write("Insert Failed. Error Details are: " &
SQLexc.ToString())
End Try
End Sub

Jul 16 '07 #1
5 2956
I hav also seen arcticle from Microsoft

http://support.microsoft.com/kb/308042/EN-US/

but not much helpful

Jul 16 '07 #2
TextContent is a byte array but you are trying to use it as a string.
declare it as a string, and remove the convert code.
-- bruce (sqlwork.com)

Bhavesh wrote:
Hello genious people,

I m trying to insert a LARGE text from Multiline Textbox into my
table
of sqlserver2000. I m using vs-2005.
Please note that I dont want to store blob data From FILE TO TABLE,
like storing IMAGE into DB.
I hav searched lots of articles on that but didn't get success.
I hav tried following code from somewhere i found.
But I m getting Error that
"Failed to convert parameter value from a Byte[] to a String." (This
Error is displayed by smart tag of VS-2005) And then when I press
F5 ,
my IE shows following Error.
/
************************************************** **************************
*****************

*************
Object must implement IConvertible.
Description: An unhandled exception occurred during the execution of
the current web request. Please review the stack trace for more
information about the error and where it originated in the code.
Exception Details: System.InvalidCastException: Object must implement
IConvertible.
Source Error:
Line 102: Try
Line 103: myConnection.Open()
Line 104: myCommand.ExecuteNonQuery() <---- Here Line is
highlighted by IE
Line 105: myConnection.Close()
Line 106: Response.Write("New TestText successfully
added!")
************************************************** **************************
*****************

*************/
Please tell me wht is wrong with my following code. & if any wrong
then pls.
Also pls tell me how to fetch all that data once at a time. Or
required ro use loop like . But dont know syntax. pls help me its
urgent for me.
Public Sub StoreTextInDB()
Dim intTextSize As Int64
'''''''Dim TextStream As Stream
'''''' Gets the Size of the Text
intTextSize = Len(Trim(txtDesc.Text))
Dim TextContent(intTextSize) As Byte
'************************************************* ********
' '''''Reads the Image
TextContent =
System.Text.Encoding.Unicode.GetBytes(Trim(txtDesc .Text))
'' ''TextStream = Request.InputStream
'' ''Dim intStatus As Integer
'' ''intStatus = TextStream.Read(TextContent, 0, intTextSize)
''
''************************************************ *********
'' '' Create Instance of Connection and Command Object
Dim szCon As String
szCon = "Data Source=localhost;uid=t;pwd=t;Initial
Catalog=myDb"
Dim myConnection As New SqlConnection(szCon)
Dim myCommand As New
SqlCommand("myStoredProcedure", myConnection)
'''' Mark the Command as a SPROC
myCommand.CommandType = CommandType.StoredProcedure
Dim prmStudNo As New SqlParameter("@StudNo", SqlDbType.Int,
4)
prmStudNo.Value = 1
myCommand.Parameters.Add(prmStudNo)
'''''''' Add Parameters to SPROC
Dim prmTestText As New SqlParameter("@TestText",
SqlDbType.Text, 16)
prmTestText.Value = TextContent
myCommand.Parameters.Add(prmTestText)
Try
myConnection.Open()
myCommand.ExecuteNonQuery()
myConnection.Close()
Response.Write("New TestText successfully added!")
Catch SQLexc As SqlException
Response.Write("Insert Failed. Error Details are: " &
SQLexc.ToString())
End Try
End Sub
Jul 16 '07 #3
Hi Bruce,

Thanks for Reply.

You are right, I had already achieved that functionality after posting
my que. But didnt get success in Fetching.
So if anybody can give sample code for fetching Text datatype with
more than 8000 chars & then showing it to textbox, then it would be
great for me.

Thanks

Jul 17 '07 #4
Hi Bruce,

Thanks For Reply.

U were right, Needed to pass string , but also need to pass size of
Data( instead of 16, passed actual length of data). So that worked for
me & didn't get any error.

But now problem in fetching, not able to fetch data from table
correctly. Here is my code. I hav following problems with this coding
I hav applied through my logic.

[1] able to fetch more than 8000 chars, but it gives more data than my
actual data, i dont know how some extra char hav been appended to my
actual data which fetching.

[2] and for fetching data with less that 8000 chars, it give me error,
& i know that its because of "SizeParam.Value = 4000" statement. And
also getting LengthOutParam.Value = 0 (ZERO) .

So is it like that DataLength will work for data with more that 8000
chars.?

And my data may be of less that 8000 & may be of more than 8000
chars.

ple reply asap

Thanks

Public Function StoreBLOBIntoFile()
Dim szBlobColumnName As String = "TestText"
Dim szTableName As String = "TempTable"
Dim szConstraint As String = " WHERE STUDNo = 3"

Dim msg As String = "Blob data not stored successfully in
File !"
Dim sqlQuery As String
Try
Dim szCon As String
szCon = "Data Source=localhost;uid=t;pwd=t;Initial Catalog=myDb"

sqlQuery = "Select @Pointer=TEXTPTR(" & szBlobColumnName &
"), @Length=DataLength(" & szBlobColumnName & ") from " & szTableName
& " " & szConstraint
Dim imageCol As Integer = 0 ' position of image column in
DataReader
Dim cn As New SqlConnection(szCon)
'
' Make sure that Photo is non-NULL and return TEXTPTR to
it.
'
Dim cmdGetPointer As New SqlCommand(sqlQuery, cn)
Dim PointerOutParam As SqlParameter =
cmdGetPointer.Parameters.Add("@Pointer", SqlDbType.VarBinary, 100)
PointerOutParam.Direction = ParameterDirection.Output
Dim LengthOutParam As SqlParameter =
cmdGetPointer.Parameters.Add("@Length", SqlDbType.BigInt)
LengthOutParam.Direction = ParameterDirection.Output
cn.Open()
cmdGetPointer.ExecuteNonQuery()
If PointerOutParam.Value Is DBNull.Value Then
cn.Close()
Exit Try
End If
'
' Set up READTEXT command, parameters, and open
BinaryReader.
'
Dim cmdReadBinary As New SqlCommand("READTEXT " &
szTableName & "." & szBlobColumnName & " @Pointer @Offset @Size
HOLDLOCK", cn)
Dim PointerParam As SqlParameter =
cmdReadBinary.Parameters.Add("@Pointer", SqlDbType.Binary, 16)
Dim OffsetParam As SqlParameter =
cmdReadBinary.Parameters.Add("@Offset", SqlDbType.Int)
Dim SizeParam As SqlParameter =
cmdReadBinary.Parameters.Add("@Size", SqlDbType.Int)
Dim dr As SqlDataReader

'Dim fs As New System.IO.FileStream(DestFilePath,
IO.FileMode.OpenOrCreate, IO.FileAccess.Write)

Dim Offset As Integer = 0
OffsetParam.Value = Offset
Dim Buffer(LengthOutParam.Value - 1) As Byte
'
' Read buffer full of data and write to the file stream.
'
PointerParam.Value = PointerOutParam.Value
Do
' Calculate buffer size - may be less than
BUFFER_LENGTH for the last block.
'
If (Offset + SizeParam.Value) >= LengthOutParam.Value
Then
SizeParam.Value = LengthOutParam.Value - Offset
Else
SizeParam.Value = 4000
End If
dr =
cmdReadBinary.ExecuteReader(CommandBehavior.Single Result)
dr.Read()
dr.GetBytes(imageCol, 0, Buffer, 0, SizeParam.Value)
dr.Close()

txtDesc.Text = txtDesc.Text &
ConvertByteArrayToString(Buffer)

Offset += SizeParam.Value
OffsetParam.Value = Offset
Loop Until Offset >= LengthOutParam.Value

cn.Close()
msg = "Blob data stored successfully in File !"
Catch ex As Exception
msg = ex.Message
End Try
StoreBLOBIntoFile = msg
End Function

Jul 17 '07 #5
hi all frienrds,

I found solution for fetching Text column of any length (with its max
limit 2^31 -1).

Here is code . If this one will be useful to anyone then it would be
great for me.

Cheers

Public Function FetchBLOBDataFromDB(ByVal szBlobColumnName As String,
ByVal szTableName As String, ByVal szConstraint As String) As String
Dim TextCol As Integer = 0 ' the column # of the BLOB field
Dim cn As New SqlConnection()
cn = Dal.getConnection
Dim cmd As New SqlCommand("SELECT " & szBlobColumnName & "
FROM " & szTableName & " " & szConstraint, cn)
Dim dr As SqlDataReader = cmd.ExecuteReader()
dr.Read()
Dim Buffer(dr.GetBytes(TextCol, 0, Nothing, 0,
Integer.MaxValue) - 1) As Byte
dr.GetBytes(TextCol, 0, Buffer, 0, Buffer.Length)
dr.Close()
cn.Close()
Return ConvertByteArrayToString(Buffer)
End Function

Public Function ConvertByteArrayToString(ByVal byteArray As Byte()) As
String
Dim enc As Encoding = Encoding.Default
Dim strText As String = enc.GetString(byteArray)
Return strText
End Function

Jul 18 '07 #6

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

Similar topics

3
by: Dan Sikorsky | last post by:
How can I get the recordset attributes for a table field in SQL Server 2000 to report the field updatable attribute correctly ... mine keeps saying the fields are not updatable? That is, (...
3
by: Tyler Hudson | last post by:
/*Code below raises following errors: Server: Msg 245, Level 16, State 1, Line 6 Syntax error converting the varchar value 'a' to a column of data type int. */ create table #x (i integer, c...
5
by: Iain Porter | last post by:
Hi, I'm trying to store large strings to a database, so am using the text field type (LongText). I have used this before when storing the html of a webpage, and was able to store more than 255...
2
by: Sunny | last post by:
I am using an asp page to access data stored by Microsoft SQL in a SQL server database. I cannot get all values to return, some display as blanks. I am using IIS v5, Microsoft SQL Server 2000,...
6
by: fniles | last post by:
I am using VB.NET 2003 and SQL Server 2000. I have a table with a datetime column type. When inserting into the table for that column, I set it to Date.Now.ToString("T") , which is something like...
0
by: gunimpi | last post by:
http://www.vbforums.com/showthread.php?p=2745431#post2745431 ******************************************************** VB6 OR VBA & Webbrowser DOM Tiny $50 Mini Project Programmer help wanted...
0
by: Bhavesh | last post by:
Hello genious people, I m trying to insert a LARGE text from Multiline Textbox into my table of sqlserver2000. I m using vs-2005. Please note that I dont want to store blob data From FILE TO...
1
by: Bhavesh | last post by:
Hi Bruce, Thanks For Reply. U were right, Needed to pass string , but also need to pass size of Data( instead of 16, passed actual length of data). So that worked for me & didn't get any...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: 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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.