简体   繁体   中英

SqlDataource based on sessionID

I have the following code that i am trying to use in order to create dynamic gridview based on a sqldatasource. The issue i am having is that the sqldatasource.ID I am using based on the SessionID. Now when i call it on page_load everything seems to work fine, but if i refresh the page it doesn't work, now i know why and its because i have an if statement in page_load which checks to see if postback exists, and if not then it loads fine with the getDefaultGrid() function, but then if it is a postback, and i try reload the getDefaultGrid() function but this time trying to pass the sqldatasource ID it doesn't work, or maybe i'm just doing it wrong. Any help is very much appreciated!!

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        getDefaultGrid(getSessionID(), startGrid());
    }
    else
    {
        getDefaultGrid(getSessionID(), (SqlDataSource)getSessionID());
    }

}



protected void getDefaultGrid(string sessionID, SqlDataSource ds)
{
    ds.SelectCommand = "SELECT * FROM [temp] WHERE SessNum = @SESSNUM";
    ds.SelectParameters.Add("SESSNUM", sessionID);

    GridView1.DataSource = ds;
    GridView1.DataBind();
}


protected SqlDataSource startGrid() 
{
    string ConnString = ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString;

    String currentSessionID = getSessionID();
    SqlDataSource sqlDS = new SqlDataSource();
    //sqlDS.ConnectionString = "unionConnectionString";
    sqlDS.ConnectionString = ConnString;
    sqlDS.ID = getSessionID();
    sqlDS.InsertCommand = "INSERT INTO [temp] ([SessNum], [Row], [Size], [Description], [Quantity], [Unit], [Duration], [DurationType], [Amount])VALUES (@SESSNUM, @ROW, @SIZE, @DESCRIPTION, @QUANTITY, @UNIT, @DURATION, @DURATIONTYPE, @AMOUNT)";
    sqlDS.InsertParameters.Add("SESSNUM", currentSessionID.ToString());
    sqlDS.InsertParameters.Add("ROW", "1");
    sqlDS.InsertParameters.Add("SIZE", "Size");
    sqlDS.InsertParameters.Add("DESCRIPTION", "Description");
    sqlDS.InsertParameters.Add("QUANTITY", "Quantity");
    sqlDS.InsertParameters.Add("UNIT", "Unit");
    sqlDS.InsertParameters.Add("DURATION", "Duration");
    sqlDS.InsertParameters.Add("DURATIONTYPE", "DurationType");
    sqlDS.InsertParameters.Add("AMOUNT", "Amount");
    sqlDS.Insert();
    return sqlDS;
}

protected string getSessionID()
{
    string session = HttpContext.Current.Session.SessionID;
    return session;
}

Create a new Instance of SQLDATASOURCE and the pass the the session id's to them. Similar type of questions are asked already you can simply go through them. First check the page where you store you session id and then move forward step by step. use break-points.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM