0

I have been this problem for a week and searching every existing forum for an answer maybe this time that i post my own problem.

My problem was in saving a data in a database. I want to save data into access database about picture and data of customer.

It appears that my problem was in INSERT INTO statement, because there was a msgbox appears that every time i try to save a data from textbox. and lastly, I'm new to vb.net. Please help me,please. my coding as below.

    Dim ms As New IO.MemoryStream
    Me.PictureBox1.Image.Save(ms, Me.PictureBox1.Image.RawFormat)
    Dim arrayImage() As Byte = ms.GetBuffer
    ms.Close()
    fdCon.ConnectionString = strConnectionString

    sql = "INSERT INTO TBL_CUSTOMER([id_card],[cus_fname],[cus_lname],[age],[bd_date],[address],[phone],[e_mail],[date_reg],[MATE],[PIC])VALUES (@txtID,@txtFname,@txtLname,@txtAge,@bdDate,@txtAdd,@txtPhone,@txtEmail,@dmDateIN,@txtMate,@Picture"

    'Set SQL OBJECT 
    objSql = New OleDbCommand(sql, fdCon)
    Try
        fdCon.Open()
        With objSql
            .Parameters.Add(New OleDbParameter("@Picture", SqlDbType.Image)).Value = arrayImage

            .Parameters.AddWithValue("@txtID", txtID.Text)
            .Parameters.AddWithValue("@txtFname", txtFname.Text)
            .Parameters.AddWithValue("@txtLname", txtLname.Text)
            .Parameters.AddWithValue("@txtAge", txtAge.Text)
            .Parameters.AddWithValue("@bdDate", bdDate.Text)
            .Parameters.AddWithValue("@txtAdd", txtAdd.Text)
            .Parameters.AddWithValue("@txtPhone", txtPhone.Text)
            .Parameters.AddWithValue("@txtEmail", txtEmail.Text)
            .Parameters.AddWithValue("@dmDateIN", dmDateIN)
            .Parameters.AddWithValue("@txtMate", txtMate.Text)


        End With


        'Execute DataReader 
        MyDataReader = objSql.ExecuteNonQuery

        'Store Values in String Variables 

        'Close Connection 

    Catch ex As Exception
        'TODO HANDLE EX 
        MessageBox.Show(ex.Message)
    End Try
    'DataGridView1.DataSource = 
    fdCon.Close()

1 Answer 1

1

You don't have the end ) for the values section.

Dim arrayImage() As Byte
Using ms As New IO.MemoryStream
  Me.PictureBox1.Image.Save(ms, Me.PictureBox1.Image.RawFormat)
  arrayImage = ms.ToArray()
End Using

sql = "INSERT INTO TBL_CUSTOMER([id_card],[cus_fname],[cus_lname],[age],[bd_date],[address],[phone],[e_mail],[date_reg],[MATE],[PIC]) VALUES
      (@txtID,@txtFname,@txtLname,@txtAge,@bdDate,@txtAdd,@txtPhone,@txtEmail,@dmDateIN,@txtMate,@Picture)"

Check out this article.

Sign up to request clarification or add additional context in comments.

6 Comments

When I fill the end ) for the values section. I have error "The field is too small to accept the amount of data you attempted to add. Try inserting or pasting less data." how to fix it.
Did you try hovering over the variable to see what it has?
I try to find parameter but not have abnormal. every parameter have to show string.
If the values are adding are the number of fields in that table you can write it Insert Into TBL_CUSTOMERS Values(...). Otherwise make sure your field names are correct.
@user3418046 - it is clear from your first comment that the new error says that there is at least one column that has declared to be of lesser capacity than the data you are trying to insert. So your job is to find that column and either reduce the size of data or increase the column capacity.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.