Display gridview only when matching ID's match - c#

I have applied a filter on dropdownlist selected value which matched the Project_id, Building_id and flat_id then gridview should display other wise not.
Now what I want is if ALL the id matches then only display the gridview.
Here is my code below:-
DataTable dt = new DataTable();
dt.Columns.Add("PROJECT_ID");
dt.Columns.Add("BUILDING_ID");
dt.Columns.Add("FLAT_ID");
DataView dView = new DataView(dt);
dView.RowFilter = "PROJECT_ID = " + ddlProject.SelectedValue + " AND BUILDING_ID = " + ddlBuilding.SelectedValue + " AND FLAT_ID = " + ddlUnit.SelectedItem.Text;
GrdBookingStatus.DataSource = dView.ToTable();
GrdBookingStatus.DataBind();
DisplayGrid();
GrdBookingStatus.Visible = true;
Currently it is showing if the ID's doesn't matches also
what condition should I check ?
DisplayGrid()
protected void DisplayGrid()
{
OracleCommand cmd = new OracleCommand("SELECT pn.property_name, pn.building, cd.flat_id, ab.flat_status, ab.COMMENTS " +
"FROM xxcus.xxacl_pn_projbuild_v pn INNER JOIN xxacl_pn_flat_status_his ab " +
"ON pn.project_id = ab.project_id AND pn.building_id = ab.building_id " +
"INNER JOIN xxacl_pn_flat_det_v cd ON cd.flat_id = ab.flat_id", ObjPriCon);
DataTable dtfillgrid = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dtfillgrid);
GrdBookingStatus.DataSource = dtfillgrid;
GrdBookingStatus.DataBind();
}

Your DisplayGrid() method reverts back the filter you had and displays all data.
Move the filter inside the DisplayGrid() method.
protected void DisplayGrid()
{
OracleCommand cmd = new OracleCommand("SELECT pn.project_id AS project_id, pn.property_name, pn.building, pn.building_id AS building_id, cd.flat_id AS flat_id, ab.flat_status, ab.COMMENTS " +
"FROM xxcus.xxacl_pn_projbuild_v pn INNER JOIN xxacl_pn_flat_status_his ab " +
"ON pn.project_id = ab.project_id AND pn.building_id = ab.building_id " +
"INNER JOIN xxacl_pn_flat_det_v cd ON cd.flat_id = ab.flat_id", ObjPriCon);
DataTable dtfillgrid = new DataTable();
OracleDataAdapter da = new OracleDataAdapter(cmd);
da.Fill(dtfillgrid);
DataView dView = new DataView(dtfillgrid);
dView.RowFilter = "PROJECT_ID = " + ddlProject.SelectedValue + " AND BUILDING_ID = " + ddlBuilding.SelectedValue + " AND FLAT_ID = " + ddlUnit.SelectedItem.Text;
GrdBookingStatus.DataSource = dView.ToTable();
GrdBookingStatus.DataBind();
GrdBookingStatus.Visible = dView.ToTable().Rows.Count > 0;
}
I've also modified you SELECT statement to return pn.project_idand pn.building_id

protected void Button1_Click(object sender, EventArgs e)
{
if (ddl.SelectedIndex > 0)
{
int count = 0;
int selectedcheckbox = 0;
pfPL.Month = lblMonthId.Value;
pfPL.Year = lblYear.Value;
DataTable dt = pfBL.GetEmployeeCode(pfCPL);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
if (i < dt.Rows.Count - 1)
{
for (int k = 0; k < grd.Rows.Count; k++)
{
string grdcode;
Label lbl = (Label)grd.Rows[k].FindControl("lblCode");
grdcode = lbl.Text;
CheckBox chk = (CheckBox)grd.Rows[k].FindControl("chkone");
e;
dbcode = dt.Rows[i][0].ToString();
if (dbcode.Trim() == grdcode.Trim())
{
chk.Checked = true;
count++;
}
}
}
}
}
}
}

Related

Rows cannot be programmatically added to datagrid

