Monday, 11 March 2013
Insert, Edit, Delete, Update in GridView Using Asp.Net with C-Sharp And How to display images from database using image Path in GridView
aspx Code
<asp:GridView ID="Fare_Images_GridView" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None"
AllowPaging="false" AllowSorting="true"
onrowcancelingedit="Fare_Images_GridView_RowCancelingEdit"
onrowediting="Fare_Images_GridView_RowEditing"
OnRowDeleting="Fare_Images_GridView_RowDeleting"
OnRowUpdating="Fare_Images_GridView_RowUpdating"
OnRowCommand="Fare_Images_GridView_RowCommand" >
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lbl_image_id" runat="server" Visible="false" Text='<%# Eval("image_id") %>' ></asp:Label>
<asp:Label ID="lbl_Partner_id" runat="server" Visible="false" Text='<%# Eval("Partner_id") %>' ></asp:Label>
<asp:Label ID="lbl_image_name" runat="server" Visible="false" Text='<%# Eval("image_name") %>' ></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Logo">
<ItemTemplate >
<asp:Image ID="lbl_image_path" runat="server" ImageUrl='<%#Eval("image_path") %>' Width="70px" Height="70px" />
</ItemTemplate>
<EditItemTemplate>
<asp:Image ID="lbl_image_path" runat="server" ImageUrl='<%#Eval("image_path") %>' Width="40px" Height="40px" />
<asp:FileUpload ID="FileUploadImagePartner" FileName='<%#Eval("image_path") %>' Width="100px" runat="server" />
</EditItemTemplate>
<InsertItemTemplate>
<asp:FileUpload ID="FileUploadImagePartner" FileName='<%#Eval("image_path") %>' Width="100px" runat="server" />
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Partner Name">
<ItemTemplate>
<asp:LinkButton ID="link_Partner_Name" CommandName="Partner_Name" CommandArgument='<%# Eval("Partner_id") %>' runat="server" Text='<%#Eval("Partner_Name") %>' ></asp:LinkButton>
<%-- <asp:Label ID="lbl_Partner_Name" runat="server" Visible="true" Text='<%# Eval("Partner_Name") %>' ></asp:Label>--%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="txt_Partner_Name" runat="server" Text='<%#Eval("Partner_Name") %>' Width="80px" ></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Edit" ShowHeader="false">
<ItemTemplate>
<asp:LinkButton ID="btnedit" runat="server"
CommandName="Edit" Text="Edit" ></asp:LinkButton>
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton ID="btndelete" runat="server"
CommandName="Delete" Text="Delete" OnClientClick="return confirm('This will delete your all Fares.Are you sure you want to delete? ');" ></asp:LinkButton>
<asp:LinkButton ID="btnupdate" runat="server"
CommandName="Update" Text="Update" ></asp:LinkButton>
<asp:LinkButton ID="btncancel" runat="server"
CommandName="Cancel" Text="Cancel"></asp:LinkButton>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="white" /><%--#2461BF--%>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
CS Code
//Method in the Data Access Layer
public void FillGridView(string strProName, System.Web.UI.WebControls.GridView GridViewName)
{
//int status = 0;
DataTable dt = new DataTable();
try
{
using (SqlConnection conn = DataAccess.GetConnected())
{
SqlCommand cmd = new SqlCommand(strProName, conn);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sqlda = new SqlDataAdapter();
sqlda.SelectCommand = cmd;
sqlda.Fill(dt);
GridViewName.DataSource = dt;
GridViewName.DataBind();
}
}
catch (Exception ex)
{
//throw ex.Message;
}
}
//Call on Presentation Layer
protected void GetAllImagesPartner()
{
da.FillGridView("ShowPartnerImagesGridView", Fare_Images_GridView);
}
protected void Fare_Images_GridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
Fare_Images_GridView.EditIndex = -1;
GetAllImagesPartner();
}
protected void Fare_Images_GridView_RowEditing(object sender, GridViewEditEventArgs e)
{
Fare_Images_GridView.EditIndex = e.NewEditIndex;
GetAllImagesPartner();
}
protected void Fare_Images_GridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string filename = "";
string image="";
try
{
FileUpload fileupload = (FileUpload)Fare_Images_GridView.Rows[e.RowIndex].FindControl("FileUploadImagePartner");
if (fileupload.HasFile)
{
// string filename = Path.GetFileName(this.fileupload.PostedFile.FileName);
filename = fileupload.FileName;
image=filename;
fileupload.SaveAs(Server.MapPath("~/Partner_Fare_Images/" + filename));
}
else
{
Label image_name = (Label)Fare_Images_GridView.Rows[e.RowIndex].FindControl("lbl_image_name");
image=image_name.Text;
//filename = image_name.ToString();
}
Label lbl_Partner_Id = (Label)Fare_Images_GridView.Rows[e.RowIndex].FindControl("lbl_Partner_id");
Label lbl_image_Id = (Label)Fare_Images_GridView.Rows[e.RowIndex].FindControl("lbl_image_id");
TextBox txt_Partner_Name = (TextBox)Fare_Images_GridView.Rows[e.RowIndex].FindControl("txt_Partner_Name");
using (SqlConnection con = DataAccess.GetConnected())
{
SqlCommand cmd = new SqlCommand("UpdatePartnerImage", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Partner_Id", lbl_Partner_Id.Text);
cmd.Parameters.AddWithValue("@Image_Id", lbl_image_Id.Text);
cmd.Parameters.AddWithValue("@Image_Path", "Partner_Fare_Images/" + image);
//cmd.Parameters.AddWithValue("@Image_Path", );
cmd.Parameters.AddWithValue("@Partner_Name", txt_Partner_Name.Text);
cmd.Parameters.AddWithValue("@image_name", image);
cmd.ExecuteNonQuery();
Fare_Images_GridView.EditIndex = -1;
GetAllImagesPartner();
}
}
catch (Exception ex)
{
}
}
protected void Fare_Images_GridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
GridViewRow row = Fare_Images_GridView.Rows[e.RowIndex];
Label Image_Id = (Label)row.FindControl("lbl_Image_id");
Label Partner_Id = (Label)row.FindControl("lbl_Partner_id");
using (SqlConnection con = DataAccess.GetConnected())
{
SqlCommand cmd = new SqlCommand("deletePartnerFullData", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Image_Id", Image_Id.Text);
cmd.Parameters.AddWithValue("@Partner_Id", Partner_Id.Text);
cmd.ExecuteNonQuery();
con.Close();
GetAllImagesPartner();
}
}
catch (Exception ex)
{
}
}
protected void Fare_Images_GridView_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Partner_Name")
{
using (SqlConnection con = DataAccess.GetConnected())
{
try
{
int rowindex = Convert.ToInt32(e.CommandArgument);
GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;
LinkButton Partner_Name = (LinkButton)row.FindControl("link_Partner_Name");
//Label Partner_Id = (Label)row.FindControl("lbl_Partner_Id");
hdn_Add_Fare_Partner.Value = Partner_Name.Text;
hdn_Add_Fare_Id.Value = rowindex.ToString();
lbl_Add_Fare.Text = "";
lbl_Add_Fare.Text = "Add Fare For " + hdn_Add_Fare_Partner.Value;
GetAllFaresById(Convert.ToInt32(hdn_Add_Fare_Id.Value));
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
finally
{
con.Close();
}
}
}
}