ARTICLE

Database Connectivity of WPF with SQL Server using VB.NET

Posted by Rohatash Kumar Articles | WPF using VB.NET January 20, 2011
In this article, we will learn how to create database connectivity of WPF with database SQL Server using VB.NET.
 
Reader Level:

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.Object, ByVal e As System.Windows.RoutedEventArgs) Handles 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.

Login to add your contents and source code to this article
share this article :
post comment
 

Sir, how to connect vb.net with sql server?

Posted by jeeja NS Feb 25, 2011
Team Foundation Server Hosting
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor