Check Database Record


In this video tutorial we will learn how to check database 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 check account record by providing Account Number. 


Here is How we want to design windows form application .





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


   private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection(@"Data Source=.;Initial Catalog=Login;Integrated Security=True");

            SqlCommand cmd = new SqlCommand(@"SELECT statementamount FROM ACCOUNT WHERE 
            Account_Number = @Account_Number 
        and First_Name = @First_Name", cn);
            cmd.Parameters.AddWithValue("@Account_Number", textBox1.Text);
            cmd.Parameters.AddWithValue("@First_Name", textBox2.Text);
            cmd.Parameters.AddWithValue("@statementamount", textBox3.Text);
            cn.Open();
            var value = cmd.ExecuteScalar();
            var da = new SqlDataAdapter(cmd);
            DataTable tbl = new DataTable();
            da.Fill(tbl);

            if (tbl.Rows.Count == 0)
            {
                MessageBox.Show("No such account/holder information on file.");
            }


            else if (Convert.ToDouble(textBox3.Text) <= Convert.ToDouble(value))
            {
                MessageBox.Show("Ok to withdraw funds");
            }
            else
                MessageBox.Show("Not enough funds");
        }
    }

If done everything correctly you will be able to check the record  values and message box  will display expected message  . 

No comments:

Post a Comment