Friday, March 30, 2012

Saving uploaded picture filepath to sql database

I am trying to save an uploaded picture filepath to a sql database record. The record that the filepath will be saved to will be the record determined by the logged in userid. I get an error stating Object not set to an instance of an object. This is my code for when the upload button is hit

ProtectedSub uploadbutton_Click(ByVal senderAsObject,ByVal eAs System.EventArgs)Handles uploadbutton.Click

If UploadTest.HasFile =FalseThen

' No file uploaded!

UploadDetails.Text ="Please first select a file to upload..."

Else

' Display the uploaded file's details

UploadDetails.Text =String.Format( _

"Uploaded file: {0}<br />" & _

"File size (in bytes): {1:N0}<br />" & _"Content-type: {2}", _

UploadTest.FileName, _

UploadTest.FileBytes.Length, _

UploadTest.PostedFile.ContentType)

' Save the file

Dim filePathAsString = Server.MapPath("~/ProfilePictures/" & UploadTest.FileName)

UploadTest.SaveAs(filePath)

Dim datasources1AsNew SqlDataSourcedatasources1.ConnectionString = ConfigurationManager.ConnectionStrings("aspnetdb_connection").ToString()

datasources1.UpdateCommandType = SqlDataSourceCommandType.Text

Dim myUserIDAsString = Membership.GetUser().ProviderUserKey.ToString

datasources1.UpdateParameters("userid").DefaultValue = myUserID

datasources1.UpdateCommand ="UPDATE into profile_details (pic) VALUES (@.filepath) WHERE ([UserId] = @.UserId)"

EndIf

EndSub

Any ideas on what i need to do to fix this?

i save image path into my database too

1: i take the file name and i create a dirrectory with the name of loged in user

string fileName = FileUpload1.FileName.ToString(); //i don't know if is FileName() or FileName.ToString()...

string userName = Profile.UserName;

string uploadFolderPath ="~/upload/"; // the folder where you save uploads

string filePath =HttpContext.Current.Server.MapPath(uploadFolderPath);

Directory.CreateDirectory(filePath +"\\" + userName);

2: i save the picture

FileUpload1.SaveAs("F:\\Documents and Settings\\Administrator\\Desktop\\WebSite2\\upload\\" + userName +"\\" + fileName);

3: i save the filepath into a hiddenField

HiddenField1.Value = userName +"\\" + fileName; // would be like "user1\picture.jpg"

4: i insert the HiddenField1 Value into my database

this is what i did and i am pleased...hope i helped you

|||

The problem I am having is the actual code to imput the string into the database. It seems if i use the hiddenfield way your talking about their has to be another step to actually input it.

|||

i don't understand very clear what you mean, but i use a stored procedure to insert the filepath into database like this one

CREATE PROCEDURE InsertPicture@.Image1varchar(200)ASINSERT INTO Pictures (Image1)VALUES (@.Image1)

you can use an update procedure instead of iseart if you want to change the filepath for a specific picture with PictureID = x

and in the code behind you will have something like this

SqlConnection conn =new SqlConnection(ConnectionString);// wite your conn string in bracketsSqlCommand command =new SqlCommand("InsertPicture", conn);command.CommandType = CommandType.StoredProcedure;SqlParameter Image1_param =new SqlParameter();Image1_param.ParameterName ="@.Image1";Image1_param.SqlDbType = SqlDbType.VarChar;Image1_param.Value = HiddenField1.Value.ToString();command.Parameters.Add(Image1_param);conn.Open();SqlDataReader reader = command.ExecuteNonQuery();reader.Close();command.Connection.Close();conn.Close();
|||

in your example what column in the database are you saving the imagefile path to? Image1 right?

|||

yes, i have many columns like PictureID, Name, Description and so on, but in Image1 i store the filepath

No comments:

Post a Comment