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
Guest User
Tech Writer
271
36.2k
edit data in gridview asp.net entity framework
Aug 25 2020 4:50 AM
hello..
i am trying to edit the data in gridview....
my issues are:
redirect to same page and also i am geting a id which record i want to edit. but i want in fileupload which i already save image to be selected and also in dropdown the selected category shown when i redirect...
<form id=
"form1"
runat=
"server"
>
<div>
<table
class
=
"style1"
>
<tr>
<td>
<asp:FileUpload ID=
"fileUpload1"
runat=
"server"
/>
</td>
</tr>
<tr>
<td>Category:</td>
<td>
<asp:DropDownList runat=
"server"
ID=
"ddlWallCategry"
OnSelectedIndexChanged=
"ddlWallCategry_SelectedIndexChanged"
AutoPostBack=
"true"
>
</asp:DropDownList></td>
</tr>
</table>
</div>
<asp:Button ID=
"btnSave"
runat=
"server"
Text=
"Save"
OnClick=
"btnSave_Click"
CssClass=
"btsave"
/>
<asp:Button ID=
"btnupdate"
runat=
"server"
Text=
"Update"
OnClick=
"btnupdate_Click"
CssClass=
"btsave"
/>
<asp:GridView ID=
"GridViewCategory"
runat=
"server"
AutoGenerateColumns=
"False"
DataKeyNames=
"Id"
AllowPaging=
"True"
OnPageIndexChanging=
"GridView1_PageIndexChanging"
OnRowEditing=
"GridViewCategory_RowEditing"
OnRowUpdating=
"GridViewCategory_RowUpdating"
OnRowDeleting =
"GridViewCategory_RowDeleting"
>
<Columns>
<asp:TemplateField HeaderText=
"CategoryName"
ItemStyle-Width=
"150"
>
<ItemTemplate>
<asp:Label ID=
"lblCategoryName"
runat=
"server"
Text=
'<%# Eval("CategoryName") %>'
></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID=
"TxtCategoryName"
runat=
"server"
Text=
'<%# Eval("CategoryName") %>'
></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:ImageField DataImageUrlField=
"Thumbnail"
HeaderText=
"Thumbnail"
>
<ControlStyle Height=
"100px"
Width=
"100px"
/>
</asp:ImageField>
<asp:TemplateField HeaderText=
"Action"
>
<ItemTemplate>
<asp:ImageButton ID=
"imgbtnEdit"
runat=
"server"
CommandName=
"Edit"
ImageUrl=
"~/img/edit.png"
PostBackUrl=
'<%# "WallPaperRecord.aspx?id="+ Eval("Id") %>'
Height=
"32px"
Width=
"32px"
/>
<asp:ImageButton ID=
"imgbtndel"
runat=
"server"
CommandName=
"delete"
ImageUrl=
"~/img/delete.png"
Height=
"32px"
Width=
"32px"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</form>
protected
void
Page_Load(
object
sender, EventArgs e)
{
Int32 ID = Convert.ToInt32(Request.QueryString[
"Id"
]);
if
(ID ==
null
|| ID == 0)
{
}
else
{
editmode();
}
if
(!IsPostBack)
{
bindUsingCategory();
//bind dropdown from database
bindCategory();
//bind gridview
}
}
public
void
bindUsingCategory()
//bind dropdown from database
{
using
(WallpaperEntities13 context =
new
WallpaperEntities13())
{
ddlWallCategry.DataSource = (from r
in
context.WallPaperCategries select
new
{ CategoryName = r.CategoryName, Id = r.Id }).ToList();
ddlWallCategry.DataTextField =
"CategoryName"
;
ddlWallCategry.DataValueField =
"Id"
;
ddlWallCategry.DataBind();
ddlWallCategry.Items.Insert(0,
new
System.Web.UI.WebControls.ListItem());
}
}
private
void
bindCategory()
//bind gridview
{
using
(WallpaperEntities13 context =
new
WallpaperEntities13())
{
GridViewCategory.DataSource = context.WallpaperRecords.ToList();
GridViewCategory.DataBind();
}
}
protected
void
ddlWallCategry_SelectedIndexChanged(
object
sender, EventArgs e)
{
using
(WallpaperEntities13 context =
new
WallpaperEntities13())
{
var Category = (from em
in
context.WallpaperRecords
where em.CategoryId == ddlWallCategry.SelectedValue
select
new
{
em.Id,
em.CategoryName,
em.Thumbnail,
}).ToList();
ddlWallCategry.DataTextField =
"CategoryName"
;
ddlWallCategry.DataValueField =
"Id"
;
GridViewCategory.DataSource = Category;
GridViewCategory.DataBind();
}|
}
protected
void
editmode()
{
Int32 ID = Convert.ToInt32(Request.QueryString[
"CategoryName"
]);
using
(WallpaperEntities13 ctx =
new
WallpaperEntities13())
{
btnupdate.Visible =
true
;
btnSave.Visible =
false
;
var Category =
int
.Parse(Request[
"Id"
]);
var query = (from c
in
ctx.WallpaperRecords
where c.Id == Category
select
new
{
c.CategoryName,
c.Id,
c.CategoryId,
c.Thumbnail
}).FirstOrDefault();
if
(query !=
null
)
{
lblCategoryName = query.CategoryName.ToString();
ddlWallCategry.SelectedValue = query.CategoryId;
ctx.SaveChanges();
GridViewCategory.EditIndex = -1;
ddlWallCategry. DataBind();
//getting error in this line...ddlWallCategry' has a SelectedValue which is invalid because it does not exist in the list of items.
}
ddlWallCategry.SelectedValue =
""
;
}
}
protected
void
btnupdate_Click(
object
sender, EventArgs e)
{
using
(var context =
new
WallpaperEntities13())
{
var Categorys =
int
.Parse(Request[
"Id"
]);
string
Category = Convert.ToString(Session[
"CategoryName"
]);
var std = (from x
in
context.WallpaperRecords
where x.CategoryName == Category
select x).FirstOrDefault();
if
(std !=
null
)
{
std.CategoryName = lblCategoryName.ToString();
context.SaveChanges();
GridViewCategory.EditIndex = -1;
DataBind();
}
}
}
Reply
Answers (
1
)
how selected all month through save one parameter in c# desktop
How to crack azure certification Az900 in 1 attempt