I have a category table with 4 columns as and i have made Id as identity as well as Primarykey
ID Categoryname Cimage
Then i have a subcategory table as
ID1 Subcategoryname Simage Description Catid .
In category page
I have a Textbox (for entering name of category) and Image . I have binded it to gridview.
so my gridview displays Textbox data and image.
In my Subcategory page
I have a dropdownlist ,Textbox1 (for entering name of subcategory) , Image , Textbox2 (for entering description)
I populate the dropdownlist from category table(categoryname)
and the rest data should be saved into database and display in gridview..
I am stuck with subcategory page .My data enters into database but unable to display in gridview....
how should i relate 2 tables with id and catid .
and how to write the ado.net connections for subcategory page..
I have been trying for weeks but dint succeed...
can anyone help me out with detailed coding. please
Thanks in advance .
Loading
Iftikar HussainPosted Jul 12, 2013, 2:41 AM
and in your code
try the above code and check weather your first issue is resolved or not
Regards,
Iftikar
Iftikar HussainPosted Jul 15, 2013, 7:39 AM
Regards,
Iftikar
antony victorPosted Jul 15, 2013, 7:29 AM
it works fine when i type, select * from subcategory where cid=@cid;
But by using above query i cant get cname into gridview , which is in the category table.
Iftikar HussainPosted Jul 15, 2013, 4:25 AM
select id1,sname,siamge ,desc where cid=@cid
Regards,
Iftikar
antony victorPosted Jul 15, 2013, 3:42 AM
id cname cimage
id (1,1) int;
cname varchar(50);
cimage varchar(250)
My subcategory Table
id1 sname simage desc cid
id1 (1,1) int ;
sname varchar(50);
siamge varchar(250);
desc varchar(250);
cid int
sp for category table
Insert category
Delete category
update category
sp for Subcategory table
Insert subcategory
Delete Subcategory
Update Subcategory
Iftikar HussainPosted Jul 15, 2013, 3:32 AM
Regards,
Iftikar
antony victorPosted Jul 15, 2013, 3:28 AM
I got an error saying
SqlException was unhandled By UserCode.
Incorrect syntax near the keyword 'desc'.
Sanjeeb LenkaPosted Jul 15, 2013, 2:01 AM
try this code:
private void bindgrid1()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
DataSet ds = new DataSet();
string query = "SELECT C.cname AS Category,S.cid,S.sname AS SubCategory,S.simage AS subcatimage, S.desc AS description FROM subcategory S INNER JOIN Category c ON S.Cid=C.id where S.Cid=@cid";
SqlCommand com = new SqlCommand(query, con);
com.Parameters.AddWithValue("@cid", DropDownList1.SelectedValue);
SqlDataAdapter sqlda = new SqlDataAdapter(com);
sqlda.Fill(ds);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
con.Close();
sqlda.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
Label3.Visible = true;
GridView2.Visible = false;
Label3.Text = "No images added yet";
}
else
{
Label3.Visible = false;
GridView2.Visible = true;
GridView2.DataSource = ds;
GridView2.DataBind();
sqlda.Dispose();
con.Close();
}
}
Iftikar HussainPosted Jul 15, 2013, 1:57 AM
Regards,
Iftikar
antony victorPosted Jul 15, 2013, 1:50 AM
I m able to retreive all subcategory details, but now a small correction
In my subcategory Gridview , i also want to display cname(categoryname) which i pulled in ddl from category table...
coz i dont have a cname(categoryname) in my subcategory table .
Iftikar HussainPosted Jul 15, 2013, 1:37 AM
You should able to retrieve all subcategory by cid
Regards,
Iftikar
antony victorPosted Jul 15, 2013, 1:32 AM
In my gridview ,i am cascading data(cname) into ddl from previous category table.
following is the below code
private void bindgrid1()
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
DataSet ds = new DataSet();
SqlCommand com = new SqlCommand("select * from subcategory where cid=@cid", con);
com.Parameters.AddWithValue("@cid", DropDownList1.SelectedValue);
SqlDataAdapter sqlda = new SqlDataAdapter(com);
sqlda.Fill(ds);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
con.Close();
sqlda.Fill(ds);
if (ds.Tables[0].Rows.Count == 0)
{
Label3.Visible = true;
GridView2.Visible = false;
Label3.Text = "No images added yet";
}
else
{
Label3.Visible = false;
GridView2.Visible = true;
GridView2.DataSource = ds;
GridView2.DataBind();
sqlda.Dispose();
con.Close();
}
}
Design of my table is
category
id cname cimage
subcategory
id1 sname simage desc cid.
Here what i have done is, i am storing id value into ddl from category table and also storing the same into cid of subcategory table so that i can relate two tables.
and one more thing is , i dont have cid common in both the tables.
could you guide me please sir.
Sanjeeb LenkaPosted Jul 13, 2013, 3:07 PM
You want to show the Category Name in Subcategory gridview. or You want to show
Category id.
if you want Category name then try this query and bind it to grid:
SELECT C.Cname AS Category,S.Cid,S.sname AS SubCategory,S.simage AS subcatimage, S.desc AS description
FROM subcategory S
INNER JOIN Category c
ON S.Cid=C.Cid
any problem reply
antony victorPosted Jul 13, 2013, 4:20 AM
I have a problem, In my subcategory table , i have columns
id1 sname simage desc cid
But i want to display in gridview as
category subcategory subcatimage description ...
The category column , i populated it from category table onto dropdownlist in subcategorypage
and that showed up in dropdownlist in subcategory page and i am able to delete rows via cid.
The problem now is how do i display the category column in subcategory gridview ?
antony victorPosted Jul 12, 2013, 7:38 AM
Iftikar HussainPosted Jul 12, 2013, 6:28 AM
select * from subcategory where cid=@cid
Regards,
Iftikar
antony victorPosted Jul 12, 2013, 6:21 AM
Iftikar HussainPosted Jul 12, 2013, 5:47 AM
Regards,
Iftikar
antony victorPosted Jul 12, 2013, 5:45 AM
Iftikar HussainPosted Jul 12, 2013, 5:05 AM
put the break point in the highlighted line
Regards,
Iftikar
antony victorPosted Jul 12, 2013, 4:59 AM
Iftikar HussainPosted Jul 12, 2013, 3:25 AM
Regards,
Iftikar
antony victorPosted Jul 12, 2013, 3:23 AM
Iftikar HussainPosted Jul 12, 2013, 3:02 AM
Regards,
Iftikar
antony victorPosted Jul 12, 2013, 2:56 AM
antony victorPosted Jul 12, 2013, 2:11 AM
This is the query in sp written..
Iftikar HussainPosted Jul 12, 2013, 2:06 AM
Regards,
Iftikar
antony victorPosted Jul 12, 2013, 2:03 AM
where they were implementing the employee details.
I have kept the sp name same but changed the sp code in database.
SqlCommand com = new SqlCommand("insert_employee", con);
SqlCommand com = new SqlCommand("delete_employee", con);
SqlCommand com = new SqlCommand("update_employee", con);
so i have changed it to SqlCommand com = new SqlCommand("insert_category", con);
Here i am implementing a Customer Management System,
where in ,if i try to upload the same image twice ,it uploads but
when i try to delete any one of that , the other image link doesnt show up
, but data still exists in database.
Iftikar HussainPosted Jul 12, 2013, 1:47 AM
Regards,
Iftikar
antony victorPosted Jul 12, 2013, 1:39 AM
But can u help me . I am stuck with 2 things
When ever i try to upload the same image twice in category page with same name ,it uploads
but when i delete one of the image then the other image also doesn't show up .
But the row still exists with other data....
Second issue in Subcategory table is that image uploads twice everytime i click the button.
I have attached the category and subcategory page below.
CATEGORY PAGE
SUBCATEGORY PAGE
Please suggest me.
Iftikar HussainPosted Jul 10, 2013, 8:37 AM
You can do like this
Regards,
Iftikar