I get an error in my code:

ExecuteNonQuery: Connection property has not been initialized. 

This could be due to the line in this code:

```
OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures (UserID, picturepath) VALUES ('" + theUserId + "' , '" + fileuploadpath + "')");

```

Full 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;

        }

    }
}

        }

```

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?

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?

**EDIT:**

New error (fileupload works as its stored in the correct area but passing the fileupload to the insert statement is abit wonky)

I get the error 

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 

Which is kind of strange? 

All im trying to do is save the filepath+filename in mydb my attempt at passing to the insert has obviously failed. 

```
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;
        }
    }
}
        }

```

As you can see on this line:

```
VALUES ('" + theUserId + "' , '" + fileuploadpath + "')", cn);

```

im missing the "filename" i tryed this:

```
VALUES ('" + theUserId + "' , '" + fileuploadpath, filename + "')", cn);

```

Cheap shot lol but worth a go I guess and it cryed as it always does!