Facing issue while cmd.ExecuteNonwuery() RRS feed

  • Question

  • User-836358016 posted

    i want to insert image into access db using asp.net c# fileupload dialog box.  but i am facing issue in command  cmd.executenonquery. below is my code



    if (!FileUpload1.HasFile)
    Label1.Visible = true;
    Label1.Text = "Please Select Image File";

    int length = FileUpload1.PostedFile.ContentLength;
    byte[] pic = new byte[length];

    FileUpload1.PostedFile.InputStream.Read(pic, 0, length);

    string str = FileUpload1.FileName;
    FileUpload1.PostedFile.SaveAs(Server.MapPath("~/uploads/" + str));

    string Image = "~/Uploads/" + str.ToString();

    OleDbCommand cmd = new OleDbCommand("insert into ImageToTable values(@id,@Image,@name)", con);
    cmd.Parameters.AddWithValue("@id", TextBox1.Text);
    cmd.Parameters.AddWithValue("@Image", Image);
    cmd.Parameters.AddWithValue("@name", TextBox2.Text);

    cmd.ExecuteNonQuery();                <----------------------------------------error occurs here
    Label1.Visible = true;
    Label1.Text = "Image Uploaded Sucessfully";

    Wednesday, June 20, 2018 8:36 AM

All replies

  • User753101303 posted


    Always tell explicitely which error you have. When  reading code you often find multiple possible causes such as the connection not being opened yet or not having the correct number of columns in your insert or trying to populate an auto generated id or using a value which is too wide and more likely it will be still some other error etc....

    So it's IMHO it's much better to just always start from the actual error you SEE so that you can then focus on what matters in your code for this error.

    Wednesday, June 20, 2018 9:01 AM
  • User632428103 posted

    Hello all,

    @prasadjoshi => looks at the excellent article from mike dot net :)


    Wednesday, June 20, 2018 12:32 PM
  • User-1716253493 posted

    Possible problems :

    • characters to long, increase fields size
    • insert same id into key field, maybe you should use UPDATE instead
    Wednesday, June 20, 2018 12:42 PM