Wednesday 16 April 2014

Deleting selected data from database in window form


                                                                                                                                             Previous
                                                                                                                                                   Next

Here i will explain how to delete selected data from database, before this i explain how to update or edit data.

Step(1):-Registration form with delete button

Drag and down a button from toolbox and give it name as Delete.
Step(2):-code

Double click on Delete button and write code. here database is same as before shown you in tutorial 6

private void Delete_txt_Click(object sender, EventArgs e)
{
    if (!(id_txt.Text == string.Empty))
    {
        string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=reladmin@123;";
        SqlConnection con = new SqlConnection(str);
        string query = "Delete from data1 where ID= '" + this.id_txt.Text + "'";
        SqlCommand cmd = new SqlCommand(query, con);
        SqlDataReader myreader;
        try
        {
             con.Open();
            myreader = cmd.ExecuteReader();
            MessageBox.Show("successfully data Deleted","user information");
            while (myreader.Read())
           {
           }
           con.Close();
       }
      catch (Exception ec)
      {
         MessageBox.Show(ec.Message);
      }
    }
    else
   {
        MessageBox.Show("enter ID which you want to delete","User information");
    }
}
Step(3):-Output

Run your project and enter ID and click on delete button.
                                                                                                                                             Previous
                                                                                                                                                   Next

Monday 14 April 2014

Edit_Update a data from database with button in window form


                                                                                                                                         Previous
                                                                                                                                              Next

Here i will explain Edit_Update data from database in window form. Before this i explain that How to insert data into database using code.

Step(1) : Registration form with update button
Drag and down a button from tool box and give it name as Update.

Step(2) : Code
Double click on update button and write code, database is same as shown you in tutorial 6.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Update_Click(object sender, EventArgs e)
        {
               SqlConnection con = new SqlConnection(str);
                String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
                String query = "update set Name = '" + this.name_txt.Text + "', Surname= '" + this.surname_txt.Text +"',Age= '" + this.Age_txt.Text+ "' where ID= '" + this.id_txt.Text + "' ";
         
              SqlCommand cmd = new sqComamnd(query,con);
                SqlDataReader dbr;
                       
               try
              {
               con.open();
               dbr = cmd.ExecuteReader();
                MessageBox.Show("Updated data");
              while(dbr.read())
             {
                 
              }
            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);

            }
        }
    }
} 

Step(3) : Output
Run your programme and click on update button.

                                                                                                                                         Previous
                                                                                                                                              Next

Sunday 13 April 2014

Insert data into database in window form


                                                                                                                                               previous
                                                                                                                                                   next
Here i will explain how to insert data into database in window form. Before this i explain that how to open a second form using first form.

Step(1) : Registration form

Here i make a registration form where i take 4 lable and 4 textbox and a button(save).

Step(2) : database

Now make a database into sql server

Step(3) : Code on save button



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           SqlConnection con = new SqlConnection(str);
                String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
                String query = "insert into data (E.id,name,surname,age) values ('"+this.eid_txt.text+"','"+this.nametxt.text+"','"+this.surname_txt.text+"','"+this.age_txt.text+"')";
     SqlCommand cmd = new sqComamnd(query,con);         
  SqlDataReader dbr;
                       
               try
              {
               con.open();
               dbr = cmd.ExecuteReader();
                MessageBox.Show("saved");
              while(dbr.read())
             {
                 
              }
            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);

            }
        }
    }
} 

Step(4) : output


Now click on save button
                                                                                                                                               previous
                                                                                                                                                   next

How to open a second form using first form in window form

                                                                                                                                           
                                                                                                                                            previous
                                                                                                                                                 Next

Here i will explain how to open second from using first form in window form.Before this i explain that group box and picture box in window form.

Step(1):  Login form

There is a login from when i enter correct password and username then page direct to another page name is form2.




Step(2):  Add a new form

Now go to solution explorer and select your project and right click on it and select a new window form and give a name it as form2
 





And new form is


Step(3):  Coding

now click on submit button and write a code.



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace First_Csharp_app
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (!(usertxt.Text == string.Empty))
                {
                    if (!(passtxt.Text == string.Empty))
                    {
                        String str = "server=MUNESH-PC;database=windowapp;UID=sa;password=123";
                        String query = "select * from data where username = '" + usertxt.Text + "'and password = '" +this.passtxt.Text + "'";
                        SqlConnection con = new SqlConnection(str);
                        SqlCommand cmd = new SqlCommand(query, con);
                        SqlDataReader dbr;
                        con.Open();
                        dbr = cmd.ExecuteReader();
                        int count = 0;
                        while (dbr.Read())
                        {
                            count = count + 1;
                        }
                        if (count == 1)
                        {
                           this.hide();
                           Form2  f2 = new form2();         //this is the change, code for redirect
                           f2.ShowDialog();
                        }
                        else if (count > 1)
                        {
                            MessageBox.Show("Duplicate username and password""login page");
                        }
                        else
                        {
                            MessageBox.Show(" username and password incorrect""login page");
                        }
                    }
                    else
                    {
                        MessageBox.Show(" password empty""login page");
                    }
                }

                else
                {
                    MessageBox.Show(" username empty""login page");
                }
                // con.Close();

            }
            catch (Exception es)
            {
                MessageBox.Show(es.Message);

            }
        }
    }
}

Step(4) :- output

when you click on submit button there will open a new form name is form2
                                                                                                                                            previous
                                                                                                                                                 Next

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...