Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

WPF Connection String Help Needed

$
0
0

OK, so here is the deal. My applications login screen is giving me an issue at run time. I have been stuck for about a week now hopefully someone can help me out.

When I run the application in the debugger on the computer that built the application I can successfully create a user account and log in. 

When I close the application the user account I created is not there. 

When I run the release file on the computer I am given this error:

"An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll

Additional information: Violation of PRIMARY KEY constraint 'PK__Users__C9F284575839EA86'. Cannot insert duplicate key in object 'dbo.Users'. The duplicate key value is (test@test.com                                    ).

The statement has been terminated. "

App.Config:

<?xml version="1.0" encoding="utf-8" ?><configuration><configSections></configSections><connectionStrings><add name="KitchenWizard_0_1.MySettings.RegistrationConnectionString"
            connectionString="Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Registration.mdf;Integrated Security=True;Connect Timeout=30"
            providerName="System.Data.SqlClient" /></connectionStrings><startup> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.1" />    </startup></configuration>

MainWindow.xmal.vb:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Text
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media
Imports System.Windows.Media.Imaging
Imports System.Windows.Navigation
Imports System.Windows.Shapes
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions
Class MainWindow
    Inherits Window

    Public Sub New()
        MyBase.New()
        InitializeComponent()
    End Sub

    Private CreateUser As CreateUser = New CreateUser

    Private HomeScreen As HomeScreen = New HomeScreen

    Private Sub loginButton_Click(sender As Object, e As RoutedEventArgs) Handles loginButton.Click
        If (registeruserNameTextBox.Text.Length = 0) Then
            errormessage.Text = "Enter a Username."
            registeruserNameTextBox.Focus()
        ElseIf Not Regex.IsMatch(registeruserNameTextBox.Text, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$") Then
            errormessage.Text = "Enter a valid Username."
            registeruserNameTextBox.Select(0, registeruserNameTextBox.Text.Length)
            registeruserNameTextBox.Focus()
        Else
            Dim userNameT As String = registeruserNameTextBox.Text
            Dim password As String = passwordTextBox.Password
            Dim con As SqlConnection = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Registration.mdf;Integrated Security=True;Connect Timeout=30")
            con.Open()
            Dim cmd As SqlCommand = New SqlCommand(("Select * from Users where UserName='" _+ (userNameT + ("'  and Password='" _+ (password + "'")))), con)
            cmd.CommandType = CommandType.Text
            Dim adapter As SqlDataAdapter = New SqlDataAdapter
            adapter.SelectCommand = cmd
            Dim dataSet As DataSet = New DataSet
            adapter.Fill(dataSet)
            If (dataSet.Tables(0).Rows.Count > 0) Then
                Dim username As String = (dataSet.Tables(0).Rows(0)("UserName").ToString)
                HomeScreen.TextBlockName.Text = username
                'Sending value from one form to another form.
                HomeScreen.Show()
                Close()
            Else
                errormessage.Text = "Sorry! Please enter existing UserName/Password."
            End If
            con.Close()
        End If
    End Sub

    Private Sub newUserButton_Click(sender As Object, e As RoutedEventArgs) Handles newUserButton.Click
        CreateUser.Show()
        Close()
    End Sub
End Class

CreateUser.xmal.vb

Imports System
Imports System.IO
Imports System.Net
Imports System.Windows
Imports System.Windows.Controls
Imports System.Windows.Data
Imports System.Windows.Media
Imports System.Windows.Media.Animation
Imports System.Windows.Navigation
Imports System.Text
Imports System.Windows.Documents
Imports System.Windows.Input
Imports System.Windows.Media.Imaging
Imports System.Windows.Shapes
Imports System.Data
Imports System.Data.SqlClient
Imports System.Text.RegularExpressions

Public Class CreateUser
    Inherits Window

    Public Sub New()
        MyBase.New()

        Me.InitializeComponent()
    End Sub


    Private Sub submitButton_Click_1(sender As Object, e As RoutedEventArgs) Handles submitButton.Click
        If (registeruserNameTextBox.Text.Length = 0) Then
            errormessage.Text = "Enter an UserName."
            registeruserNameTextBox.Focus()
        ElseIf Not Regex.IsMatch(registeruserNameTextBox.Text, "^[a-zA-Z][\w\.-]*[a-zA-Z0-9]@[a-zA-Z0-9][\w\.-]*[a-zA-Z0-9]\.[a-zA-Z][a-zA-Z\.]*[a-zA-Z]$") Then
            errormessage.Text = "Enter a valid Username"
            registeruserNameTextBox.Select(0, registeruserNameTextBox.Text.Length)
            registeruserNameTextBox.Focus()
        Else

        End If
        Dim userName As String = registeruserNameTextBox.Text
        Dim password As String = registerpasswordTextBox1.Password
        If (registerpasswordTextBox1.Password.Length = 0) Then
            errormessage.Text = "Enter password."
            registerpasswordTextBox1.Focus()
        ElseIf (confirmPasswordTextBox.Password.Length = 0) Then
            errormessage.Text = "Enter Confirm password."
            confirmPasswordTextBox.Focus()
        ElseIf (registerpasswordTextBox1.Password <> confirmPasswordTextBox.Password) Then
            errormessage.Text = "Confirm password must be same as password."
            confirmPasswordTextBox.Focus()
        Else
            errormessage.Text = ""
            Dim con As SqlConnection = New SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Registration.mdf;Integrated Security=True;Connect Timeout=30")
            con.Open()
            Dim cmd As SqlCommand = New SqlCommand(("Insert into Users (UserName,Password) values('" _+ (userName + ("','" _+ (password + "')")))), con)

            cmd.CommandType = CommandType.Text
            cmd.ExecuteNonQuery()
            cmd.UpdatedRowSource.ToString()
            con.Close()
            errormessage.Text = "You have Registered successfully."
            Reset()
        End If
    End Sub

    Private Sub cancelButton_Click_1(sender As Object, e As RoutedEventArgs) Handles cancelButton.Click
        Dim login As MainWindow = New MainWindow
        login.Show()
        Close()
    End Sub


    Private Sub loginButton_Click(sender As Object, e As RoutedEventArgs) Handles loginButton.Click
        Dim login As MainWindow = New MainWindow
        login.Show()
        Close()
    End Sub
End Class

HomeScreen.xmal.vb

Public Class HomeScreen

    Private Sub logoutButton_Click(sender As Object, e As RoutedEventArgs) Handles logoutButton.Click
        Dim logout As MainWindow = New MainWindow
        logout.Show()
        Close()
    End Sub
End Class



Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>