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
antony victor
NA
90
22.2k
How to populate dropdownlist and add values to database.
Jun 24 2013 5:50 AM
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>
<%-- <asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1"
CommandArgument='<%# Eval("ID") %>'
CommandName="Delete" runat="server">
Delete</asp:LinkButton>
</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()");
//if (!IsPostBack)
//{
//}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Get Filename from fileupload control
//string filename = Path.GetFileName(fileuploadimages.PostedFile.FileName);
string filename = TextBox1.Text;
//Save images into Images folder
fileuploadimages.SaveAs(Server.MapPath("Images/" + filename));
//Getting dbconnection from web.config connectionstring
//Open the database connection
con.Open();
//Query to insert images path and name into database
SqlCommand cmd = new SqlCommand("Insert into Category1(ImageName,ImagePath) values(@ImageName,@ImagePath)", con);
//Passing parameters to query
cmd.Parameters.AddWithValue("@ImageName", filename);
cmd.Parameters.AddWithValue("@ImagePath", "Images/" + filename);
cmd.ExecuteNonQuery();
//Close dbconnection
con.Close();
Response.Redirect("~/Default.aspx");
}
//protected void gvImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
//{
// int id = Convert.ToInt32(Gd_GalleryDetails.DataKeys[e.RowIndex].Value);
// ad.deletegalleryimage(id);
// msg.Show("Image deleted from gallery");
// fillgrid();
//private void BindData(string Query)
//{
//{
// con.Open();
// using (SqlCommand comm = new SqlCommand(Query +
// "; select * from Category1 ", con))
// {
// SqlDataAdapter da = new SqlDataAdapter(con);
// DataSet ds = new DataSet();
// da.Fill(ds);
// gvImages.DataSource = ds;
// gvImages.DataBind();
// }
// }
//}
// protected void gvImages_RowDeleting(object sender, GridViewDeleteEventArgs e)
//{
// string EmployeeID = gvImages.DataKeys[e.RowIndex].Value.ToString();
// string Query = "delete Category1 where Category1.Category1ID =" +Category1ID;
// BindData(Query);
//}
//protected void gvImages_RowDataBound(object sender,
// GridViewRowEventArgs e)
//{
// if (e.Row.RowType == DataControlRowType.DataRow)
// {
// LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
// l.Attributes.Add("onclick", "javascript:return " +
// "confirm('Are you sure you want to delete this record " +
// DataBinder.Eval(e.Row.DataItem, "CategoryID") + "')");
// }
//}
//protected void gvImages_RowCommand(object sender,
// GridViewCommandEventArgs e)
//{
// if (e.CommandName == "Delete")
// {
// // get the categoryID of the clicked row
// int ID = Convert.ToInt32(e.CommandArgument);
// // Delete the record
// DeleteRecordByID(ID);
// // Implement this on your own :)
// }
//}
//protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
//{
// int ID = (int)gvImages.DataKeys[e.RowIndex].Value;
// DeleteRecordByID(ID);
//}
protected void gvImages_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void btnDelete_Click(object sender, EventArgs e)
{
//Loop through all the rows in gridview
foreach (GridViewRow gvrow in gvImages.Rows)
{
//Finiding checkbox control in gridview for particular row
CheckBox chkdelete = (CheckBox)gvrow.FindControl("chkdelete");
//Condition to check checkbox selected or not
if (chkdelete.Checked)
{
//Getting UserId of particular row using datakey value
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>
Reply
Answers (
18
)
ReportViewer - Add Header Dynamically
save .ppt file in sql server