Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

insert into Db AND fileupload to folder

Status
Not open for further replies.

ciberperson

Programmer
Aug 31, 2000
29
CA
Has anyone successfully built an aspx form that will upload a file to the folder structure (asp:fileupload) and at the same insert form fields (like a friendly title of the photo to be uploaded) into a DB? I am not having any success trying to make this work. Any help out there?
 
1. get the filestream from the file upload control
2. save the stream to a file
on error:
stop and notify user
3. get values from the form for the database
on error:
delete file
stop and notify user
4. saves values to database
on error:
delete file
stop and notify user
5. display success message


Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the quick response jmeckley. Actually I am using .NET2.0 so I can use the fileUpload control which eliminates the stream to a file. I am having success uploading the file to a folder. I can also insert my form field to a DB. I just haven't been able to combine the two - I suppose because of the fileupload control.
Here's what I have so far:

The form page:
<form id="form1" runat="server">
<input id="estabid" type="hidden" value='xxxx'/>
<b>Title</b>
<br />
<asp:TextBox ID="title" runat="server">Enter Title Here</asp:TextBox>
<br />
<b>Image</b>
<br />
<asp:FileUpload ID="FileUpload1" runat="server" />
<p>
<asp:Button ID="Button1" runat="server" Text="Upload" OnClick="Button1_Click" />

<p>
<asp:Label ID="Label1" runat="server"></asp:Label>
</p>
</form>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
InsertCommand="INSERT INTO dbo.upload_images(title) VALUES (@title)"
SelectCommand="SELECT [id], [estabid], [title] FROM [upload_images] ORDER BY [estabid]">
<InsertParameters>
<asp:ControlParameter ControlID="title" Name="title" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>

and the code behind:

<script runat="server">
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
try {
FileUpload1.SaveAs("C:\\uploader\\images\\" +
FileUpload1.FileName);
Label1.Text = "File name: " +
FileUpload1.PostedFile.FileName + "<br>" +
FileUpload1.PostedFile.ContentLength + " kb<br>" +
"Content type: " +
FileUpload1.PostedFile.ContentType;

//SqlDataSource1.Insert(); THIS Doesn't work
}
catch (Exception ex) {
Label1.Text = "ERROR: " + ex.Message.ToString();
}
else
{
Label1.Text = "You have not specified a file.";
}
}
protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
{
if (e.Exception != null)
{
Response.Write(e.Exception.Message);
Response.End();
}

}

</script>
 
Remove the datasource control. You can't debug them. Write the code yourself, this way you can debug any problems. I belive that the datasource control is the issue here.
 
I am having success uploading the file to a folder. I can also insert my form field to a DB. I just haven't been able to combine the two
this doesn't make any sense. there is no magic all-in-one control to upload a file and save data to a database. they are independent actions. they can happen together, in a unit of work, by coding it yourself.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
OK I got it. Thanks for pointing me in the right direction. I removed the SQLDatasource control and created the code myself. I was then able to control both the file upload and the DB insert.

BTW - I wasn't looking for a "magic all-in-one control", I was just looking for help. Thanks jbenson001 for providing that pointer for me.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top