Inserting Data Into SQL Database By using Windows Form Application


In this video tutorial we will learn how to insert data  into database by using windows from  application. To continue with this session you must have knowledge about Object Oriented Programming Languages (C#) because we will be using c# programming languages to write the code . 


Software Required :


1.Microsoft SQL Server .
2.Microsoft SQL Management Studio.
3.visual studio 2017 .  

Please share if you think these video are useful .Late video we will learn how to consume WCF ,WCF(REST) service into Angular JS Application, Windows Form Application, Console Application etc .  In this session we are trying to learn some basic knowledge about insert , update and delete operation . 
Here is what we i want do . First we need to create database in sql server .Here is the SQL Script to Create the Database  table and insert some data  but we will be doing it by using windows form application .I created a table called tblReciption and in this we want to insert the data by using store procedure .

Sql Script ..


CREATE TABLE [dbo].[tblReciption](

[User_Id] [int] IDENTITY(1,1) NOT NULL,

[username] [nvarchar](50) NOT NULL,

[password] [nvarchar](50) NOT NULL




Here is store procedure ..

Create Procedure [dbo].[REs]
 @username [nvarchar](50),
@password[nvarchar](50)
as
Begin 
select * from  tblReciption
Insert into tblReciption values(@username,@password)
End

Here is How we want to design windows form application .



To insert the data successfully first we need to add the connection string in app.config file 
  <connectionStrings>
        <add name="LoginConnectionString" connectionString="Data Source=.;Initial Catalog=Login;Integrated Security=True" providerName="System.Data.SqlClient"/>
    </connectionStrings>

Just double click the Register button to generate the button event .Copy and paste following codes .


 private void button1_Click(object sender, EventArgs e)
        {
             //Read the connection string from Web.Config 
stringConnectionString=ConfigurationManager.ConnectionStrings["LoginConnectionString"].ConnectionString;

    using (SqlConnection con = new SqlConnection(ConnectionString))
    {
        //Create the SqlCommand object
        SqlCommand cmd = new SqlCommand("REs", con);

       
        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        //Add the input parameters to the command object
        cmd.Parameters.AddWithValue("@username", txt1.Text);
        cmd.Parameters.AddWithValue("@password", text2.Text);       

        //Open the connection and execute the query
        con.Open();
        cmd.ExecuteNonQuery();
                
      
        label4.Text = "You are successfully Registred";
    }
}

If you done every thing correctly we will get the message   "You are successfully Registred";. 






2 comments: