hi friends ,
can i use session value two times becouse at sexond time when i used session i get null value
plz help me
my code is
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//imgExport.Visible = false;
string ID = Request.QueryString["0"];
txtqueryexec.Text = "";
string LoginId = Convert.ToString(Session["LoginID"]);
if (LoginId !="")
{
FillGrid();
}
}
Session["DrdQury"] = drpdwn_query.SelectedItem.Text.ToString();
Session["TxtQuery"] = txtqueryexec.Text.Trim();
}
protected void btnexec_Click(object sender, EventArgs e)
{
}
private void FillGrid()
{
DataSet ds;
string drd = Convert.ToString(Session["DrdQury"]); //my first session
string txt = Convert.ToString(Session["TxtQuery"]);
if (!String.IsNullOrEmpty(Convert.ToString(Session["DrdQury"])) && !String.IsNullOrEmpty(Convert.ToString(Session["TxtQuery"])))
{
ds = objclient.get_queryexec(drd, txt);
//Session["DrdQury"] = "null"; Session["TxtQuery"] = "null";
}
else
{
ds = objclient.get_queryexec(drpdwn_query.SelectedItem.Text, txtqueryexec.Text);
}
if (ds.Tables[0].Rows.Count > 0)
{
gv_command.Visible = true;
gv_command.DataSource = ds;
gv_command.DataBind();
}
}
protected void drpdwn_query_SelectedIndexChanged(object sender, EventArgs e)
{
string query = drpdwn_query.SelectedItem.Text;
switch(query)
{
case "SELECT":
txtqueryexec.Text = "SELECT * FROM [TABLENAME]";
break;
case "INSERT":
txtqueryexec.Text = "INSERT INTO [TABLENAME] [(col1, col2, col3,...colN)] VALUES (value1, value2, value3,...valueN)";
break;
case "UPDATE":
txtqueryexec.Text = "UPDATE [TABLENAME] SET column_1 = [value1], column_2 = [value2]";
break;
}
}
protected void imgExport_Click(object sender, ImageClickEventArgs e)
{
string strPath = Server.MapPath(Request.ApplicationPath);
strPath = strPath + "\\ExportedFiles\\SchemeRating.xls";
string drd = Convert.ToString(Session["DrdQury"]); //my second session but here i got null value
string txt = Convert.ToString(Session["TxtQuery"]);
DataSet ds = objclient.get_queryexec(drd, txt);
exportToExcelSlow(ds.Tables[0], strPath);
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.ClearContent();
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", "attachment; filename=SchemeRating.xls");
Response.WriteFile(strPath);
Response.End();
Response.Flush();
}
Loading

Zoran HorvatPosted Jul 22, 2011, 7:41 AM
Session["DrdQury"] = drpdwn_query.SelectedItem.Text.ToString();
Session["TxtQuery"] = txtqueryexec.Text.Trim();
Method FillGrid() is invoked before these lines are executed, so at "// my first session" statement you still have non-null non-empty values in session variables. However, after this code completes in the Page_Load() method, session variables will remain empty or null. Hence, when imgExport_Click is executed, they will still be empty.
Note that drpdwn_query_SelectedIndexChanged() does not contain something like: Session["DrdQury"] = drpdwn_query.SelectedItem.Text;
Similarly, you might handle the txtqueryexec.TextChanged event to set: Session["TxtQuery"] = txtqueryexec.Text;
Zoran
Zoran HorvatPosted Jul 22, 2011, 8:32 AM
Please don't forget to check "This is correct answer" checkbox on the first post which answered your question correctly.
Thanks,
Zoran
sagar BhosalePosted Jul 22, 2011, 8:27 AM
event
Session["DrdQury"] = drpdwn_query.SelectedItem.Text.ToString();
So fill this session value in dropdown SelectedIndex Change event
&
txtqueryexec.TextChanged event to set: Session["TxtQuery"] = txtqueryexec.Text;
i got solution thanx all of u for ur valuable help
God blase u
Ashish DoshiPosted Jul 22, 2011, 8:09 AM
You will have to initialize the Session Variables in the Session_OnStart event in the global.asax file
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Session["DrdQury"] = "";
Session["TxtQuery"] = "";
}
This causes the variables (Key Value Pair) to be created in the Session Object. Post which you can use the same.
Hope that works.
Jaganathan BantheswaranPosted Jul 22, 2011, 8:02 AM
Then put a break point on page load and debug it to check where the session variables get null.
Blocked AccountPosted Jul 22, 2011, 8:01 AM
you are assigning Session value with the dropdown selected value. I think in page load no dropdown value be selected, so your Session value is null and when you use it second time it's value is still null.
Session["DrdQury"] = drpdwn_query.SelectedItem.Text.ToString();
So fill this session value in dropdown SelectedIndex Change event instead of Page_Load
Zoran HorvatPosted Jul 22, 2011, 7:57 AM
Session["DrdQury"] = drpdwn_query.SelectedItem.Text.ToString();
Session["TxtQuery"] = txtqueryexec.Text.Trim();
If you still need those values set according to drpdwn_query and txtqueryexec, then implement that functionality better, as in this place it doesn't work because both Text properties seem to be empty strings in Page_Load function.
Zoran
Jaganathan BantheswaranPosted Jul 22, 2011, 7:50 AM
Session["DrdQury"] = drpdwn_query.SelectedItem.Text.ToString();
Session["TxtQuery"] = txtqueryexec.Text.Trim();
Add this,
Session["DrdQury"] = drpdwn_query.SelectedItem.Text.ToString() != null ? drpdwn_query.SelectedItem.Text.ToString() : Session["DrdQury"].ToString();
Session["TxtQuery"] = txtqueryexec.Text.Trim() != null ? txtqueryexec.Text.Trim() : Session["TxtQuery"].ToString() ;
Hope it will solve your problem.
sagar BhosalePosted Jul 22, 2011, 7:36 AM
first session i used for display data in gridview & second session is used for display data in Excel sheet when i click on image button
but still i got null value on my second session
plz help
Jaganathan BantheswaranPosted Jul 22, 2011, 7:13 AM
On Page load event you are calling FillGrid method.
In FillGrid,
if (!String.IsNullOrEmpty(Convert.ToString(Session["DrdQury"])) && !String.IsNullOrEmpty(Convert.ToString(Session["TxtQuery"])))
{
ds = objclient.get_queryexec(drd, txt);
Session["DrdQury"] = "null"; Session["TxtQuery"] = "null";
}
Can you tell me you are assiging null string value here.
Zoran HorvatPosted Jul 22, 2011, 7:09 AM
Session["DrdQury"] = "null"; Session["TxtQuery"] = "null";
This statement executes if they already contained non-null and non-empty string, which looks to be always true.
Maybe the problem is in negation you used in the if statement:
if (!String.IsNullOrEmpty(Convert.ToString(Session["DrdQury"])) && !String.IsNullOrEmpty(Convert.ToString(Session["TxtQuery"])))
I am not sure what was the logic behind this, but maybe correct logic is this:
if (String.IsNullOrEmpty(Convert.ToString(Session["DrdQury"])) && String.IsNullOrEmpty(Convert.ToString(Session["TxtQuery"])))
The code looks a bit confusing...
Zoran