Delete Database Record By using Windows Form Application


In this video tutorial we will learn how to delete database single  record by using windows from  application. To continue with this session please watch previous video. 


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 already created the required table and inserted some sample data.  

we want to delete account record by providing Account Number. 

Here is the SQL Script .

  CREATE TABLE [dbo].[ACCOUNT](
[Account_Number] [int] IDENTITY(1,1) NOT NULL,
[Tittle] [nvarchar](50) NULL,
[First_Name] [nvarchar](50) NULL,
[Last_Name] [nvarchar](50) NULL,
[Occupation] [nvarchar](50) NULL,
[DOB] [nvarchar](50) NULL,
[Mobile] [nvarchar](50) NULL,
[Home_No] [nvarchar](50) NULL,
[Street_Name] [nvarchar](50) NULL,
[Country] [nvarchar](50) NULL,
[Post_Code] [nvarchar](50) NULL,
[Gender] [nvarchar](50) NULL,
[Anual_Salary] [money] NULL,
[statementamount] [money] NULL,
[Email] [nvarchar](50) NULL,
[Nationality] [nvarchar](50) NULL,
[FileName] [nvarchar](500) NULL,
[FileData] [varbinary](max) NULL,
[Type_Of_Account] [nvarchar](50) NULL,
[Branch_Name] [nvarchar](50) NULL,
[Sort_Code] [nvarchar](50) NULL,
[Over_Draft] [money] NULL,
[physicallyChallenged] [nvarchar](50) NULL,
)


Here is How we want to design windows form application .




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


  private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                label3.Text = "Please provide the Account Number ";
                return;
            }

            try
            {
   stringconnectionstring =@"Data Source=.;Initial Catalog=Login;Integrated            Security=True";
                using (SqlConnection conn = new SqlConnection(connectionstring))
                {
                    conn.Open();
                    using (SqlCommand cmd = new SqlCommand("DELETE FROM ACCOUNT" + " WHERE Account_Number =@Account_Number", conn))
                    {
                        cmd.Parameters.AddWithValue("@Account_Number", textBox1.Text);

                        int rows = cmd.ExecuteNonQuery();
                        label3.Text = "ACCOUNT IS DELETED";

                    }
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
                label3.Text = "Account is not Exist";

            }
        }

If done everything correctly you will see the message box will display the message . 



No comments:

Post a Comment