1

I am trying to insert in database Ms Access 2007 . First i get all the file name from folder then copy that file name in database .Here is my Database screenshot.

This is my database screen shot

This is my code

       string some = "Nothing";
        Response.Write(v);
        string[] filePaths = Directory.GetFiles(Server.MapPath("~/Gallery/GalleryImage/" + v));
        int a =0;
        OleDbCommand cmd = new OleDbCommand();
        OleDbConnection mycon = new OleDbConnection();
        mycon.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\AGENTJ.AGENTJ-PC\Documents\Visual Studio 2010\WebSites\mfaridalam\App_Data\mfaridalam1.accdb";

        cmd = mycon.CreateCommand();
        mycon.Open();
        foreach (string item in filePaths)
        {
            a++;
            string filename = Path.GetFileName(item);
            string ips = 00 + a.ToString();



          cmd.CommandText = "INSERT INTO [Image] ([Image],[Sort],[Created],[Albumid],[Description],[title])VALUES(" + filename + "," + ips + "," + some + "," +
           v + "," + some + "," + some + ");";


          int temp = cmd.ExecuteNonQuery();
          if (temp > 0)
          {
              Response.Write("Writing is complete, Success!");

          }
          else
          {
              Response.Write("Application Error, Try Again!");
          }


          Response.Write(filename+ "<br/>");

                  }
        mycon.Close();
        cmd.Dispose();
        mycon.Dispose();

I am Getting this error

       No value given for one or more required parameters.

In line Line 42: int temp = cmd.ExecuteNonQuery();

2
  • As I can see you have 7 fields to insert and passing only 6 values. Commented Jul 22, 2013 at 19:30
  • No Its 6 Field and 6 values "INSERT INTO [Image] ([Image],[Sort],[Created],[Albumid],[Description],[title])VALUES(" + filename + "," + ips + "," + some + "," + v + "," + some + "," + some + ");"; I got solution Thank you anyway i was not using single quotes '' thanks anyway Commented Jul 22, 2013 at 19:36

1 Answer 1

3

If you had used a parameterized query this error would never be seen. The problem is in your string concatenation that lacks of quotes around the string passed for the values in every text/memo field present in your table.

A parameterized query could require more typing but is more readable and will avoid error in parsing values for strings, dates, decimals etc.... (and that big problem called Sql Injection )

cmd.CommandText = "INSERT INTO [Image] ([Image],[Sort],[Created],[Albumid]," + 
                  "[Description],[title])VALUES(?,?,?,?,?,?)";

cmd.Parameters.AddWithValue("@p1",filename);
cmd.Parameters.AddWithValue("@p2",ips);
cmd.Parameters.AddWithValue("@p3",some);
cmd.Parameters.AddWithValue("@p4",v);
cmd.Parameters.AddWithValue("@p5",some);
cmd.Parameters.AddWithValue("@p6",some);
int temp = cmd.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you i got it right query is this "INSERT INTO [Image] ([Image],[Sort],[Created],[Albumid],[Description],[title])VALUES('" + filename + "','" + ips + "','" + some + "','" + v + "','" + some + "','" + some + "');";
Yes it works until you have some value containing a single quote, then it crash with a syntax error. (For example Description="This is the best album from Guns N' Roses") And this is only a programming problem. You should also consider security concers like SQL Injection. No, really, do not use string concatenation, use always a parameterized query.
Steve thanks your recommendation .. Can i contact you like on facebook or email so i can get some useful tips from you
Sorry @JayZee but I am very busy at the moment and I am sure that if you post other questions here you will get answers in no time. See you again on SO.

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.