Click here to Skip to main content
15,918,889 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a software to upload images?

in my application there's a picture box, and when someone click browse button. the picture will show in the picturebox.

so i'm passing the picturebox directly to sql image.


C#
string connetionString = null;

            //string sql = null;
            string name = "";
            string pass = "";

            connetionString = "Data Source=PD-JANAKAN;Initial Catalog=VisaDb;User ID=Madushan;Password=P@19861030";


            using (SqlConnection conn = new SqlConnection(connetionString))
            {

                conn.Open();

                String Usr = ReferClass.Uname;


                string selectStmt = "select UserId from VisaUserLog where UserName = @Usr";

                using (SqlCommand cmd2 = new SqlCommand(selectStmt, conn))
                {
                    cmd2.Parameters.AddWithValue("@Usr", UserName.Text);
                    SqlDataReader myReader = cmd2.ExecuteReader();
                    while (myReader.Read())
                    {
                        //int ID = myReader.GetInt32(0);
                        int UsrId = (int)myReader["UserId"];
                        ReferClass.UserId = UsrId;
                    }
                    myReader.Close();
                    ((IDisposable)myReader).Dispose();
                }

                string cmdInsert = "insert into Visa_UsrDataBase(User_Id, Visa_Country, Cust_Name, Pass_No, Exp_Date, Collect_Date, Assign_by, Collectd_Prson, Visa_Type, Uplad_Doc, Uplad_Image) value (@Uid,@Vcountry,@Cusname,@PassNo,@ExpDate,@CollectDate,@Assigned,@CollectPerson,@VisaType,@Upladpic,@UpladDoc)";

                using (SqlCommand cmd = new SqlCommand(cmdInsert, conn))
                {
                    cmd.Parameters.AddWithValue("@Uid", ReferClass.UserId);
                    cmd.Parameters.AddWithValue("@Vcountry", VisaCountry.SelectedText);
                    cmd.Parameters.AddWithValue("@Cusname", CustName.Text);
                    cmd.Parameters.AddWithValue("@PassNo", PassNo.Text);
                    cmd.Parameters.AddWithValue("@ExpDate", SqlDbType.Date).Value = expData.Value;
                    cmd.Parameters.AddWithValue("@CollectDate", SqlDbType.Date).Value = colData.Value;
                    cmd.Parameters.AddWithValue("@Assigned", AssignedPerson.Text);
                    cmd.Parameters.AddWithValue("@CollectPerson", ColPerson.Text);
                    cmd.Parameters.AddWithValue("@VisaType", VisaPerson.Text);
                    cmd.Parameters.AddWithValue("@Upladpic", System.Data.SqlDbType.Image).Value = this.pictureBox1;
                    //cmd.Parameters.AddWithValue("@UpladDoc", System.Data.SqlDbType.Image).Value = this.m_barrImg;

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("Success");
                }


                conn.Close();



it gives me an error saying.

No mapping exists from object type System.Windows.Forms.PictureBox to a known managed provider native type.

i put path as well.
that gives me an error saying Incorrect syntax near 'value'.

and i did like this way as well.

C#
byte[] Picture;

                Picture = File.ReadAllBytes(ReferClass.PicturePath);


conn.Open();

string cmdInsert = "insert into Visa_UsrDataBase(User_Id, Visa_Country, Cust_Name, Pass_No, Exp_Date, Collect_Date, Assign_by, Collectd_Prson, Visa_Type, Uplad_Doc, Uplad_Image) value (@Uid,@Vcountry,@Cusname,@PassNo,@ExpDate,@CollectDate,@Assigned,@CollectPerson,@VisaType,@Upladpic,@UpladDoc)";

                using (SqlCommand cmd = new SqlCommand(cmdInsert, conn))
                {
                    cmd.Parameters.AddWithValue("@Uid", ReferClass.UserId);
                    cmd.Parameters.AddWithValue("@Vcountry", VisaCountry.SelectedText);
                    cmd.Parameters.AddWithValue("@Cusname", CustName.Text);
                    cmd.Parameters.AddWithValue("@PassNo", PassNo.Text);
                    cmd.Parameters.AddWithValue("@ExpDate", SqlDbType.Date).Value = expData.Value;
                    cmd.Parameters.AddWithValue("@CollectDate", SqlDbType.Date).Value = colData.Value;
                    cmd.Parameters.AddWithValue("@Assigned", AssignedPerson.Text);
                    cmd.Parameters.AddWithValue("@CollectPerson", ColPerson.Text);
                    cmd.Parameters.AddWithValue("@VisaType", VisaPerson.Text);
                    cmd.Parameters.AddWithValue("@Upladpic", System.Data.SqlDbType.Image).Value = Picture;
                    //cmd.Parameters.AddWithValue("@UpladDoc", System.Data.SqlDbType.Image).Value = this.m_barrImg;

                    cmd.ExecuteNonQuery();

                    MessageBox.Show("Success");
                }


                conn.Close();


it gives me an error saying,

Incorrect syntax near 'value'.

please help me to solve this.
Posted
Updated 16-Feb-15 22:08pm
v3
Comments
Kuthuparakkal 17-Feb-15 23:12pm    
private void cmdSave_Click(object sender, EventArgs e)
{
try
{
//Read Image Bytes into a byte array
byte[] imageData = ReadFile(txtImagePath.Text);

//Initialize SQL Server Connection
SqlConnection CN = new SqlConnection(txtConnectionString.Text);

//Set insert query
string qry = "insert into ImagesStore (OriginalPath,ImageData) _
values(@OriginalPath, @ImageData)";

//Initialize SqlCommand object for insert.
SqlCommand SqlCom = new SqlCommand(qry, CN);

//We are passing Original Image Path and
//Image byte data as sql parameters.
SqlCom.Parameters.Add(new SqlParameter("@OriginalPath",
(object)txtImagePath.Text));

SqlCom.Parameters.Add(new SqlParameter("@ImageData",
(object)imageData));

//Open connection and execute insert query.
CN.Open();
SqlCom.ExecuteNonQuery();
CN.Close();

//Close form and return to list or images.
this.Close();
}
Hemas Ilearn 18-Feb-15 2:46am    
but is there anyway to readfile? because it gives me an error saying not (readfile)implemented a method?

byte[] imageData = ReadFile(txtImagePath.Text);

Hello ,
Refer: Link
thanks
 
Share this answer
 
Convert image into base64 or save the image at any server folder and save the path to database.
 
Share this answer
 
Comments
Hemas Ilearn 17-Feb-15 1:06am    
i can convert base 64... if database content is in binary format.
but it is in image format..
User-8621695 17-Feb-15 4:16am    
ohh does you mean that data type in database is image?
Hemas Ilearn 17-Feb-15 5:04am    
yesss...
User-8621695 17-Feb-15 5:16am    
use this below link, Hope it will help:
http://www.codeproject.com/Articles/10861/Storing-and-Retrieving-Images-from-SQL-Server-usin

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900