How do I resolve this error:
Rows cannot be programmatically added to the DataGridView's rows collection when the control is data-bound.
From the below code:
string str = "Er.fName,Er.lName";
DataSet dataSet = new DataSet();
DataSet info = this.GetData.getInfo("SELECT 0 AS ErNo, Er.EmpID, (Er.fName&''&sr.lName) AS [Employee Name], Ed.DeptNo AS [Dept No] FROM (EmployeeReg AS Er INNER JOIN EmployeeDept AS Ed ON Er.EmpId = Er.EmpId) WHERE Ed.DeptId=" + (object) DeptID + " AND Ed.Status=0 AND Er.EmpStatus=1 ORDER BY " + str, "EmployeeDept");
if (info.Tables[0].Rows.Count > 0) {
for (int index = 0; index < info.Tables[0].Rows.Count; ++index)
//error from the code blw
this.dgvPrint.Rows.Add((object) 0, (object)(index + 1), (object) info.Tables[0].Rows[index]["Emp Name"].ToString(), (object) info.Tables[0].Rows[index]["Emp No"].ToString(), (object) info.Tables[0].Rows[index]["EmpId"].ToString());
}
if (this.dgvPrint.Rows.Count > 0)
this.btnPrint.Enabled = true;
else
this.btnPrint.Enabled = false;
var table = info.Tables[0];
table.Rows.Add(new object[] {
0,
index + 1,
table.Rows[index]["Emp Name"],
table.Rows[index]["Emp No"],
table.Rows[index]["EmpId"]
});
or:
var table = info.Tables[0];
var new = table.NewRow();
new["colllName"] = 1;
new["colllName 2"] = 2;
//ect...
table.Rows.Add(new);

how to add button dynamically in gridview column?

i have this code of gridview. how do i enable button to be add inside all column in each row other than first column using this code ? i dont have templatefield or others assign for gridview because i declare gridview display through this code. how can i achieve that ? this is my code :
`protected void Page_Load(object sender, EventArgs e)
{
//where request_date >= DATEADD(day,-8, GETDATE())
con.Open();
DataTable dtTemp = new DataTable();
cmd = new SqlCommand("SELECT a.request_date,a.prod_line,a.jo_no,a.qty,CONVERT(VARCHAR(10),a.need_by_date ,101) as need_by_date,b.process_id FROM CutPanelCard a LEFT JOIN CutPanelConfirmation b on b.req_id=a.req_id where request_date >= DATEADD(day,-8, GETDATE()) order by request_date", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dtTemp);
ViewState["Information"] = dtTemp;
try
{
{
//DataTable dtTemp = (DataTable)ViewState["Information"];
DataTable dtDistinctRecords = dtTemp.DefaultView.ToTable(true, "prod_line");
DataTable dtStudentName = dtTemp.DefaultView.ToTable(true, "request_date");
DataTable a = new DataTable();
DataTable dtStudent = new DataTable();
dtStudent.Columns.Add("request_date");
foreach (DataRow rows in dtDistinctRecords.Rows)
{
dtStudent.Columns.Add(rows["prod_line"].ToString());
}
foreach (DataRow row in dtStudentName.Rows)
{
DataRow dr = dtStudent.NewRow();
dr["request_date"] = row["request_date"];
DataView dv = new DataView(dtTemp);
dv.RowFilter = "request_date='" + row["request_date"] + "'";
DataTable dtStudentdtl = dv.ToTable();
for (int i = 0; i < dtStudentdtl.Rows.Count; i++)
{
string colValue = dtStudentdtl.Rows[i]["jo_no"].ToString();
string colValue2 = dtStudentdtl.Rows[i]["qty"].ToString();
string colValue3 = dtStudentdtl.Rows[i]["need_by_date"].ToString();
string colValue4 = dtStudentdtl.Rows[i]["process_id"].ToString();
dr[dtStudentdtl.Rows[i]["prod_line"].ToString()] = "JO: " + colValue + " Quantity: " + colValue2 + " Need by Date: " + colValue3 + " Completion Status: " + colValue4;
}
dtStudent.Rows.InsertAt(dr, dtStudent.Rows.Count);
}
GridView1.DataSource = dtStudent;
GridView1.DataBind();
GridView_Row_Merger(GridView1);
con.Close();
}
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}`

