Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Call a stored procedure

Author  Topic 

Shep
Starting Member

19 Posts

Posted - 2013-06-18 : 15:08:42
Using VB and a Windows Form
I need to call a stored procedure named: validateUser

It is called by a button click and will put the info into a datatable.

Then I will use an "IF Statement" to validate that the Username and Password are valid.

If they ARE valid, then the user will be able to proceed to a Mainform
If they are NOT valid, then a Label will appear with "INVALID USERNAME/PASSWORD"

Any help will be appreciated.
Thanks
JS

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-06-18 : 15:37:59
It is hard to tell what specifically you need help with. Especially hard because you never actually asked a question.

Do you have a vb development environment like visual studio?
Do you know how to create a windows forms project?
Do you know how to create a form with a button and associate some code with the OnClick event of the button?
Do you know how to use ADO.NET objects to connect to a sql server database and call a stored procedure?
Do you have Sql server management studio and can you create a stored procedure?
Do you already have a database with tables and data necessary to perform these validations?
etc...

There are plenty of tutorials out there for all this stuff - have you tried searching for any of them?
This forum is really for advice and/or code to solve specific problems. Not so much for providing complete solutions.




Be One with the Optimizer
TG
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-06-18 : 15:38:50
This is a standard problem that most of the frameworks including the .Net framework is set up for already. Google for "form-based authentication" and you will find lots of resources including sample code, tutorials etc. Some MSDN documentation here: http://support.microsoft.com/kb/301240
Go to Top of Page

Shep
Starting Member

19 Posts

Posted - 2013-06-18 : 15:46:32
Sorry TG, I should have been more specific.

I am using Visual studio 2012 and sql server 2008R2.
The form is already created with the username and password textboxs and a submit button.

The stored procedure is:
@Username nvarchar(50), @Password nvarchar(50)
AS

SELECT *
FROM [User]
WHERE Username = @Username
AND [Password] = @Password

Its in Northwind database.
I already have a database connection

The tables are created and filled but only need to use the User table.

I DO NOT know how to connect to the database when the button is clicked and call the store procedure.

Once the info is passed in, I plan to write the IF statement to validate that the USERNAME/PASSWORD is valid.

This is what I need help with.

Thanks again
JS
Go to Top of Page

TG
Master Smack Fu Yak Hacker

6065 Posts

Posted - 2013-06-18 : 16:07:17
have a look at MSDN SqlCommand. There are some examples on this as well as on most of the methods. You'll need to decide if your procedure will return a result or not because you would call a different method depending.

usually simply double clicking the button in a designer window will create the OnClick event handler where you can put this code.

Be One with the Optimizer
TG
Go to Top of Page

Shep
Starting Member

19 Posts

Posted - 2013-06-19 : 15:01:58
Here is the code that I ended up using(actual sql connection X'ed out):

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
'Create connection to database
Dim con As New SqlConnection("server=developmentdb1.xxxxxxxxxxx;database=Northwind;UID=NorthwindUser;PWD=NorthwindUser!;")
con.Open()

Dim command As SqlCommand = New SqlCommand("validateUser", con)
command.CommandType = CommandType.StoredProcedure

'add parameters
command.Parameters.AddWithValue("@Username", txtUsername.Text)
command.Parameters.AddWithValue("@Password", txtPassword.Text)

Dim result As Int32
result = CType(command.ExecuteScalar(), Int32)

If result = 1 Then

Main.Show()

Else
failLabel.Visible = True
End If


End Sub

I'm going to create a new thread similiar to this one about a similar code.


J.E.Shepler
Go to Top of Page
   

- Advertisement -