Click here to Skip to main content
12,077,753 members (49,174 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: VB VB.NET
Hello my friends

I have this query and i want to store an image to sql server database

Dim cmd As New SqlCommand()
cmd.CommandText = "INSERT INTO Staff(Lastname,Firstname,Fathername,Birthday,City,Phone,Photo)" & _
"VALUES (@Lastname,@Firstname,@Fathername,@Birthday,@City,@Phone,,@Photo)"
With cmd.Parameters
.Add(New SqlParameter("@Lastname", txtLname.Text))
.Add(New SqlParameter("@Firstname", txtFname.Text))
.Add(New SqlParameter("@Fathername", txtFtname.Text))
.Add(New SqlParameter("@Birthday", txtBdate.Text))
.Add(New SqlParameter("@City", txtCity.Text))
.Add(New SqlParameter("@Phone", txtPhone.Text))
.Add(New SqlParameter("@Photo", What in this?))

Can anyone know how?

Thanks in advance
Posted 1-Feb-13 22:31pm
jomachi1.6K

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

The error is here

VALUES (@Lastname,@Firstname,@Fathername,@Birthday,@City,@Phone,,@Photo)
//use as below remove the extra comma

VALUES (@Lastname,@Firstname,@Fathername,@Birthday,@City,@Phone,@Photo)

And about that parameter

Add(New SqlParameter("@Photo", What in this?))
it will probably be a byte array you would create from image file you need to store

Best of luck!!
  Permalink  
Comments
jomachi 2-Feb-13 3:37am
   
What can i put in photo parameter?
Mantu Singh 2-Feb-13 3:50am
   
You need to create a byte array as I hope in the database you column datatype will be varbinary or image where you need the bytes of image

byte [] img=File.ReadAllBytes("image name");

Add(New SqlParameter("@Photo",img));

Remove semicolon as it wont work in VB
jomachi 2-Feb-13 4:04am
   
Thanks my friend
Mantu Singh 2-Feb-13 4:14am
   
Pls be kind to accept the answer too ! welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web04 | 2.8.160212.1 | Last Updated 2 Feb 2013
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100