Click here to Skip to main content
15,883,747 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
Hi All,

I am selecting database in dropdownlist from first page and sending to the second page by session variable.I'm generating runtime connectionstring because i have multiple database. this time i'm able to retrieve the database. and execute stored procedure by c# code. when i click button 2nd time for query/SP.null value comes in session variable.
Please suggest me....

Thanks,
Rajnikant

What I have tried:

I tried Session,static variable
Posted
Updated 10-May-16 0:30am
Comments
Suvendu Shekhar Giri 10-May-16 4:21am    
As far I know, that's possible. Please share the relevant code so that we'll able to suggest you correctly.
OriginalGriff 10-May-16 4:43am    
And?
What have you tried?
Where are you stuck?
What happens that you didn't expect, or didn't happen that you did?
What help do you need?
Use the "Improve question" widget to edit your question and provide better information.
Member 10543687 10-May-16 5:02am    
Login.aspx.cs page
-------------------------
protected void btnSubmit_Click(object sender, EventArgs e)
{
ConnectionInnovator.innovatorSelected_DB = ddlinnovatorDB.SelectedItem.Value.ToString();
Session["DB"] = ddlinnovatorDB.SelectedItem.Value.ToString();
// Session["ddlValue"] = innovatorSelected_DB;
string user = txtUsername.Value;
string pass = txtPassword.Value;

Item login_result = null;
HttpServerConnection conn;

try
{

}
catch (Exception)
{
lblStatus.Text = "Connection failed at all, Check Username/Password.";
lblStatus.ForeColor = System.Drawing.Color.Red;
}

if (login_result.isError())
{
lblStatus.Text = "Connection failed, Check Settings and Login data.";
lblStatus.ForeColor = System.Drawing.Color.Red;
}
else
{

// Response.Cookies["ConnectionCookies"].Value = ConnectionInnovator.conStr;
// Response.Cookies["ConnectionCookies"].Expires = DateTime.Now.AddDays(1);
// Session["connectionStr"]=AddUpdateConnectionString("con");
Response.Redirect("~/Default.aspx");
}

}

below is second page
Default.aspx.cs
---------------------
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Session["DB"] = innovatorDBCaught;


// lblResult.Text = "You have choosed \"" + Session["DB"].ToString() + "\" database.";
// lblResult.Text = "You have choosed \"" + ConnectionInnovator.innovatorSelected_DB.ToString() + "\" database.";
lblResult.BackColor = System.Drawing.Color.Cyan;
lblResult.ForeColor = System.Drawing.Color.DarkBlue;
lblSearch.Text = "Input Searching Keyword : ";
ViewState["conDB"] = Session["DB"].ToString(); // Session["DB"] has null value when click search button second time
// i tried in outside from condition also.
// conStr = Session["connectionStr"].ToString();
// ConnectionInnovator.conStr = AddUpdateConnectionString("con");
}

// conStr = Request.Cookies["ConnectionCookies"].Value.ToString();
}


protected void btnSearch_Click(object sender, EventArgs e)
{


ConnectionInnovator.conStr = AddUpdateConnectionString("con");
BindInnovator();
}



protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
//GridView1.DataSource = dt;
//GridView1.DataBind();

BindInnovator();
}

