I have a category page wherein i am adding a textbox data as categoryname and an image as categoryimage to database and also binding to gridview in same page.
Now I have another page Subcategory
I want to pull the data from category page ie categoryname into dropdownlist and also add another textbox and image under it.
In category table ID ImageName Imagepath .
could anyone help me with connections in subcategory page as well as sqldatabase design as how to link the two tables.
I have pasted the category and subcategory table
CATEGORY TABLE:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Save Images In Folder and Display Images in Gridview from folder</title>
<style type="text/css">
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
</style>
<script type="text/javascript">
function Confirmationbox() {
var result = confirm('Are you sure you want to delete selected Category(s)?');
if (result) {
return true;
}
else {
return false;
}
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Category Name" Width="100px" Height="16px"
style="margin-top: 0px"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server" Width="180px" style="margin-top: 0px"
Height="23px"></asp:TextBox>
<br/>
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Upload Image" Width="100px" Height="16px"
style="margin-top: 0px"></asp:Label>
<asp:FileUpload ID="fileuploadimages" runat="server" />
<br />
<br />
<br />
<asp:Button ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" />
</div>
<div>
<br />
<br />
<asp:GridView runat="server" DataKeyNames="ID" ID="gvImages" AutoGenerateColumns="False"
DataSourceID="sqldataImages" CssClass="Gridview"
HeaderStyle-BackColor="#61A6F8"
onselectedindexchanged="gvImages_SelectedIndexChanged" >
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID" />
<asp:BoundField DataField="ImageName" HeaderText="CategoryName" />
<asp:ImageField HeaderText="Image" DataImageUrlField="ImagePath" />
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkdelete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<%----%>
</Columns>
<HeaderStyle BackColor="#61A6F8"></HeaderStyle>
</asp:GridView>
<asp:SqlDataSource ID="sqldataImages" runat="server" ConnectionString="<%$ConnectionStrings:dbconnection %>"
SelectCommand="select * from Category1"
DeleteCommand="delete from Category1 where ID=@ID" >
<DeleteParameters>
<asp:Parameter Name="ID" Type="Int32" />
</DeleteParameters>
</asp:SqlDataSource>
</div>
<asp:Button ID="btnDelete" runat="server" Text="Delete" onclick="btnDelete_Click" />
</form>
</body>
</html>
.CS file:
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
public partial class _Default : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
btnDelete.Attributes.Add("onclick", "javascript:return Confirmationbox()");
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
string filename = TextBox1.Text;
fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));
con.Open();
SqlCommand cmd = new SqlCommand("Insert into Category1(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
cmd.Parameters.AddWithValue("@ImageName", filename);
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("~/Default.aspx");
}
protected void gvImages_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnDelete_Click(object sender, EventArgs e)
{
foreach (GridViewRow gvrow in gvImages.Rows)
{
CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkdelete");
if (chkdelete.Checked)
{
int UID = Convert.ToInt32(gvImages.DataKeys[gvrow.RowIndex].Value);
sqldataImages.DeleteParameters["ID"].DefaultValue = UID.ToString();
sqldataImages.Delete();
}
}
}
}
SUBCATEGORY PAGE:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="subcategory.aspx.cs" Inherits="subcategory" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="CategoryName" Width ="150px"></asp:Label>
<asp:DropDownList ID="DropDownList1" runat="server" Height="36px" Width="150px">
</asp:DropDownList>
<br />
<br />
</div>
<asp:Label ID="Label2" runat="server" Text="SubcategoryName"></asp:Label>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<br />
Image
<asp:FileUpload ID="FileUpload1" runat="server" />
<br />
<br />
<asp:Label ID="Label3" runat="server" Text="Description"></asp:Label>
<asp:TextBox ID="TextBox2" runat="server" style="margin-left: 0px"
Width="159px"></asp:TextBox>
<br />
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Add" Width="100px"
onclick="Button1_Click"/>
</form>
</body>
</html>