Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 [Urgent]Insert data to SQL database problem

Author  Topic 

bebex
Starting Member

3 Posts

Posted - 2007-01-02 : 10:56:39
hi all,
i created a register form for users to fill in. I have a problem when i submit new record to my database. It shows "Invalid cast from System.String to System.Byte[]. i don't know how to solve the problem.
i have created a database name User_Profile.It included Username(char),Password(char),Name(varchar),Department(varchar),Workphone(binary),mobilephone(binary),Email(varchar).
Any problem with the data type?
Please help.Thanks.

Private Sub btnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
Dim cm As OleDbCommand = New OleDbCommand
Dim ds As New DataSet
Dim conn As OleDbConnection = New OleDbConnection("Provider=SQLOLEDb;Username=;Password=;Name=;Department=;Workphone=;Mobilephone=;Email=;Trusted_Connection=yes;Initial Catalog=InstantMessenger;Data Source=(local)")

cm.Connection = conn
cm.CommandText = "INSERT INTO User_Profile(Username,Password,Name,Department,Workphone,Mobilephone,Email)VALUES(?,?,?,?,?,?,?)"
cm.CommandType = CommandType.Text
conn.Open()

cm.Parameters.Add(New OleDbParameter)
cm.Parameters.Item(0).Direction = ParameterDirection.Input
cm.Parameters.Item(0).DbType = DbType.String
cm.Parameters.Item(0).Size = 10
cm.Parameters.Item(0).Value = txtUsername.Text

cm.Parameters.Add(New OleDbParameter)
cm.Parameters.Item(1).Direction = ParameterDirection.Input
cm.Parameters.Item(1).DbType = DbType.String
cm.Parameters.Item(1).Size = 10
cm.Parameters.Item(1).Value = txtPassword.Text

cm.Parameters.Add(New OleDbParameter)
cm.Parameters.Item(2).Direction = ParameterDirection.Input
cm.Parameters.Item(2).DbType = DbType.String
cm.Parameters.Item(2).Size = 50
cm.Parameters.Item(2).Value = txtName.Text

cm.Parameters.Add(New OleDbParameter)
cm.Parameters.Item(3).Direction = ParameterDirection.Input
cm.Parameters.Item(3).DbType = DbType.String
cm.Parameters.Item(3).Size = 50
cm.Parameters.Item(3).Value = txtDepartment.Text

cm.Parameters.Add(New OleDbParameter)
cm.Parameters.Item(4).Direction = ParameterDirection.Input
cm.Parameters.Item(4).DbType = DbType.Binary
cm.Parameters.Item(4).Size = 10
cm.Parameters.Item(4).Value = txtWorkphone.Text

cm.Parameters.Add(New OleDbParameter)
cm.Parameters.Item(5).Direction = ParameterDirection.Input
cm.Parameters.Item(5).DbType = DbType.Binary
cm.Parameters.Item(5).Size = 10
cm.Parameters.Item(5).Value = txtMobilePhone.Text

cm.Parameters.Add(New OleDbParameter)
cm.Parameters.Item(6).Direction = ParameterDirection.Input
cm.Parameters.Item(6).DbType = DbType.String
cm.Parameters.Item(6).Size = 50
cm.Parameters.Item(6).Value = txtEmail.Text

Try
cm.ExecuteNonQuery()
MessageBox.Show("Data Recorded Successfully")
Catch ex As Exception
MessageBox.Show(ex.Message)

End Try

conn.Close()

End Sub

nr
SQLTeam MVY

12543 Posts

Posted - 2007-01-02 : 11:00:00
I suspect it is the binaries that are causing the problem.
Why are you holding phone numbers as binary?


==========================================
Cursors are useful if you don't know sql.
DTS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

graz
Chief SQLTeam Crack Dealer

4149 Posts

Posted - 2007-01-02 : 11:00:13
You defined your two phone numbers as binary and then in your code you are assigning a String value to them. I'd consider making your phone number VARCHAR.

I'm also pretty sure you don't need this: "Username=;Password=;Name=;Department=;Workphone=;Mobilephone=;Email=;" in your connection string.

===============================================
Creating tomorrow's legacy systems today.
One crisis at a time.
Go to Top of Page

bebex
Starting Member

3 Posts

Posted - 2007-01-02 : 11:41:49
thanks for reply.
I have solve my problem.
i try to preview the data that i have inserted via Data Adapter Preview. May i know how to delete the data that i have inserted before?

I would like to ask about the code for login ( check username and password in database when try log in). Can guide me?

Thanks.
Go to Top of Page
   

- Advertisement -