private void BindInnovator()
{

using (SqlConnection scon = new SqlConnection(ConnectionInnovator.conStr))
{
if (scon.State == ConnectionState.Closed)
scon.Open();
using (SqlCommand scmd = new SqlCommand("waitfor delay '00:00:30';select * from Shippers",scon))
{
scmd.CommandTimeout = 300;
scmd.CommandType = CommandType.StoredProcedure;
scmd.CommandText = "SearchAllTables";
scmd.Parameters.Add("@SearchStr", SqlDbType.NVarChar).Value = txtSearchBox.Text.Trim();
scmd.Connection = scon;
using (SqlDataAdapter sda = new SqlDataAdapter(scmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
scon.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}

}
public string AddUpdateConnectionString(string name)
{
bool isNew = false;
string path = Server.MapPath("~/Web.Config");
XmlDocument doc = new XmlDocument();
doc.Load(path);
OriginalGriff 10-May-16 5:18am    
Dumping your code with no explanation is not helpful. I repeat:

And?
What have you tried?
Where are you stuck?
What happens that you didn't expect, or didn't happen that you did?
What help do you need?
Use the "Improve question" widget to edit your question and provide better information.
Member 10543687 10-May-16 5:57am    
I want to search a column record in whole database because user don't know this column belongs to which column of table.
user will choose database from dropdownlist.
to do this i have created a login form called loginfrm.aspx where dropdownlist for database, userid and password is available.
after successful login redirects to the next page(default.aspx) where one textbox, one button for search and one gridview is available.
when default.aspx loads then we entered any values in textbox. it searched and gives output. but when we entered another values related to database. and clicks the button that time database name (selected from dropdownlist) goes missing. that session becomes null values (object doesnot support any value......) exception comes

Session value does not get unless you call
C#
Session.clear(); //[it just clear value not make it null]
Session.Abandon();
Session["sessionname"] = null;

Please share your code so that we can help you better in order to resolve your issue
 
Share this answer
 
Comments
Member 10543687 10-May-16 6:23am    
how to make session alive in any circumstance.
koolprasad2003 10-May-16 6:28am    
I have just gone through your code, why you store your value in view state again ? session variable is accessible everywhere.
what is your search button code ?
Member 10543687 10-May-16 6:46am    
sir, I tried in every probability. In search button two mehod is calling first public string AddUpdateConnectionString(string name): this is making connectionstring in runtime and BindInnovator(): this is executing stored procedure and displaying in gridview .
Pls go through the method of btnsearch.

protected void btnSearch_Click(object sender, EventArgs e)
{


ConnectionInnovator.conStr = AddUpdateConnectionString("con");
BindInnovator();
}
public string AddUpdateConnectionString(string name)
{
bool isNew = false;
string path = Server.MapPath("~/Web.Config");
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList list = doc.DocumentElement.SelectNodes(string.Format("connectionStrings/add[@name='{0}']", name));
XmlNode node;
isNew = list.Count == 0;
if (isNew)
{
node = doc.CreateNode(XmlNodeType.Element, "add", null);
XmlAttribute attribute = doc.CreateAttribute("name");
attribute.Value = name;
node.Attributes.Append(attribute);

attribute = doc.CreateAttribute("connectionString");
attribute.Value = "";
node.Attributes.Append(attribute);

attribute = doc.CreateAttribute("providerName");
attribute.Value = "System.Data.SqlClient";
node.Attributes.Append(attribute);
}
else
{
node = list[0];
}
string conString = node.Attributes["connectionString"].Value;
SqlConnectionStringBuilder conStringBuilder = new SqlConnectionStringBuilder(conString);
conStringBuilder.InitialCatalog = ConnectionInnovator.innovatorSelected_DB;// Session["ddlValue"].ToString();
// conStringBuilder.InitialCatalog = ViewState["conDB"].ToString();
conStringBuilder.DataSource = "ADMIN-HP\\SQLEXPRESS";
conStringBuilder.IntegratedSecurity = false;
conStringBuilder.UserID = "sa";
conStringBuilder.Password = "admin@123";
node.Attributes["connectionString"].Value = conStringBuilder.ConnectionString;
if (isNew)
{
doc.DocumentElement.SelectNodes("connectionStrings")[0].AppendChild(node);
}
doc.Save(path);
return conStringBuilder.ConnectionString.ToString();

}


private void BindInnovator()
{

using (SqlConnection scon = new SqlConnection(ConnectionInnovator.conStr))
{
if (scon.State == ConnectionState.Closed)
scon.Open();
using (SqlCommand scmd = new SqlCommand("waitfor delay '00:00:30';select * from Shippers",scon))
{
scmd.CommandTimeout = 300;
scmd.CommandType = CommandType.StoredProcedure;
scmd.CommandText = "SearchAllTables";
scmd.Parameters.Add("@SearchStr", SqlDbType.NVarChar).Value = txtSearchBox.Text.Trim();
scmd.Connection = scon;
using (SqlDataAdapter sda = new SqlDataAdapter(scmd))
{
DataTable dt = new DataTable();
sda.Fill(dt);
scon.Close();
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}

}
Member 10543687 10-May-16 8:13am    
when i click a button its identifying a new request that's why session is killing
koolprasad2003 10-May-16 8:17am    
According to page life cycle of asp.net, on each post back, page is destroyed and re-created again so on button your page_loads gets called. But there is should be any issue of session loss as it is a serverside state management technique.
create public static class in firstpage ,like this
C#
public static class logininfo
       {
           public static string userid;
       }
and than use get the data
public string send
       {
           get
           {
               return logininfo.userid;
           }
       }

and use that data in secondpage
 
Share this answer
 
Comments
Member 10543687 10-May-16 6:21am    
did not work again value is null
use this in second page
C#
private login pay;

and this used in rertive method
C#
var pay = new login();
            pay.Show();

like this
C#
label1.Text = "welcome '" + pay.send + "'";
 
Share this answer
 
v2
Comments
Member 10543687 10-May-16 6:37am    
i'm selecting database from dropdown from first page and reading in second page. it's not a variable or any method
Nagarjuna99 10-May-16 6:41am    
ya that select item will temprerly move on this in page one-->>
logininfo.userid=dropdownlist.selecteditem;
Member 10543687 10-May-16 8:14am    
when i click a button its identifying a new request that's why session is killing. how to keep alive session in every scenarios

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900