在表单之间传递值 [英] Passing Value between forms

查看:69
本文介绍了在表单之间传递值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好专家。



登录表格代码:



Hello Experts .

Login Form Code :

con.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * From Admin Where EmployeeID = '" + LogId.Text.Trim() + "' AND Category = '" + CbCat.SelectedItem.ToString() + "' AND Designation = '" + CbDesignationAdmin.SelectedItem.ToString() + "' AND FinYear1 = '" + CbFinYr1.SelectedItem.ToString() + "' AND FinYear2 = '" + CbFinYr2.SelectedItem.ToString() + "' AND Emp_Password = '" + Password.Text.Trim() + "'  ", con);

OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
if (dt.Rows.Count == 0)
{
   MessageBox.Show("Wrong Login Id And Password ?");
   return;
}
else
{
   MessageBox.Show("Mr " + dt.Rows[0][4].ToString() + " :: You are Successfully Login ");
   MasterAdminForm maf = new MasterAdminForm();
   maf.Show();
   this.Hide();
}





新表单开放代码:



New Form Open Code :

con.Open();
OleDbCommand cmd = new OleDbCommand("SELECT * From Admin ", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
con.Close();
if (dt.Rows.Count != 0)
{
   label1.Text = "Welcom Mr :  " + dt.Rows[0][3].ToString();
   label1.BackColor = Color.Black;
   label1.ForeColor = Color.White;
}





这是AdminMasterForm中的问题我显示价值对于硬核价值



但是在New Form It Show问题对于胖子表值基于特定Id喜欢Admin。





Here is problem in AdminMasterForm I Show Value In For hard core value

But in New Form It Show problem For fatching table Value Onthe basic of specific Id Like Admin .

query = "SELECT * From Admin Where EmployeeID = '"+ LoginId.Text.ToString()+"' "



赞了这个



代码块添加和索引减少[/ edit]


Like This

[edit]Code block added and indexation reduced[/edit]

推荐答案

你好Rohit,



根据你上面的问题,你的确在做什么和发生了什么并不是太清楚了? br />


好​​的但是根据我的理解在LOG中,只需在会话var中输入ID,然后使用该var加载更多的页面或相应的数据。 />


,这里把你的错误放在堆栈跟踪上。



温馨的问候,

编码器。
Hi Rohit,

As per your above question here it is not too much clear point that what you are exactly doing and what happened?

Ok but up to my understanding On the LOG in form just put the entered log in ID in session var and use that var to load further pages or respective data.

and here put your error what you are facing with stack trace.

Warm Regards,
Coder.


您可以将用户名保存在静态变量中,以便您可以轻松地从下一个表单中访问它。



第二种形式声明静态属性

You can save the username in a static variable, so that you can access it from next form easily.

in the second form declare static Property
public static string LoginName {get; set;}



然后以登录形式点击按钮确定/登录点击事件


then in login form On button Ok /Login Click Event

form2.LoginName= LoginId.Text;



现在你可以用第二种形式写你的查询


Now you can write your query in second form as

query = "SELECT * From Admin Where EmployeeID = '"+ LoginName +"' "


由于您没有正确描述您的问题,我从您的主题得出了这样的结论:在表单之间传递数据。



无论你的问题是什么,都有 2路这样做(C#的基础知识) ):

1.)通过构造函数传递值

2.)制作一个 property (get; set;)并按特定表格的实例调用



这里我告诉你如何使用 ctor 传递数据。

第一表格

As you have not described your problem correctly, I came to this conclusion from your subject that you're passing the data between forms.

Regardless of your problem, there are 2 ways of doing that (basics of C#):
1.) Pass the values through constructor
2.) Make a property (get; set;) and call that by instance of the particular form

Here I'm showing you how to pass the data using ctor.
First Form
button_click(...)  // Button Click Event
{
    string userName = "admin";
    SecondForm sf = new SecondForm(userName);
    sf.Show();
}



第二表格


Second Form

string userName;
public SecondForm(string userName)   // Ctor of second form
{
    this.userName = userName;
    lbl.Text = userName;
}





-KR



-KR


这篇关于在表单之间传递值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