Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Button2_Click(object sender, EventArgs e)
    {

        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=PROMOD-PC;Initial Catalog=travel_Directions;Integrated Security=True";

        String Strt_Address = TextBox1.Text;
        String End_Address = TextBox2.Text;

        String filePath = FileUpload1.PostedFile.FileName;
        String filename = Path.GetFileName(filePath);
        String ExtStr = Path.GetExtension(filename);
        String contenttype = String.Empty;

        switch (ExtStr)
        { 
            case ".png":
                contenttype = "image/png";
                break;

            case ".jpg":
                contenttype = "image/jpg";
                break;

            case ".gif":
                contenttype = "image/gif";
                break;
        }

        if (contenttype != string.Empty)
        {
        Stream Strmf = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(Strmf);
            Byte[] imgbytes = br.ReadBytes((Int32)Strmf.Length);
            
            conn.Open();

            String QueryStr = "UPDATE MapDataImage SET Image = @Image WHERE Source='" + TextBox1.Text + "';";
            SqlCommand scmd = new SqlCommand(QueryStr, conn);
            scmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;
            scmd.ExecuteNonQuery();


        }



guys it points to

scmd.ExecuteNonQuery(); and says

Incorrect syntax near 'Image'.


....................................................

i have values on database...
and my sql database consist..

Source Varchar(max)
Age int
Image Varbinary(max)

so there, i store values manually

eg = name john , age 20

i want to check weather user input john or not to the textbox...

if textbox value equals to john ... then only i can add images to Image Field..
// i need to compare the values between textbox and database.
.....

Think example as Facebook... i can only add my pics if i'm login with my account only know.

so kind of that...

C#
String QueryStr = "UPDATE MapDataImage SET Image = @Image WHERE Source='" + TextBox1.Text + "';";


how about this?
Posted
Updated 3-Jul-13 22:41pm
v8

Look at your query:

C#
String QueryStr = "INSERT INTO MapDataImage VALUES Image WHERE @Source='" + TextBox1.Text + "';";
SqlCommand scmd = new SqlCommand(QueryStr);
scmd.Parameters.Add("@Image", SqlDbType.VarBinary).Value = imgbytes;
scmd.ExecuteNonQuery();
You are specifying the variable @Source but only providing a value for the variable @ImageProbably, what you wanted to say was:

C#
String QueryStr = "INSERT INTO MapDataImage VALUES (@Source, @Image)";
SqlCommand scmd = new SqlCommand(QueryStr);
scmd.Parameters.AddWithValue("@Image", imgbytes);
scmd.Parameters.AddWithValue("@Source", TextBox1.Text);
scmd.ExecuteNonQuery();
But it is a good idea to also provide the field names in your query to ensure they go in the right place...
SQL
INSERT INTO myTable (MyColumn1, MyColumn2) VALUES (@MC1, @MC2)
 
Share this answer
 
Comments
Thanks7872 4-Jul-13 4:03am    
↑voted.I just forgot to point out @source and @image issue in my above answer.updating shortly.Suggested to have a look at this one.Great.
OriginalGriff 4-Jul-13 4:15am    
I saw your answer, which was why I didn't mention the connection - you had that covered!
promod madushan 4-Jul-13 4:07am    
thanks for the comment mate....

but i want to check if textbox value match with particular field and then only i can add image to the image field.

eg - if my name is promod and if sql database hold myname only i can upload my photo.

that's my logic...

if i'm wrong correct me....

thanks mate!!!!
promod madushan 4-Jul-13 4:11am    
String QueryStr = "INSERT INTO MapDataImage VALUES @Image WHERE @Source='" + TextBox1.Text + "';";

now is it okay?
OriginalGriff 4-Jul-13 4:15am    
No - INSERT queries do not have a WHERE clause, because INSERT *always* creates a new row.
Are you trying to change an existing image on a specific row? If not, what are you trying to do?
Edit this line
C#
.....................
 Stream Strmf = FileUpload1.PostedFile.InputStream;
 BinaryReader br = new BinaryReader(Strmf);
 Byte[] imgbytes = br.ReadBytes((Int32)Strmf.Length);            
 conn.Open(); 
 String QueryStr = "INSERT INTO MapDataImage VALUES Image WHERE @Source='" + TextBox1.Text + "';";
 SqlCommand scmd = new SqlCommand(QueryStr,your connection);//Connection required

Regards..:laugh:
 
Share this answer
 
v2
Comments
promod madushan 4-Jul-13 3:59am    
thanks mate!!!
promod madushan 4-Jul-13 4:01am    
guys it points to

scmd.ExecuteNonQuery(); and says

Incorrect syntax near 'Image'.
Thanks7872 4-Jul-13 4:02am    
Refer to below answer by OriginalGriff.I forgot to mention it.

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