using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
namespace Taskone
{
public partial class _Default : System.Web.UI.Page
{
SqlConnection con;
protected void Page_Load(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conname"].ToString();
con = new SqlConnection(constr);
con.Open();
outlbl.Text = "Main Grid Connection Done";
if (!IsPostBack)
{
showgrid();
}
}
public void showgrid()
{
string cmd = "select id, name from master..demo";
SqlDataAdapter da = new SqlDataAdapter(cmd, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
maingrid.DataSource = dt;
maingrid.DataBind();
}
else
{
maingrid.DataSource = null;
maingrid.DataBind();
}
}
protected void maingrid_CheckedChanged(object sender, EventArgs e)
{
Boolean flag = false;
string join = "";
{
GridViewRow gvr = maingrid.SelectedRow;
for (int i = 0; i < maingrid.Rows.Count; i++)
{
if (((CheckBox)maingrid.Rows[i].FindControl("mainchk")).Checked == true)
{
string lbltxt = ((Label)maingrid.Rows[i].FindControl("mainid")).ToString();
flag = true;
join += lbltxt + ",";
}
}
if (flag == true)
{
join = join.Remove(join.Length - 1, 1);
string constr = ConfigurationManager.ConnectionStrings["conname"].ToString();
con = new SqlConnection(constr);
con.Open();
outlbl.Text = "child Grid Connection Done";
string cmd = "select id, name from master..demo where id in('" + join + "')";
SqlDataAdapter da = new SqlDataAdapter(cmd, con);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
childgrid.DataSource = dt;
childgrid.DataBind();
}
else
{
childgrid.DataSource = null;
childgrid.DataBind();
}
}
}
}
}
}
create table demo(id int identity(1,1), name varchar(15))
insert into demo values('name1')
insert into demo values('name2')
insert into demo values('name3')
insert into demo values('name4')
insert into demo values('name5')
select * from demo
You need to check all these things.., Since you join variable is string datatype you need to convert it before assign it in the query. I think the query will accept a int variable for the ID since the datatype is numeric in the DataBase...
n2adn_India
string join ="";
n2adn_India
string lbltxt =((Label)maingrid.Rows[i].FindControl("mainid")).ToString();
flag =true; join
+= lbltxt +",";
n2adn_India
string cmd ="select id, name from master..demo where id in('"+ join +"')";
Parthasarathi,
Remember to click Mark as Answer on the post that helps to others.
What you say is working, but second time, when another checkbox is checked its showing the same error.
When first time loading of the page, when I clicked the checkbox of first row. the child grid of value is showing, when second checkbox i select(again oncheckedchanged triggered) means its going to same error.
I think small looping problem i thing so... Please share your view.
string cmd = "select id, name from master..demo where id in('" + join + "')";
should be changed
string cmd ="select id, name from master..demo where id in("+ join +")";
I removed the single quotes thats it. its working
Please tell me one thing when no check box is checked I need to clear the child grid. For example. If I checked the three rows then the three row will be selected and bind to child grid, if i reset to uncheck onebyone then it automatically removed except the last one which still displayed in child grid, how to clear when no check is checked.
If I select one checkbox in maingrid, other checkboxes should be deactivate, only selected checkbox should active, then result will display in below child grid...
To reactivate other box then the selected checkbox should uncheck, So this concept allow only checkbox should check.
n2adn_India
Member
15 Points
75 Posts
New to ASP.DOT
Dec 25, 2012 03:30 PM|LINK
Hi,
I am new to ASP.DOT, I tried this GRIDVIEW and getting Some error when filling data in child grid.
Please kindly help to solve this, I am Just beginer beginner.
Concept of this gridview, When I click any checkboxs that entry should displayed below Childgrid.
Please see the below cs, design and sql code & help me to solve this situation
<%@ Page Title="Home Page" Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Taskone._Default" %> <html> <head> <body> <asp:Label ID="outlbl" runat="server" Text="OutPut Rows"></asp:Label> <div> <asp:GridView ID="maingrid" AutoGenerateColumns="false" runat="server"> <Columns> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="mainid" runat="server" Text='<%# bind("id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="NAME"> <ItemTemplate> <asp:Label ID="mainname" runat="server" Text='<%# bind("name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Select"> <ItemTemplate> <asp:CheckBox ID="mainchk" runat="server" AutoPostBack="true" OnCheckedChanged="maingrid_CheckedChanged"/> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> <div> <asp:GridView ID="childgrid" AutoGenerateColumns="false" runat="server"> <Columns> <asp:TemplateField HeaderText="ID"> <ItemTemplate> <asp:Label ID="childid" runat="server" Text='<%# bind("id") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="NAME"> <ItemTemplate> <asp:Label ID="childname" runat="server" Text='<%# bind("name") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </body> </head> </html>using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Data.SqlClient; using System.Configuration; using System.Data; namespace Taskone { public partial class _Default : System.Web.UI.Page { SqlConnection con; protected void Page_Load(object sender, EventArgs e) { string constr = ConfigurationManager.ConnectionStrings["conname"].ToString(); con = new SqlConnection(constr); con.Open(); outlbl.Text = "Main Grid Connection Done"; if (!IsPostBack) { showgrid(); } } public void showgrid() { string cmd = "select id, name from master..demo"; SqlDataAdapter da = new SqlDataAdapter(cmd, con); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { maingrid.DataSource = dt; maingrid.DataBind(); } else { maingrid.DataSource = null; maingrid.DataBind(); } } protected void maingrid_CheckedChanged(object sender, EventArgs e) { Boolean flag = false; string join = ""; { GridViewRow gvr = maingrid.SelectedRow; for (int i = 0; i < maingrid.Rows.Count; i++) { if (((CheckBox)maingrid.Rows[i].FindControl("mainchk")).Checked == true) { string lbltxt = ((Label)maingrid.Rows[i].FindControl("mainid")).ToString(); flag = true; join += lbltxt + ","; } } if (flag == true) { join = join.Remove(join.Length - 1, 1); string constr = ConfigurationManager.ConnectionStrings["conname"].ToString(); con = new SqlConnection(constr); con.Open(); outlbl.Text = "child Grid Connection Done"; string cmd = "select id, name from master..demo where id in('" + join + "')"; SqlDataAdapter da = new SqlDataAdapter(cmd, con); DataTable dt = new DataTable(); da.Fill(dt); if (dt.Rows.Count > 0) { childgrid.DataSource = dt; childgrid.DataBind(); } else { childgrid.DataSource = null; childgrid.DataBind(); } } } } } }create table demo(id int identity(1,1), name varchar(15)) insert into demo values('name1') insert into demo values('name2') insert into demo values('name3') insert into demo values('name4') insert into demo values('name5') select * from demoVipindas
Contributor
5514 Points
810 Posts
Re: New to ASP.DOT
Dec 25, 2012 04:19 PM|LINK
Please post error message also
n2adn_India
Member
15 Points
75 Posts
Re: New to ASP.DOT
Dec 26, 2012 02:20 AM|LINK
Conversion failed when converting the varchar value 'System.Web.UI.WebControls.Label' to data type int.
This error will in the line da.fill(dt)
if (flag == true)
{
join = join.Remove(join.Length - 1, 1);
string constr = ConfigurationManager.ConnectionStrings["conname"].ToString();
con = new SqlConnection(constr);
con.Open();
outlbl.Text = "child Grid Connection Done";
string cmd = "select id, name from master..demo where id in('" + join + "')";
SqlDataAdapter da = new SqlDataAdapter(cmd, con);
DataTable dt = new DataTable();
da.Fill(dt);
The Stricked lines showing the conversion error.
sarathi125
Star
13599 Points
2691 Posts
Re: New to ASP.DOT
Dec 26, 2012 03:00 AM|LINK
Hi,
You need to check all these things.., Since you join variable is string datatype you need to convert it before assign it in the query. I think the query will accept a int variable for the ID since the datatype is numeric in the DataBase...
Remember to click Mark as Answer on the post that helps to others.
My Blog :MyAspSnippets
Sujeet Saste
Contributor
2998 Points
572 Posts
Re: New to ASP.DOT
Dec 26, 2012 03:16 AM|LINK
string lbltxt = ((Label)maingrid.Rows[i].FindControl("mainid")).ToString();Change it tostring lbltxt = ((Label)maingrid.Rows[i].FindControl("mainid")).Text.ToString();Do FEAR (Face Everything And Rise)
Please mark as Answer if my post helps you..!
My Blog
n2adn_India
Member
15 Points
75 Posts
Re: New to ASP.DOT
Dec 26, 2012 04:43 AM|LINK
Thanks
What you say is working, but second time, when another checkbox is checked its showing the same error.
When first time loading of the page, when I clicked the checkbox of first row. the child grid of value is showing, when second checkbox i select(again oncheckedchanged triggered) means its going to same error.
I think small looping problem i thing so... Please share your view.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: New to ASP.DOT
Dec 26, 2012 05:03 AM|LINK
No matter how many times do you click the CheckBox, you have to make sure that your Label's Text can always be changed to numeric.
n2adn_India
Member
15 Points
75 Posts
Re: New to ASP.DOT
Dec 26, 2012 05:16 AM|LINK
Hi All,
Thanks finally catch the error with your helps.
The error in the line
string cmd = "select id, name from master..demo where id in('" + join + "')"; should be changedSujeet Saste
Contributor
2998 Points
572 Posts
Re: New to ASP.DOT
Dec 26, 2012 05:23 AM|LINK
Make following changes in your code :
if (flag == true) { //YOUR CURRENT CODE } //ADD THIS ELSE CONDITION AFTER IF LOOP else { childgrid.DataSource = null; childgrid.DataBind(); }Do FEAR (Face Everything And Rise)
Please mark as Answer if my post helps you..!
My Blog
n2adn_India
Member
15 Points
75 Posts
Re: New to ASP.DOT
Dec 26, 2012 05:54 AM|LINK
Working great Sujeet,
One more modification needed.
If I select one checkbox in maingrid, other checkboxes should be deactivate, only selected checkbox should active, then result will display in below child grid...
To reactivate other box then the selected checkbox should uncheck, So this concept allow only checkbox should check.
Please share your view.