TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Aktham Mahmoud
NA
720
38.1k
Implement Upload file control with DetailsView
Sep 16 2019 10:53 AM
Good times All
I've an application to mange Chocolate shope,
at admin side there is many pages to mange a site, my problem with a form to insert data in data base with uploaded image by File upload control implemented in details view
you can see a front end code here:
<
asp:DetailsView
ID
=
"DetailsView1"
runat
=
"server"
AutoGenerateRows
=
"False"
DataSourceID
=
"SDS_Subcategory"
DefaultMode
=
"Insert"
Height
=
"50px"
Width
=
"125px"
OnItemInserting
=
"DetailsView1_ItemInserting"
>
<
Fields
>
<
asp:TemplateField
HeaderText
=
"Main Category:"
>
<
InsertItemTemplate
>
<
asp:DropDownList
ID
=
"DropDownList1"
runat
=
"server"
AutoPostBack
=
"True"
DataSourceID
=
"SqlDataSource1"
DataTextField
=
"C_Name"
DataValueField
=
"C_Id"
OnSelectedIndexChanged
=
"DropDownList1_SelectedIndexChanged"
>
</
asp:DropDownList
>
<
asp:Literal
ID
=
"Lit_cat"
runat
=
"server"
Text
=
'<%#Bind("C_Id") %>'
>
</
asp:Literal
>
<
asp:SqlDataSource
ID
=
"SqlDataSource1"
runat
=
"server"
ConnectionString
=
"<%$ ConnectionStrings:ConnectionString %>"
SelectCommand
=
"SELECT [C_Id], [C_Name] FROM [Categories]"
>
</
asp:SqlDataSource
>
<
asp:Label
ID
=
"Label1"
runat
=
"server"
>
</
asp:Label
>
</
InsertItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Name:"
>
<
InsertItemTemplate
>
<
asp:TextBox
ID
=
"TXT_Name"
runat
=
"server"
Text
=
'<%# Bind("Name") %>'
>
</
asp:TextBox
>
</
InsertItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Image"
>
<
InsertItemTemplate
>
<
asp:FileUpload
ID
=
"FU_Image"
runat
=
"server"
Enabled
=
'<%# Bind("Image") %>'
/>
</
InsertItemTemplate
>
</
asp:TemplateField
>
<
asp:TemplateField
HeaderText
=
"Ingredients"
>
<
InsertItemTemplate
>
<
asp:TextBox
ID
=
"TXT_Description"
runat
=
"server"
Text
=
'<%# Bind("Ingredients") %>'
TextMode
=
"MultiLine"
>
</
asp:TextBox
>
</
InsertItemTemplate
>
</
asp:TemplateField
>
<
asp:CommandField
ShowInsertButton
=
"True"
/>
<
asp:TemplateField
>
<
InsertItemTemplate
>
<
asp:Label
ID
=
"LBMSG"
runat
=
"server"
>
</
asp:Label
>
</
InsertItemTemplate
>
</
asp:TemplateField
>
</
Fields
>
</
asp:DetailsView
>
</
div
>
and a code behind here:
using
System;
using
System.Collections.Generic;
using
System.Linq;
using
System.Web;
using
System.Web.UI;
using
System.Web.UI.WebControls;
using
System.Data;
using
System.Data.SqlClient;
using
System.Configuration;
using
System.IO;
namespace
WebApplication1.admin
{
public
partial
class
sub_category : System.Web.UI.Page
{
protected
void
Page_Load(
object
sender, EventArgs e)
{
Label lbmessage = (Label)DetailsView1.FindControl(
"LBMSG"
);
}
protected
void
DropDownList1_SelectedIndexChanged(
object
sender, EventArgs e)
{
string
function = ((DropDownList)DetailsView1.FindControl(
"DropDownList1"
)).SelectedValue.ToString();
Literal lit_txt = (Literal)DetailsView1.FindControl(
"lit_cat"
);
lit_txt.Text = function;
Label lbl_txt = (Label)DetailsView1.FindControl(
"Label1"
);
}
protected
void
DetailsView1_ItemInserting(
object
sender, DetailsViewInsertEventArgs e)
{
FileUpload fu = (FileUpload)DetailsView1.FindControl(
"FU_Image"
);
TextBox txtimg= (TextBox)DetailsView1.FindControl(
"TXT_Name"
);
Literal lit_txt = (Literal)DetailsView1.FindControl(
"lit_cat"
);
TextBox txtdesc =(TextBox)DetailsView1.FindControl(
"TXT_Description"
);
if
(fu.PostedFile !=
null
)
{
string
imgfile = Path.GetFileName(fu.PostedFile.FileName);
//fu.SaveAs("../images/categories" + imgfile);
fu.SaveAs(Server.MapPath(
"../images/categories"
) + imgfile);
string
mainconnection = ConfigurationManager.ConnectionStrings[
"ConnectionString"
].ConnectionString;
SqlConnection sqlcon =
new
SqlConnection(mainconnection);
sqlcon.Open();
string
sqlquery =
"insert into [dbo].[Sub_categories]([Name],[Image],[Ingredients],[C_Id]) values (@Name,@Image,@Ingredients,@C_Id)"
;
SqlCommand sqlcom =
new
SqlCommand(sqlquery, sqlcon);
sqlcom.Parameters.AddWithValue(
"@Name"
, txtimg);
sqlcom.Parameters.AddWithValue(
"@Image"
,
"../images/categories"
+ imgfile);
sqlcom.Parameters.AddWithValue(
"@Ingredients"
, txtdesc);
sqlcom.Parameters.AddWithValue(
"@C_Id"
, lit_txt);
sqlcom.ExecuteNonQuery();
Label lbmessage = (Label)DetailsView1.FindControl(
"LBMSG"
);
lbmessage.Text =
"Image and record has beed saved Succesfully"
;
sqlcon.Close();
}
else
{
Label lbmessage = (Label)DetailsView1.FindControl(
"LBMSG"
);
lbmessage.Text =
"Image and record not saved Succesfully"
;
}
}
}
}
so there is a way to solve a prolem
Reply
Answers (
1
)
Upload Multiple Attachments From Different FileUploaders C#
How to use routing in Asp.Net Core Web API