如何在多个WINDOWS中分配可变值 [英] HOW TO ASSIGN VARIABLE VALUE IN MULTIPLE WINDOWS
问题描述
Private Sub Button1_Click_1(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button1.Click
Dim f1 As New Form1
Dim S As String
S = f1.t
Dim con As New SqlConnection
con.ConnectionString =server = hcl-pc\SQLEXPRESS; Initial Catalog = UBGB_HRMS; Integrated Security = True
Dim sql As String =SELECT * FROM emp_personal_details where emp_no = s
Dim dataadapter As New SqlDataAdapter(sql,con)
Dim ds作为新数据集()
con.Open()
dataadapter.Fill(ds,emp_personal_details)
con.Close()
f3.DataGridView1.DataSource = ds
f3.DataGridView1.DataMember =emp_personal_details
F3。显示()
结束次级
结束班级
FORM1的代码是
公共舱Form1
继承Windows.Forms.Form
Dim f2 As New Form2
共享emp_no As String =
共享emp_adhaar As String =
Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理Button1.Click
Dim con As New SqlConnection
con.ConnectionString =server = hcl- pc\SQLEXPRESS;初始目录= UBGB_HRMS;综合安全=真
con.Open()
Dim cmd作为SqlCommand
cmd =新SqlCommand(选择emp_no,来自emp_personal_details的emp_adhaar,其中emp_no ='+ TextBox1.Text +'和emp_adhaar =' + TextBox2.Text +',con)
emp_no = TextBox1.Text
emp_adhaar = TextBox2.Text
Dim da As New SqlDataAdapter
da = N ew SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
If(dt.Rows.Count> 0)然后
MessageBox.Show(登录成功欢迎使用UBGB HRMS)
f2.Show()
否则
MessageBox.Show(无效登录请检查用户名和密码)
结束如果
con.Close()
End Sub < br $>
公共财产t()为字符串
获取
返回emp_no
结束获取
套装(ByVal Value As String)
emp_no =价值
结束套件
结束物业>
结束类
这里在form1中我已经创建了一个登录窗口,在成功登录后它将指向form2并以t.in形式分配了looged用户的emp_no我试图在变量s中调用t但它不起作用。错误是无效的列名s
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim f1 As New Form1
Dim S As String
S = f1.t
Dim con As New SqlConnection
con.ConnectionString = "server=hcl-pc\SQLEXPRESS;Initial Catalog=UBGB_HRMS;Integrated Security=True"
Dim sql As String = "SELECT * FROM emp_personal_details where emp_no= s"
Dim dataadapter As New SqlDataAdapter(sql, con)
Dim ds As New DataSet()
con.Open()
dataadapter.Fill(ds, "emp_personal_details")
con.Close()
f3.DataGridView1.DataSource = ds
f3.DataGridView1.DataMember = "emp_personal_details"
F3.Show()
End Sub
End Class
THE CODE FOR FORM1 IS
Public Class Form1
Inherits Windows.Forms.Form
Dim f2 As New Form2
Shared emp_no As String = ""
Shared emp_adhaar As String = ""
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim con As New SqlConnection
con.ConnectionString = "server=hcl-pc\SQLEXPRESS;Initial Catalog=UBGB_HRMS;Integrated Security=True"
con.Open()
Dim cmd As SqlCommand
cmd = New SqlCommand("select emp_no,emp_adhaar from emp_personal_details where emp_no='" + TextBox1.Text + "'and emp_adhaar='" + TextBox2.Text + "'", con)
emp_no = TextBox1.Text
emp_adhaar = TextBox2.Text
Dim da As New SqlDataAdapter
da = New SqlDataAdapter(cmd)
Dim dt As New DataTable
da.Fill(dt)
If (dt.Rows.Count > 0) Then
MessageBox.Show("Login sucess Welcome to UBGB HRMS ")
f2.Show()
Else
MessageBox.Show("Invalid Login please check username and password")
End If
con.Close()
End Sub
Public Property t() As String
Get
Return emp_no
End Get
Set(ByVal Value As String)
emp_no = Value
End Set
End Property
End Class
here in form1 i have created a log in window after the successful log in it will direct to form2 and have assigned the emp_no of looged user in t.in form i have tried to call t in variable s but it is not working. error is invalid column name s
推荐答案
你的点击处理程序中有这个:
You have this in your click handler:
Dim f1 As New Form1
Dim S As String
S = f1.t
读作:
创建新对象类型Form1
创建字符串变量S
分配给该变量值t
t当前为空字符串。
为什么?
因为您正在创建新的form1,所以在构造函数中您没有更改该变量并且它已设置为
which reads as:
create new object of type Form1
create string variable S
Assign to that variable value of t
t is currently empty string.
WHY?
Because you're creating new form1, in the constructor you're not changing that variable and it is set
Shared emp_no As String = ""
Shared emp_adhaar As String = ""
只有在单击按钮并访问数据库时才会更改。点击之后,你会有一些价值,但不会像你写的那样。
另外,如果你使用共享变量,为什么不通过共享属性访问它们?这样你就不必拥有Form1实例,你只需要写Form1.t
如果我是你,我会从emp_no中删除Shared,重命名表单,以便名称具有意义(frmLogin,frmEmployees,frmEmployee等...),对于控件(btnLogin等)也是如此,也许编写Employee类,它将从表单传递给表单而不是仅仅为了引用其他表单数据访问。
我希望这会有所帮助。
This will change only when you click on your button and access the database. AFTER that click, you would have some value, but not as you have written it.
Also, if you use Shared variables, why not access them through shared property? That way you wouldn't have to have Form1 instance, you'd just write Form1.t
If I were you, I'd remove Shared from emp_no, rename the forms so that names have a meaning (frmLogin, frmEmployees, frmEmployee etc...), same for controls (btnLogin etc), maybe write Employee class which would be passed around from form to form instead of having references to other forms just for data access.
I hope this helps.
这篇关于如何在多个WINDOWS中分配可变值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!