1

I'm creating a login form in my asp.net page and my problem now is I don't know how to connect my asp.net (vb) to the SQL Server database.

I wanted to insert the connection code in the

<script runat="server"> </script>

Is this possible?

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Login.aspx.vb" Inherits="_Default" %>
<script runat="server">
    Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        lblDate.Text = Format(Now, "MMMM dd,yyyy hh:mmtt")
    End Sub
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link rel="icon" type="image/png" href="SENCOR_Logo.ico">
    <title>Auto OCS System</title>
    <link rel="stylesheet" type="text/css" href="style.css">

</head>
<body>
    <div class="date_time">
    <p><asp:Label id="lblDate" runat="server" Text ="DateandTime"></asp:Label></p>
    </div>
    <div id="wrapper">
    <div class = "container">
        <div class = "login">
            <h1><img src="SENCOR_Logo.jpg" height="35px;"></h1>
            <form method = "post" action = "" runat="server">
                <p><asp:TextBox id="txtUser" runat="server" placeholder="Username"/></p>
                <p><asp:TextBox id="txtPass" runat="server" TextMode="Password" placeholder="Password"/></p>
                <p class = "remember_me">
                <label>
                <label>
                    <asp:CheckBox id="chkRem" runat="server"/>
                    Remember me on this computer
                </label>
                </label>
                </p>
                    <p class="submit"><asp:Button id="btnLogin" Text="Login" runat="server" OnClick = "btnLogin_Click" /></p>
            </form>
        </div>
        <div class="login-help">
            <p>Forget your password? <asp:HyperLink id="lnkRem" runat="server" Text="Click here to reset it." NavigateUrl ="~/Login.aspx"></asp:HyperLink></p>
        </div>
    </div></div>
    <div class="footer">
        <p>&copy; 2016 Emilyn Pascua. All rights reserved.</p>
    </div>
</body>
</html>
1
  • See this MSDN page it may help you. Commented Feb 11, 2016 at 6:24

2 Answers 2

1

This may help you.change code according to your data

Private Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnlogin.Click
    ConnectToSQL()

End Sub

Private Sub ConnectToSQL()

    Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim Passowrd As String
    Dim Passowrd2 As String
    Dim userName As String

    Try
        If
            /*change the data source and initial catalog according to your sql server engine and data base*/
            con.ConnectionString = "Data Source = YOUR-PC; Initial Catalog = YOUR-DB; Integrated Security = True"
            con.Open()

            cmd.Connection = con
             /*change the data fields names and table according to your database*/
            cmd.CommandText = " SELECT  UserName, Password FROM   AdminDetail WHERE   (UserName = '" & txtUsername.Text & "' ) AND (Password = '" & txtPassword.Text & "')"

            Dim lrd As SqlDataReader = cmd.ExecuteReader()
            If lrd.HasRows Then
                While lrd.Read()

                    //Do something here
                    Passowrd = lrd("Password").ToString()
                    userName = lrd("UserName").ToString()

                    Passowrd2 = txtPassword.Text()

                    If Passowrd = Passowrd2 And userName = txtUsername.Text Then

                        MessageBox.Show("Logged in successfully as " & userName, "", MessageBoxButtons.OK, MessageBoxIcon.Information
                                        )
                        frmMain.Show()
                        Me.Hide()

                        //Clear all fields
                        txtPassword.Text = ""
                        txtUsername.Text = ""

                    End If

                End While

            Else
                MessageBox.Show("Username and Password do not match..", "Authentication Failure", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
                //Clear all fields
                txtPassword.Text = ""
                txtUsername.Text = ""
            End If

        End If

    Catch ex As Exception
        MessageBox.Show("Error while connecting to SQL Server." & ex.Message)

    Finally
        con.Close() //Whether there is error or not. Close the connection.

    End Try

End Sub

I hope you can embed the same in your server tags..you may refer the following link for clarification

http://www.aspsnippets.com/Articles/Different-Embedded-Code-Blocks-and-its-use-in-ASPNet.aspx

Sign up to request clarification or add additional context in comments.

3 Comments

check is sqlserver installed on your machine? and is connection working properly?
thanks .. i have debug the code you gave me.. to work it properly.
am new to vb.net,and your question helped me in improving my knowledge.:)
0
String cons = "Data Source=.; initial Catalog=SalaamUnExamSystem; Integrated Security=true";
            SqlConnection con = new SqlConnection(cons);
            SqlCommand cmd = new SqlCommand("insert into stdtbl values('" + StdName.Text + "','" + FName.Text + "','"+gender+"')",con);
            con.Open();
            int r=cmd.ExecuteNonQuery();
            if (r > 0)
            {
                lblMessage.Text="inserted";
            }
            else
            {
                lblMessage.Text = "Not Inserted";
            }

1 Comment

It is used for connection with two parties

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.