Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 3.5 > ASP.NET 3.5 Professionals
| Search | Today's Posts | Mark Forums Read
ASP.NET 3.5 Professionals If you are an experienced ASP.NET programmer, this is the forum for your 3.5 questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Professionals section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old August 26th, 2009, 10:47 AM
Authorized User
 
Join Date: Jun 2009
Posts: 66
Thanks: 22
Thanked 0 Times in 0 Posts
Default If Else Condition not working inside the submittButton_Click function/method

Hello,
I want submit button to fetch different query results from the database. So I have implemented which query to execute inside the SubmitButton_Click function/method. But it's not working as I have desired....it's fetching one query every time.

To give you a brief idea, in my .aspx I have 2 dropdown lists (1 to select which query to run and another one to select which patient number should I pull from the DB), 1 text box which contains date field. I have kinda found what's causing the problem....but don't know how to solve it. Somehow from the drop down list "ANY" is selected all the times.....no matter what I select from the second dropdown list. Any suggestion would be really helpful.

The code is as follows:



using System;
using System.Collections;
using System.ComponentModel;
using System.Configuration;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;

namespace HIV
{
/// <summary>
/// Summary description for run_specific_query.
/// </summary>
public class run_specific_query : System.Web.UI.Page
{
protected HIV.Controls.NavMenu navMenu;
protected HIV.Controls.NavSubMenu navSubMenu;
protected System.Web.UI.HtmlControls.HtmlGenericControl message;
protected System.Web.UI.WebControls.Label resultsLabel;
protected System.Web.UI.WebControls.DropDownList Specific_Query_DDL;
protected System.Web.UI.WebControls.DropDownList mlno_DDL;
protected System.Web.UI.WebControls.TextBox date_TB;
protected System.Web.UI.WebControls.CompareValidator dateValidator;
protected System.Web.UI.WebControls.DataGrid resultsDatagrid;
protected System.Web.UI.WebControls.Button submitButton;
protected System.Web.UI.WebControls.Label data_src;
protected System.Web.UI.WebControls.LinkButton exportLinkbutton;

private void Page_Load(object sender, System.EventArgs e)
{
navMenu.SelectedMainItem = HIV.Controls.NavMenu.MainItems.QUERY;
navSubMenu.SelectedMainItem = HIV.Controls.NavMenu.MainItems.QUERY;
navSubMenu.SelectedSubItem = HIV.Controls.NavSubMenu.SubItems.RUN_SPECIFIC_QUER Y;

getMLNO();

//if (!this.IsPostBack)
//LoadQueryLinks();
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
BuildQueryForm(); [This function/method is calling the submit button function]
InitializeComponent();

base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//this.submitButton.Click += new System.EventHandler(this.submitButton_Click);
//this.Load += new System.EventHandler(this.Page_Load);
this.exportLinkbutton.Click += new System.EventHandler(this.exportLinkbutton_Click);
this.Load += new System.EventHandler(this.Page_Load); [after the data is pulled from the DB the export to excel button should show up, so I have implemented this here]

}
#endregion

protected void BuildQueryForm()
{
try
{
this.submitButton.Click += new System.EventHandler(this.submitButton_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
catch(Exception e)
{
message.InnerHtml = "Exception: " + e.ToString();
}
}

private void getMLNO()
{
OleDbConnection connection = new OleDbConnection(HIV.Database.DataConstants.CONNECT ION_STRING);
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand("SELECT DISTINCT re_MLNO FROM ml_hiv_status", connection);

DataSet ds = new DataSet();
adapter.SelectCommand = command;

if (adapter.Fill(ds) > 0)
{
int adp= adapter.Fill(ds);
DataView view = ds.Tables[0].DefaultView;
mlno_DDL.DataSource = view;
mlno_DDL.DataValueField="re_MLNO";
mlno_DDL.DataBind();
resultsLabel.Visible= true;
//resultsLabel.Text ="TEST MESSAGE: You Have selected the DB "+ adp + Specific_Query_DDL.SelectedItem.Value;
mlno_DDL.Items.Insert(0, "ANY");
}
connection.Close();
}

private void submitButton_Click(object sender, System.EventArgs e)
{
if(Specific_Query_DDL.SelectedItem.Value=="default ")
{
resultsLabel.Visible= true;
resultsLabel.Text ="Please select a specific query from the dropdown list";
exportLinkbutton.Visible = false;
}
if(Specific_Query_DDL.SelectedItem.Value=="Resista ntL")
{
resultsLabel.Visible= true;
resultsLabel.Text ="You Have selected"+ Specific_Query_DDL.SelectedItem.Value;
}
if(Specific_Query_DDL.SelectedItem.Value=="Negativ eL")
{
resultsLabel.Visible= true;
resultsLabel.Text ="You Have selected"+ Specific_Query_DDL.SelectedItem.Value;
}
if(Specific_Query_DDL.SelectedItem.Value=="Positiv eL") [The problem is here in this if else block]
{
if(mlno_DDL.SelectedItem.Value=="ANY")
{
executePositiveListQuery_ANY(); [All the time this one is called but...]
}
else //if(mlno_DDL.SelectedItem.Value!="ANY")
{
executePositiveListQuery_MLNO(mlno_DDL.SelectedIte m.Value); [...I want this method to be called as well when I am selecting a specific patient number]
}
}
if(Specific_Query_DDL.SelectedItem.Value=="DeadL")
{
resultsLabel.Visible= true;
resultsLabel.Text ="You Have selected"+ Specific_Query_DDL.SelectedItem.Value;
}

}

public void executePositiveListQuery_ANY()
{
string sql= "SELECT DISTINCT re_MLNO FROM ml_hiv_status WHERE (re_HIV1_Status=1 AND re_HIV2_Status=1)";
try
{
OleDbConnection connection = new OleDbConnection(HIV.Database.DataConstants.CONNECT ION_STRING);
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand(sql, connection);

DataSet ds = new DataSet();
adapter.SelectCommand = command;

int count = adapter.Fill(ds);

if (count > 0 && count <= 200)
{
DataView view = ds.Tables[0].DefaultView;
//view.Sort=e.SortExpression;
resultsDatagrid.DataSource = view;
resultsDatagrid.DataBind();
resultsDatagrid.Visible = true;
resultsLabel.Text = count.ToString() + " results found.";
resultsLabel.Visible = true;
exportLinkbutton.Visible = true;
}
else if (count> 200)
{
DataView view = ds.Tables[0].DefaultView;
//view.Sort=e.SortExpression;
resultsDatagrid.DataSource = view;
resultsDatagrid.DataBind();
resultsDatagrid.Visible = false;
resultsLabel.Text = "More than 200 results found. Please Click the Export to Excel Link to Download the Results.";
resultsLabel.Visible = true;
exportLinkbutton.Visible = true;
}
else
{
resultsLabel.Text = "No results found.";
resultsLabel.Visible = true;
exportLinkbutton.Visible = false;
resultsDatagrid.Visible = false;
}
connection.Close();
}
catch(Exception ex)
{
message.InnerHtml = "SQL: " + sql + "<p></p>" + ex.ToString();
}

//resultsLabel.Text ="You Have selected"+ Specific_Query_DDL.SelectedItem.Value;
}

public void executePositiveListQuery_MLNO(string selected_MLNO)
{
/*string sql= "SELECT * FROM ml_hiv_status WHERE (re_HIV1_Status=1 AND re_HIV2_Status=1)";
try
{
OleDbConnection connection = new OleDbConnection(HIV.Database.DataConstants.CONNECT ION_STRING);
OleDbDataAdapter adapter = new OleDbDataAdapter();
OleDbCommand command = new OleDbCommand(sql, connection);

DataSet ds = new DataSet();
adapter.SelectCommand = command;

int count = adapter.Fill(ds);

if (count > 0 && count <= 200)
{
DataView view = ds.Tables[0].DefaultView;
//view.Sort=e.SortExpression;
resultsDatagrid.DataSource = view;
resultsDatagrid.DataBind();
resultsDatagrid.Visible = true;
resultsLabel.Text = count.ToString() + " results found.";
resultsLabel.Visible = true;
exportLinkbutton.Visible = true;
}
else if (count> 200)
{
DataView view = ds.Tables[0].DefaultView;
//view.Sort=e.SortExpression;
resultsDatagrid.DataSource = view;
resultsDatagrid.DataBind();
resultsDatagrid.Visible = false;
resultsLabel.Text = "More than 200 results found. Please Click the Export to Excel Link to Download the Results.";
resultsLabel.Visible = true;
exportLinkbutton.Visible = true;
}
else
{
resultsLabel.Text = "No results found.";
resultsLabel.Visible = true;
exportLinkbutton.Visible = false;
resultsDatagrid.Visible = false;
}
connection.Close();
}
catch(Exception ex)
{
message.InnerHtml = "SQL: " + sql + "<p></p>" + ex.ToString();
}*/

resultsLabel.Text ="You Have selected"+ selected_MLNO;
}

private void exportLinkbutton_Click(object sender, System.EventArgs e)
{
resultsDatagrid.Visible = true;
DataGridItem tblGrid=resultsDatagrid.Items[0];
ArrayList alLinks= new ArrayList();
TableCell TC;
LinkButton LB;

for(int i=0; i<tblGrid.Cells.Count - 1; i++)
{
TC=tblGrid.Cells[i];
if(TC.Controls.Count> 0)
{
LB=(LinkButton)TC.Controls[0];
resultsDatagrid.Items[0].Cells[i].Controls.Clear();
resultsDatagrid.Items[0].Cells[i].Text=LB.Text;
}
else
LB=new LinkButton();

alLinks.Add(LB);
}

Response.ContentType = "application/vnd.ms-excel";
Response.Charset = "";
resultsDatagrid.EnableViewState = false;

System.IO.StringWriter tw = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter hw = new System.Web.UI.HtmlTextWriter(tw);
resultsDatagrid.RenderControl(hw);
//TextWriter TW= File.CreateText(Server.MapPath("ExportToExcel.xls" ));

Response.Write(tw.ToString());
Response.End();

for(int i=0; i<tblGrid.Cells.Count - 1; i++)
{
LB = (LinkButton)alLinks[i];
//TC=tblGrid.Rows[0].Cells[i];
resultsDatagrid.Items[0].Cells[i].Controls.Add(LB);
}
}
}
}



Please help! Thanks to everyone who give their time and energy to solve this.

Last edited by skhan; August 26th, 2009 at 11:00 AM..
  #2 (permalink)  
Old August 26th, 2009, 02:30 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there.. I don't understand.. your problem is with your logic?? Or anything else prevents the code to reach it???

only one part of the if will be executed.. that's sure.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #3 (permalink)  
Old August 26th, 2009, 02:47 PM
Authorized User
 
Join Date: Jun 2009
Posts: 66
Thanks: 22
Thanked 0 Times in 0 Posts
Default

Thanks for the reply.

And about cross posting....I thought maybe if I post in another section then it'll bring attention to few more people...but maybe I was wrong. I need the solution by tomorrow, so I took the aggressive approach I guess. I am sorry for posting twice if that's confusing to people.

Anyways, my problem was I am populating the dropdown list from my Database. But when I click submit button after selecting any other value than 'ANY' from the dropdown list, it only takes 'ANY' value instead of taking the selected value and send query to the database and pulls all the rows as 'ANY' is selected all the times. But I need to select only the rows that I am selecting the patient number for.

Please let me know if anymore details is needed.
  #4 (permalink)  
Old August 26th, 2009, 03:51 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Well, you are always filling the combos in the init section of the page. So you are looking for the controls every time the page is loaded, and not when the page "reads" what the user has selected.
You are looking for the user choice but the page is not informing you that, because you are reading the value when you create the control, and not after the user change it.
You have to do what you want in the page load event, only when is postback.

I hope you understand me, probably Imar has a better way to explain it.
__________________
HTH

Gonzalo


================================================== =========
Read this if you want to know how to get a correct reply for your question.
(Took that from Doug signature and he Took that from Peter profile)
================================================== =========
My programs achieved a new certification :
WORKS ON MY MACHINE
================================================== =========
I know that CVS was evil, and now i got the
proof.
================================================== =========
  #5 (permalink)  
Old August 26th, 2009, 04:21 PM
Authorized User
 
Join Date: Jun 2009
Posts: 66
Thanks: 22
Thanked 0 Times in 0 Posts
Default

Thank you for your reply. I have solved it.....I was loading the MLNOs every time the page was loading, that was the problem. But now I have tucked the getMLNO() inside the if ! Post back condition and it helped. That way the selected list item remained selected whenever the page is post back and 'ANY' was out of my way :)

Thanks for helping, and thanks to Imar (who could have helped me as well ) :)


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to add " " inside a method({ }) and XSLT 7 July 14th, 2008 03:27 AM
Calling a method defined inside a .NET dll rajen C# 5 January 7th, 2008 04:04 AM
Calling a method defined inside a .NET dll rajen C# 2005 0 September 19th, 2006 12:58 AM
cannot 'seek' inside a function dextermagnific XSLT 2 August 10th, 2006 01:54 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.