Click here to Skip to main content
16,004,653 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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
Posted
Updated 15-Dec-14 23:34pm
v3
Comments
Sinisa Hajnal 16-Dec-14 5:09am    
First: writing with ALL CAPS on the web means you're SHOUTING. Please don't do that.
Second: post only the code relevant to the error.
See the solution the explanation of your code.
ritesh3133 16-Dec-14 5:15am    
sorry bro i am new to the site
Sinisa Hajnal 16-Dec-14 7:03am    
No problem, but it is fairly universal...anywhere on web (or even e-mail) if you write in all caps it is considered shouting :) Learn and forget this happened.

1 solution

You have this in your click handler:
VB
Dim f1 As New Form1
       Dim S As String
       S = f1.t



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
VB
Shared emp_no As String = ""
 Shared emp_adhaar As String = ""

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.
 
Share this answer
 
Comments
ritesh3133 16-Dec-14 5:46am    
thnx bro i am getting the clue now .....you have suggested the to use shared property which here i was trying with t but it did not work...if possible can you plz elaborate with example in current context.
Sinisa Hajnal 16-Dec-14 7:02am    
I also suggested to REMOVE shared from the variables :)
Sinisa Hajnal 16-Dec-14 7:05am    
As for shared property, just add Shared in front of property name, just like the variable.
ritesh3133 16-Dec-14 7:58am    
i have changed my code as per your suggestion by using shared property but still it is giving the same error .....
Sinisa Hajnal 16-Dec-14 8:37am    
Here: SELECT * FROM emp_personal_details where emp_no= s <-- this "s" doesn't exists - you cannot just add your variables into query string...either use parametrized query (google this) or add directly into the string (use String.Format) - this direct concatenation is BAD practice, but possible.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900