简体   繁体   English

从其他文件向数据表添加行

[英]Adding rows in data table from a different file

I have created a data table of four columns in my global.asax file. 我在global.asax文件中创建了一个包含四列的数据表。 I am adding rows only to two columns of the data table in the global.asax file. 我仅将行添加到global.asax文件中的数据表的两列中。 I am trying to add the other two rows from a different .aspx.cs file which is under the same project. 我试图从同一项目下的不同.aspx.cs文件中添加其他两行。 When I try to add the row, it gives me an error 当我尝试添加行时,它给我一个错误

"the name "dr" does not exists in the current context". “名称“ dr”在当前上下文中不存在”。

My code: 我的代码:

login.aspx.cs: login.aspx.cs:

protected void btnLogin_Click(object sender, EventArgs e)
{
    if(txtPassword.Text == "")
    {            
        Server.Transfer("Main.aspx", true);            
    }

    if(txtUserName.Text!= "" && txtPassword.Text!= "")
    {
        Server.Transfer("Userlog.aspx", true);
    }


    dr["username"] = Session["UserName"]; // username
    dr["login_time"] = Session["LoginTime"]; //login time
}

global.asax: global.asax:

void Session_Start(Object s, EventArgs e)
{

    Application.Lock();

    dt = new DataTable();
    dt.Columns.Add(new DataColumn("session_id", System.Type.GetType("System.String")));
    dt.Columns.Add(new DataColumn("username",   System.Type.GetType("System.String")));
    dt.Columns.Add(new DataColumn("login_time", System.Type.GetType("System.DateTime")));
    dt.Columns.Add(new DataColumn("ip_address", System.Type.GetType("System.String")));

    Application["visitorTable"] = dt;

    DataRow dr = dt.NewRow();

    dr["session_id"] = (System.String)Session.SessionID; // session id
    dr["ip_address"] = Request.ServerVariables["SERVER_NAME"]; //ip-address

    dt.Rows.Add(dr);

    //dt = (DataTable)Application["visitorTable"];

    Application["visitorTable"] = dt;

    DataView view = new DataView(dt);
    Application.UnLock();

}

It will through since dr variable doesnt exists. 由于dr变量不存在,它将通过。 If you want the datatable row in session object then store the datatable row with some key word and then retrieve it. 如果要在会话对象中使用数据表行,则将数据表行与某些关键字一起存储,然后进行检索。

Session["RowDataTable"] = dr;

Then login page 然后登录页面

var dr = Session["RowDataTable"] as DataRow;

Hmm don't realy know where to start: 嗯,真的不知道从哪里开始:

Firstly your defined variables dt and dr are only available in the scope of the method Session_Start(){}. 首先,您定义的变量dt和dr仅在Session_Start(){}方法的范围内可用。 That means, after the method-call, the both variables are not available anymore. 这意味着,在方法调用之后,两个变量都不再可用。

Secondary, if you need to access your Data also outside of that method, you should store it in an private variable: 次要的,如果您还需要在该方法之外访问数据,则应将其存储在私有变量中:

private DataTable _myDataTable;

or in a public Propertiy to access it from outside the current class: 或在公共场所从当前课程之外访问它:

public DataTable MyDataTable { get; set }

You should also inform you about the static modifier: 您还应该告知您有关static修饰符的信息:

https://msdn.microsoft.com/de-de/library/98f28cdx.aspx https://msdn.microsoft.com/de-de/library/98f28cdx.aspx

Hope that helps you a little bit. 希望对您有所帮助。

And for that what you want in the current situation, the answer of Mahesh Malpani maybe leads you to. 在当前情况下您想要的是, Mahesh Malpani的答案可能会带您进入。

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

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