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" %>
.Gridview
{
font-family:Verdana;
font-size:10pt;
font-weight:normal;
color:black;
}
function Confirmationbox() {
var result = confirm('Are you sure you want to delete selected Category(s)?');
if (result) {
return true;
}
else {
return false;
}
}
style="margin-top: 0px">
Height="23px">
style="margin-top: 0px">
DataSourceID="sqldataImages" CssClass="Gridview"
HeaderStyle-BackColor="#61A6F8"
onselectedindexchanged="gvImages_SelectedIndexChanged" >
<%--
CommandArgument='<%# Eval("ID") %>'
CommandName="Delete" runat="server">
Delete
--%>
SelectCommand="select * from Category1"
DeleteCommand="delete from Category1 where ID=@ID" >
.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" %>
Image
Width="159px">
onclick="Button1_Click"/>
18 Replies
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Sanjeeb LenkaPosted Jun 25, 2013, 3:38 AM
put this
check this Id you are binding.
if changing this you are getting same problem the share your subcatogory table structure in database
antony victorPosted Jun 25, 2013, 3:30 AM
Buttonclick
Sanjeeb LenkaPosted Jun 25, 2013, 3:24 AM
antony victorPosted Jun 25, 2013, 3:22 AM
antony victorPosted Jun 25, 2013, 3:18 AM
Sanjeeb LenkaPosted Jun 25, 2013, 3:17 AM
antony victorPosted Jun 25, 2013, 3:04 AM
This is the error line
The DataSourceID of 'GridView1' must be the ID of a control of type IDataSource. A control with ID 'sqldataImages' could not be found.
I am pasting the code of sqldatasource in aspx page as well as the backend code
Subcategory.aspx
buttonclick
Sanjeeb LenkaPosted Jun 25, 2013, 2:55 AM
antony victorPosted Jun 25, 2013, 2:15 AM
A field or property with the name 'ImageName' was not found on the selected data source. Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.Web.HttpException: A field or property with the name 'ImageName' was not found on the selected data source.
My Button click event.
string ID= Convert.ToString(DropDownList1.SelectedItem.Value);
con.Open(); SqlCommand cmd = new SqlCommand("Insert into Subcategory1(SimageName,SimagePath) values(@SimageName,@SimagePath)", con); string filename = TextBox1.Text; //Save images into Images folder FileUpload1.SaveAs(Server.MapPath("Images/" + filename)); //Passing parameters to query cmd.Parameters.AddWithValue("@SimageName", filename); cmd.Parameters.AddWithValue("@SimagePath", "Images/" + filename); cmd.ExecuteNonQuery(); //Close dbconnection con.Close(); Response.Redirect("~/Subcategory.aspx");
Sanjeeb LenkaPosted Jun 25, 2013, 1:31 AM
your design completed . db design completed so what the problem actually you are facing
antony victorPosted Jun 25, 2013, 1:26 AM
Sanjeeb LenkaPosted Jun 25, 2013, 1:21 AM
antony victorPosted Jun 25, 2013, 1:20 AM
antony victorPosted Jun 25, 2013, 1:13 AM
Pankaj PandeyPosted Jun 24, 2013, 8:11 AM
Sanjeeb LenkaPosted Jun 24, 2013, 7:16 AM
just follow this sample table for subcategory
subcategryId should be primary key with identity.
in your form take one dropdown bind it to category name and id
one textbox for subcategoryname
and file upload conrol.
if any problem reply.
antony victorPosted Jun 24, 2013, 6:48 AM
Bhabani prasad DashPosted Jun 24, 2013, 6:25 AM
cmd = new SqlCommand("select Distinct colname from tablename order by colname asc ", con);
da = new SqlDataAdapter(cmd);
ds = new DataSet();
da.Fill(ds, "tablename");
ddl_state.DataSource = ds;
ddl_state.DataValueField = "colname";
ddl_state.DataBind();