WPF Database Connectivity with SQL Server in VB.NET

In this article, we will learn how to create database connectivity of WPF with database SQL Server using VB.NET.
  • 8054

In this article, we will learn how to create database connectivity of WPF with database SQL Server using VB.NET.

creating a table in database

Creating a table in SQL Server.

Table name login and database name model.

use model

go

create table login

(

FirstName varchar(50),

LastName varchar(45),

Login varchar(55),

Password varchar(60)

)

go

select * from login;

go

Table has created in SQL Server.

Now come to the front end window forms application in VB.NET.

Taking four TextBlock, three textbox, one passwordbox and one button control on the form. 
 

lo1.gif 

Figure 1.

XAML code

<Window x:Class="MainWindow"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    Title="MainWindow" Height="350" Width="525">

    <Grid>

        <TextBlock Height="25" HorizontalAlignment="Left" Margin="10,10,0,0" Name="TextBlock1"Text="FirstName" VerticalAlignment="Top" />

        <TextBlock Height="23" HorizontalAlignment="Left" Margin="10,41,0,0" Name="TextBlock2"Text="LastName" VerticalAlignment="Top" />

        <TextBlock Height="23" HorizontalAlignment="Left" Margin="34,71,0,0" Name="TextBlock3"Text="Login" VerticalAlignment="Top" />

        <TextBlock Height="23" HorizontalAlignment="Left" Margin="13,107,0,0" Name="TextBlock4"Text="Password" VerticalAlignment="Top" />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="82,10,0,0" Name="txtfname"Uid="txtfname" VerticalAlignment="Top" Width="120" />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="82,38,0,0" Name="txtlname"VerticalAlignment="Top" Width="120" />

        <TextBox Height="23" HorizontalAlignment="Left" Margin="82,71,0,0" Name="txtlogin"VerticalAlignment="Top" Width="120" />

        <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="90,150,0,0"Name="Button1" VerticalAlignment="Top" Width="75" />

        <PasswordBox Height="23" HorizontalAlignment="Left" Margin="90,107,0,0"Name="txtpassword" VerticalAlignment="Top" Width="120" />

    </Grid>

</Window>

Now double click on the button save and add the following c# code.

Private Sub Button1_Click(ByVal sender As System.ObjectByVal e AsSystem.Windows.RoutedEventArgsHandles Button1.Click

        Dim str As String = "Data Source=.;uid=sa; pwd=123;database=model"

        Dim con As New SqlConnection(str)

        con.Open()

        Dim sql As String = "INSERT INTO login VALUES(@FirstName,@lastName,@Login, @Password)"

        Dim cmd As New SqlCommand(sql, con)

        cmd.Parameters.AddWithValue("@FirstName", txtfname.Text)

        cmd.Parameters.AddWithValue("@Lastname", txtlname.Text)

        cmd.Parameters.AddWithValue("@Login", txtlogin.Text)

        cmd.Parameters.AddWithValue("@Password", txtpassword.Password)

        cmd.ExecuteNonQuery()

        MessageBox.Show(" Record has been saved")

        con.Close()

    End Sub

Now execute the application press ctrl+F5.

lo2.gif 

Figure 2.

Now click on the save button message will be display.

lo3.gif 

Figure 3.

Now open the sql server record has been saved in the table login.

Categories

More Articles

© 2019 DotNetHeaven. All rights reserved.