title: ExecuteNonQuery: Connection property has not been initialized?
body: <p>I get an error in my code:</p>
  
  <p>ExecuteNonQuery: Connection property has not been initialized. </p>
  
  <p>This could be due to the line in this code:</p>
  
  <pre><code>OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')");
  </code></pre>
  
  <p>Full code:</p>
  
  <pre><code>{
      string theUserId = Session["UserID"].ToString();
      {
          OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;");
          cn.Open();
      }
      if (FileUploadControl.HasFile)
      {
          try
          {
              string filename = Path.GetFileName(FileUploadControl.FileName);
              //FileUploadControl.SaveAs(Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename);
              string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/");
              FileUploadControl.SaveAs(Path.Combine(fileuploadpath, filename));
              StatusLabel.Text = "Upload status: File uploaded!";
  
  
  
              OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')");
              cmd.ExecuteNonQuery();
          }
  
          catch (Exception ex)
          {
              StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
  
          }
  
      }
  }
  
          }
  </code></pre>
  
  <p>There is another problem, I dont think its an insert I want as this is going to make a duplicate entry within my database, would it just be a case of changing it from INSERT INTO to UPDATE?</p>
  
  <p>Also is there a way to overwrite upon uploading the image? Atm it just saving the image into the same folder as the one I already have? The first image or any image obviously isnt going to have the same file name so how would I go about overwriting any image in the folder with the one im uploading?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>New error (fileupload works as its stored in the correct area but passing the fileupload to the insert statement is abit wonky)</p>
  
  <p>I get the error </p>
  
  <p>The file could not be uploaded. The following error occured: ERROR [42000] [MySQL][ODBC 3.51 Driver][mysqld-5.5.9]You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''C:\Users\Garrith\Documents\Visual Studio 2010\WebSites\WebSite1\userdata\1\uplo' at line 1 </p>
  
  <p>Which is kind of strange? </p>
  
  <p>All im trying to do is save the filepath+filename in mydb my attempt at passing to the insert has obviously failed. </p>
  
  <pre><code>protected void UploadButton_Click(object sender, EventArgs e)
  {
      if (FileUploadControl.HasFile)
      {
          try
          {
              string theUserId = Session["UserID"].ToString();
              OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;");
              cn.Open();
              string filename = Path.GetFileName(FileUploadControl.FileName);
              //FileUploadControl.SaveAs(Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + filename);
              string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/");
              FileUploadControl.SaveAs(Path.Combine(fileuploadpath, filename));
              StatusLabel.Text = "Upload status: File uploaded!";
              //some kind of function to take the path then enter it into my insert syntax?
              OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn);
              cmd.ExecuteNonQuery();
          }
          catch (Exception ex)
          {
              StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
          }
      }
  }
          }
  </code></pre>
  
  <p>As you can see on this line:</p>
  
  <pre><code>VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn);
  </code></pre>
  
  <p>im missing the "filename" i tryed this:</p>
  
  <pre><code>VALUES ('" + theUserId + "' , '" + fileuploadpath, filename + "')", cn);
  </code></pre>
  
  <p>Cheap shot lol but worth a go I guess and it cryed as it always does!</p>