ASP.NET Listbox not selecting multiple values

I have a listbox in my webform from which I am trying to select multiple values, but I am only getting last selected values. I have tried in two ways. First I explicitly added list items:
<asp:ListBox ID="ListBox2" runat="server"
SelectionMode="Multiple" AutoPostBack="True">
<asp:ListItem>teama</asp:ListItem>
<asp:ListItem>teamb</asp:ListItem>
</asp:ListBox>
Second, I tried to bind to a table in a database:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ListBox2.SelectionMode = ListSelectionMode.Multiple;
string scon = ConfigurationManager.ConnectionStrings["Test_AthiraConnectionString"].ConnectionString;
SqlConnection con=new SqlConnection(scon);
con.Open();
SqlCommand cmd = new SqlCommand("select department from department", con);
cmd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
ListBox2.DataSource = ds;
ListBox2.DataValueField = "department";
ListBox2.DataTextField = "department";
ListBox2.DataBind();
con.Close();
}
}
Then I tried these different methods to select multiple items on buttonclick event
First:
string k =null ,k1 = null;
foreach (int i in ListBox2.GetSelectedIndices())
{
k1 = ListBox2.Items[i].Text + "/";
k += k1;
Response.Write(k);
}
Second:
foreach (ListItem li in ListBox2.Items)
{
if (li.Selected == true)
{
k += li.Text + "/";
Response.Write(k);
}
}
Third:
k = String.Join("/", ListBox2.Items
.Cast<ListItem>()
.Where(i => i.Selected)
.Select(i=>i.Value)
.ToArray());
Response.Write(k);
Fourth:
for (int i = ListBox2.Items.Count - 1; i >= 0; i--)
{
if (ListBox2.Items[i].Selected == true)
{
k1 = ListBox2.Items[i].Text + "/";
k += k1;
Response.Write(k);
}
}
But none of them seem to work. I am getting only the last selected value.
You may access the items in the list as follow and check their properties.
IEnumerator ie = ListBox2.Items.GetEnumerator();
while (ie.MoveNext())
{
ListItem li = (ListItem)ie.Current;
//Use ListItem here
}
please try below code, It will return coma separator string for Listbox selected value
public string GetSelectedValues()
{
string selectedVal = string.Empty;
int i = 0;
foreach (int index in lstbox.GetSelectedIndices())
{
if (i == 0)
selectedVal = lstbox.Items[index].Value;
else
selectedVal = selectedVal + ";" + lstbox.Items[index].Value.ToString();
i++;
}
return selectedVal ;
}

how to use repeater iteratively?

I have one repeater which displays the list of puja.i have also used one datalist to display the names of temples.whenever i click on search it displays only the last value of the repeater i.e it is taking only the last value of the repeater....my question is how to use loop inside a repeater???
here is my code behind how i used datalist and repeater:
protected void Page_Load(object sender, EventArgs e)
{
//lblnodatafound.Visible = true;
//lbldatafound.Visible = false;
if (Request.QueryString["search"] != null)
{
var dt = new DataTable("Data");
String source = Request.QueryString["search"];
var splitseparator = new string[] { " " };
String[] result = source.Split(splitseparator, StringSplitOptions.RemoveEmptyEntries);
foreach (String s in result)
{
if (IsAlphaNumeric(s) == true)
{
var dttemp = new DataTable();
dttemp = fnsearch(s);
dt.Merge(dttemp, true);
}
}
if (dt.Rows.Count != 0)
{
int count = dt.Rows.Count;
if (dt.Rows.Count > 0)
{
DataList1.DataSource = dt;
DataList1.DataBind();
lblnodatafound.Visible = false;
lbldatafound.Visible = true;
//added the repeater to display the list of puja
for (int i = 0; i < dt.Rows.Count; i++)
{
String id = dt.Rows[i]["id"].ToString();
string query = "select puja.id,puja.name as puja_name, mandir.name as mandir_name from puja,mandir where mandir.id= puja.with_mandir and puja.with_mandir = '" + id + "'";
conn.Open();
MySqlCommand cmd = new MySqlCommand(query, conn);
MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
DataTable dt1 = new DataTable();
adp.Fill(dt1);
conn.Close();
if (dt1.Rows.Count > 0)
{
lblmandirpuja.Text = "Poja in " + dt1.Rows[0]["mandir_name"];
foreach (RepeaterItem repeatItem in Repeater1.Items){
Repeater1.DataSource = dt1;
Repeater1.DataBind();
}
}
}
}
}
else
{
lblnodatafound.Visible = true;
lbldatafound.Visible=false;
}
}
}
Try to change this line:
if (dt1.Rows.Count > 0)
for something like:
for(int i = 0; i < dt1.Rows.Count; i++)
And this one:
lblmandirpuja.Text = "Poja in " + dt1.Rows[0]["mandir_name"];
The 0 for the i:
lblmandirpuja.Text = "Poja in " + dt1.Rows[i]["mandir_name"];

