简体   繁体   English

基于sessionID的SqlDataource

[英]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. 我尝试使用以下代码,以便基于sqldatasource创建动态gridview。 The issue i am having is that the sqldatasource.ID I am using based on the SessionID. 我遇到的问题是基于SessionID使用的sqldatasource.ID。 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. 现在,当我在page_load上调用它时,一切似乎都可以正常工作,但是如果刷新页面则无法正常工作,现在我知道原因及其原因,因为我在page_load中有一个if语句,用于检查回发是否存在,是否不存在然后使用getDefaultGrid()函数可以很好地加载它,但是如果它是回发的,我尝试重新加载getDefaultGrid()函数,但是这次尝试传递sqldatasource ID,它不起作用,或者也许我只是在做错了。 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. 创建一个新的SQLDATASOURCE实例, SQLDATASOURCE会话ID传递给它们。 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. 首先检查您存储会话ID的页面,然后逐步进行操作。 use break-points. 使用断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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