Click here to Skip to main content
15,920,704 members
Home / Discussions / Visual Basic
   

Visual Basic

 
AnswerRe: string tokenization Pin
TwoFaced20-Jun-07 12:16
TwoFaced20-Jun-07 12:16 
GeneralRe: string tokenization Pin
hsprasain20-Jun-07 12:56
hsprasain20-Jun-07 12:56 
AnswerRe: string tokenization Pin
TwoFaced20-Jun-07 12:25
TwoFaced20-Jun-07 12:25 
QuestionMedia control Pin
stijn.janssen20-Jun-07 8:47
stijn.janssen20-Jun-07 8:47 
AnswerRe: Media control Pin
Dave Kreskowiak21-Jun-07 4:19
mveDave Kreskowiak21-Jun-07 4:19 
Questionprogress bar monitor files downloaded from a server Pin
steve_rm20-Jun-07 7:03
steve_rm20-Jun-07 7:03 
AnswerRe: progress bar monitor files downloaded from a server Pin
svanwass20-Jun-07 8:12
svanwass20-Jun-07 8:12 
QuestionWriting to SQL server w/ VB.Net Pin
svanwass20-Jun-07 4:19
svanwass20-Jun-07 4:19 
First the setup
My Code:
<br />
Imports System.Data.SqlClient<br />
Imports System.Text<br />
Public Class Form1<br />
<br />
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
<br />
        Dim myConnection As New SqlConnection()<br />
        Dim myDataSet As New DataSet()<br />
        Dim myDataAdapter As SqlDataAdapter<br />
        Dim myUpdateCommand As SqlCommand<br />
        Dim myNewStr As String<br />
<br />
        'create connection string<br />
        myConnection.ConnectionString ="Server=.;Database=TestDB;UID=steve;pwd=password;Trusted_Connection=yes;"<br />
<br />
        'open connection to DB<br />
        myConnection.Open()<br />
<br />
        'setup data adapter <br />
        myDataAdapter = New SqlDataAdapter("select TEXT from SCRIPTS WHERE ID=1", myConnection)<br />
<br />
        'Initialize the SqlCommand object that will be used as the DataAdapter's UpdateCommand.<br />
        'Note that the WHERE clause uses only the CustId field to locate the record that is to be updated.<br />
        myUpdateCommand = New SqlCommand("UPDATE SCRIPTS SET TEXT=@pNewText FROM SCRIPTS WHERE ID =@pMyID", myDataAdapter.SelectCommand.Connection)<br />
<br />
        'Create and append the parameters for the Update command.<br />
        myUpdateCommand.Parameters.Add(New SqlParameter("@pNewText", SqlDbType.VarChar))<br />
        myUpdateCommand.Parameters("@pNewText").SourceVersion = DataRowVersion.Current<br />
        myUpdateCommand.Parameters("@pNewText").SourceColumn = "TEXT"<br />
<br />
        myUpdateCommand.Parameters.Add(New SqlParameter("@pMyID", SqlDbType.Int))<br />
        myUpdateCommand.Parameters("@pMyID").SourceVersion = DataRowVersion.Original<br />
        myUpdateCommand.Parameters("@pMyID").SourceColumn = "ID"<br />
<br />
<br />
        'Assign the SqlCommand to the UpdateCommand property of the SqlDataAdapter.<br />
        myDataAdapter.UpdateCommand = myUpdateCommand<br />
<br />
        'call the fill method to populate our dataset<br />
        myDataAdapter.Fill(myDataSet, "SCRIPTS")<br />
<br />
        'present the current entry in database<br />
        MsgBox("Script before updating" + vbCrLf + Encoding.Unicode.GetString(myDataSet.Tables("SCRIPTS").Rows(0)("TEXT")))<br />
<br />
        'modify current with replace to update field<br />
        myNewStr = Replace(Encoding.Unicode.GetString(myDataSet.Tables("SCRIPTS").Rows(0)("TEXT")), "hello", "steve")<br />
<br />
        'now convert our replaced field to a byte array<br />
        Dim dBytes As Byte()<br />
        dBytes = StrToByteArray(myNewStr)<br />
<br />
        'set the dataset table row to our converted byte array<br />
        myDataSet.Tables("SCRIPTS").Rows(0)("TEXT") = dBytes<br />
<br />
        'call the update method to write back to DB<br />
        myDataAdapter.Update(myDataSet, "SCRIPTS")<br />
<br />
        'close connection<br />
        myConnection.Close()<br />
<br />
    End Sub<br />
<br />
    Public Shared Function StrToByteArray(ByVal str As String) As Byte()<br />
<br />
        'converts string to byte array<br />
        Dim encoding As New System.Text.ASCIIEncoding()<br />
        Return encoding.GetBytes(str)<br />
<br />
    End Function<br />
<br />
End Class<br />


DB SETUP
-----------
Column Name Data Type
ID int (primary key)
TEXT image

DB Records:
ID=1

TEXT =
1040101010801080111033013010010301110111010009801210101033013010083011609701140116033013010010201050110011501050104033013010010401010108010801110330
(
when converted to string is:
hello!
goodbye!
Start!
finsih!
hello!
)

My code works perfect up until the point of the update method

myDataAdapter.Update(myDataSet, "TP_SCRIPTS")

Once I hit this line, i get an error saying :Failed to convert parameter value from a Byte[] to a String.

Help please!

-steve
AnswerRe: Writing to SQL server w/ VB.Net Pin
Dave Kreskowiak21-Jun-07 4:16
mveDave Kreskowiak21-Jun-07 4:16 
QuestionHow do I Empty a ComboBox after set DataSource Pin
Kerry Drake20-Jun-07 3:16
Kerry Drake20-Jun-07 3:16 
AnswerRe: How do I Empty a ComboBox after set DataSource Pin
ctwalker20-Jun-07 4:01
ctwalker20-Jun-07 4:01 
GeneralRe: How do I Empty a ComboBox after set DataSource Pin
Kerry Drake20-Jun-07 6:01
Kerry Drake20-Jun-07 6:01 
QuestionUserName asn Password in Smtp. how? Pin
alexburzak20-Jun-07 2:53
alexburzak20-Jun-07 2:53 
AnswerRe: UserName asn Password in Smtp. how? Pin
Sanjay Kunjam20-Jun-07 3:01
Sanjay Kunjam20-Jun-07 3:01 
GeneralRe: UserName asn Password in Smtp. how? Pin
alexburzak20-Jun-07 3:16
alexburzak20-Jun-07 3:16 
QuestionHow to insert Null Values Pin
Vimalsoft(Pty) Ltd20-Jun-07 1:34
professionalVimalsoft(Pty) Ltd20-Jun-07 1:34 
AnswerRe: How to insert Null Values Pin
Christian Graus20-Jun-07 11:56
protectorChristian Graus20-Jun-07 11:56 
GeneralRe: How to insert Null Values Pin
Vimalsoft(Pty) Ltd20-Jun-07 19:35
professionalVimalsoft(Pty) Ltd20-Jun-07 19:35 
GeneralRe: How to insert Null Values Pin
Dave Kreskowiak21-Jun-07 4:12
mveDave Kreskowiak21-Jun-07 4:12 
GeneralRe: How to insert Null Values Pin
Vimalsoft(Pty) Ltd21-Jun-07 22:40
professionalVimalsoft(Pty) Ltd21-Jun-07 22:40 
GeneralRe: How to insert Null Values Pin
Dave Kreskowiak22-Jun-07 16:41
mveDave Kreskowiak22-Jun-07 16:41 
Questionintptr Pin
Taylor Kobani20-Jun-07 0:16
Taylor Kobani20-Jun-07 0:16 
AnswerRe: intptr Pin
Dave Kreskowiak21-Jun-07 4:11
mveDave Kreskowiak21-Jun-07 4:11 
GeneralRe: intptr [modified] Pin
Taylor Kobani22-Jun-07 7:58
Taylor Kobani22-Jun-07 7:58 
QuestionAdd -in problems Pin
Katarzyna_20-Jun-07 0:06
Katarzyna_20-Jun-07 0:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.