how i get limited latest 6 rows from table

i am trying to retrieve latest 6 rows from my database table ,i am using max value but how
i get limited 6 rows from table and that retrieved result to use in for loop,to display hyperlinks in panel
protected void Page_Load(object sender, EventArgs e)
{
String sql = "select title from up_song ";
SqlDataAdapter adpt = new SqlDataAdapter(sql, cn);
DataSet ds = new DataSet();
adpt.Fill(ds, "title");
if (ds.Tables["title"].Rows.Count > 0)
{
int m = ds.Tables["title"].Rows.IndexOf(ds.Tables["title"].Rows[8]);
int k = ds.Tables["title"].Rows.IndexOf(ds.Tables["title"].Rows[1]);
for (i=m; i >= k ; --i)
{
try
{
hp[i] = new HyperLink();
hp[i].ID = "hp" + i;
hp[i].Text = ds.Tables["title"].Rows[i].ItemArray[0].ToString();
hp[i].NavigateUrl = "Downloadpage.aspx";
hp[i].ForeColor = System.Drawing.Color.White;
Panel1.Controls.Add(hp[i]);
Panel1.Controls.Add(new LiteralControl("<br>"));
HttpCookie coo = new HttpCookie("song");
coo["sogtit"] = ds.Tables["title"].Rows[i].ItemArray[0].ToString();
Response.Cookies.Add(coo);
}
catch(Exception ex)
{
Response.Write(ex.Message);
}
}
}
String sql1 = "select title from up_song where Song_type='Indian Pop Album'";
SqlDataAdapter adpt1 = new SqlDataAdapter(sql1, cn);
DataSet ds1 = new DataSet();
adpt1.Fill(ds1, "title");
if (ds1.Tables["title"].Rows.Count > 0)
{
String query = "select max(song_id) from up_song;";
SqlDataAdapter adpt2= new SqlDataAdapter(query,cn);
DataSet ds2= new DataSet();
adpt2.Fill(ds2,"max");
//int m = ds.Tables["title"].Rows.IndexOf(ds1.Tables["title"].Rows[query]);
//int k = ds.Tables["title"].Rows.IndexOf(ds.Tables["title"].Rows[1]);
for (i = 0; i <= ds2.Tables["max"].Rows.Count; ++i)
{
try
{
hp[i] = new HyperLink();
hp[i].ID = "hp" + i;
hp[i].Text = ds1.Tables["title"].Rows[i].ItemArray[0].ToString();
hp[i].NavigateUrl = "Downloadpage.aspx";
hp[i].ForeColor = System.Drawing.Color.White;
Panel2.Controls.Add(hp[i]);
Panel2.Controls.Add(new LiteralControl("<br>"));
HttpCookie coo = new HttpCookie("song");
coo["sogtit"] = ds.Tables["title"].Rows[i].ItemArray[0].ToString();
Response.Cookies.Add(coo);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
cn.Close();
select top 6 title from up_song order by song_id desc

Categories

Resources