Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 149,913 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 2,188 people online right now. Registration is fast and FREE... Join Now!




Help with login code

 
Reply to this topicStart new topic

Help with login code, logineform in vb.net2005 sql2000

flodi
26 Jan, 2008 - 08:17 AM
Post #1

New D.I.C Head
*

Joined: 20 Jan, 2008
Posts: 10

hi every body
can any one tell me why this code give me error thats said [ An SqlParameter with ParameterName 'tech_user_name' is not contained by this SqlParameterCollection ]
where is my mistak please
CODE

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim str As String
        Dim SqlCommand2 As New SqlCommand
        str = "LoginSP1"
        With SqlCommand2
            .CommandType = CommandType.StoredProcedure
            .CommandText = str
            .Parameters.AddWithValue("@tech_user_name", UsernameTextBox.Text)
            .Parameters.AddWithValue("@tech_password", PasswordTextBox.Text)
            .Connection = SqlConnection1
        End With
        SqlConnection1.Open()
        Dim valid As Integer = SqlCommand2.ExecuteScalar
        If valid < 1 Then
            'invalid login
            MsgBox("wrong user name or password", MsgBoxStyle.OkOnly)
            UsernameTextBox.Text = ""
            PasswordTextBox.Text = ""
            UsernameTextBox.Focus()
            SqlConnection1.Close()
            SqlCommand2.Parameters.Clear()
        Else
            'valid login
            OK.Enabled = False
            Cancel.Enabled = False
            UsernameLabel.Enabled = False
            PasswordLabel.Enabled = False
            MenuStrip1.Enabled = True
            PasswordTextBox.Enabled = False
            username = UsernameTextBox.Text
            Label1.Text = username
            SqlConnection1.Close()
            SqlCommand2.Parameters.Clear()
        End If
        SqlConnection1.Open()
        SqlCommand1.Parameters("tech_user_name").Value = UsernameTextBox.Text
        Dim read As SqlClient.SqlDataReader
        read = SqlCommand1.ExecuteReader
        If read.Read Then
            TextBox1.Text = read.Item("user_type")
            read.Close()
            SqlConnection1.Close()
            SqlCommand1.Parameters.Clear()
        End If
    End Sub


i use stored procedure
CODE

ALTER PROCEDURE dbo.LoginSP1
    @tech_user_name nvarchar (50) ,
    @tech_password nvarchar (50)
        AS    select count(tech_user_name) from client
    where tech_user_name=@tech_user_name and tech_password=@tech_password
        RETURN

so i call it then i would like to search by the username text to find the user type Admin or User

User is offlineProfile CardPM
+Quote Post

B-Boy209
RE: Help With Login Code
26 Jan, 2008 - 10:32 AM
Post #2

D.I.C Head
**

Joined: 9 Dec, 2007
Posts: 61


My Contributions
i dont know much about this in fact i dont know anything about it...but

[ An SqlParameter with ParameterName((('tech_user_name')))is not contained by this SqlParameterCollection ]

shows that around tech_user_name has ( ' ' ) not quotations( " " ) like in your code...dont know if that matters or has anything to do with it but to me thats what stood out...im probably wrong...but had to give my opinion...sorry if im wrong...


oh this part is what im talking about...

SqlCommand1.Parameters("tech_user_name").Value = UsernameTextBox.Text

This post has been edited by B-Boy209: 26 Jan, 2008 - 10:34 AM
User is offlineProfile CardPM
+Quote Post

flodi
RE: Help With Login Code
26 Jan, 2008 - 02:43 PM
Post #3

New D.I.C Head
*

Joined: 20 Jan, 2008
Posts: 10

this is the correct answer
SqlCommand1.Parameters("@tech_user_name").Value = UsernameTextBox.Text
thanks any way
User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Help With Login Code
26 Jan, 2008 - 10:41 PM
Post #4

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 9,483



Thanked: 161 times
Dream Kudos: 9075
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
You must be using the drag & drop SqlCommand Object, because if it's created in code then your original syntax was correct:

CODE

Parameters.AddWithValue("@tech_user_name", UsernameTextBox.Text)



Is the correct syntax, but not for the Drag & Drop control, which doesn't make much sense to me anyways
User is online!Profile CardPM
+Quote Post

flodi
RE: Help With Login Code
27 Jan, 2008 - 12:11 AM
Post #5

New D.I.C Head
*

Joined: 20 Jan, 2008
Posts: 10

hi
this my final code and its works fine with me if you have any comments please tell me
CODE

Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim str As String
        Dim SqlCommand2 As New SqlCommand
        str = "LoginSP1"
        With SqlCommand2
            .CommandType = CommandType.StoredProcedure
            .CommandText = str
            .Parameters.AddWithValue("@tech_user_name", UsernameTextBox.Text)
            .Parameters.AddWithValue("@tech_password", PasswordTextBox.Text)
            .Connection = SqlConnection1
        End With
        SqlConnection1.Open()
        Dim valid As Integer = SqlCommand2.ExecuteScalar
        If valid < 1 Then
            'invalid login
            MsgBox("كلمة المرور او اسم المستخدم خطاء", MsgBoxStyle.OkOnly)
            UsernameTextBox.Text = ""
            PasswordTextBox.Text = ""
            UsernameTextBox.Focus()
            SqlConnection1.Close()
            SqlCommand2.Parameters.Clear()
        Else
            'valid login
            OK.Enabled = False
            Cancel.Enabled = False
            UsernameLabel.Enabled = False
            PasswordLabel.Enabled = False
            MenuStrip1.Enabled = True
            UsernameTextBox.Enabled = False
            PasswordTextBox.Enabled = False
            username = UsernameTextBox.Text
            Label1.Text = username
            SqlCommand2.Parameters.Clear()
            SqlCommand1.Parameters("@tech_user_name").Value = UsernameTextBox.Text
            Dim read As SqlClient.SqlDataReader
            read = SqlCommand1.ExecuteReader
            If read.Read Then
                Label2.Text = read.Item("user_type")
                read.Close()
                SqlConnection1.Close()
                SqlCommand1.Parameters.Clear()
            End If
        End If
    End Sub

thanks to be interisting on my topic
User is offlineProfile CardPM
+Quote Post

B-Boy209
RE: Help With Login Code
27 Jan, 2008 - 10:05 PM
Post #6

D.I.C Head
**

Joined: 9 Dec, 2007
Posts: 61


My Contributions
QUOTE(flodi @ 26 Jan, 2008 - 03:43 PM) *

this is the correct answer
SqlCommand1.Parameters("@tech_user_name").Value = UsernameTextBox.Text
thanks any way



oh but i was talking about the ("tech_user_name") right here that was in your first code...


End If
SqlConnection1.Open()
SqlCommand1.Parameters("tech_user_name").Value = UsernameTextBox.Text
Dim read As SqlClient.SqlDataReader
read = SqlCommand1.ExecuteReader
If read.Read Then
TextBox1.Text = read.Item("user_type")
read.Close()
SqlConnection1.Close()
SqlCommand1.Parameters.Clear()
End If
End Sub
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/8/09 02:05PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month