Saturday, 19 April 2014

Database value in textBox if selected in listbox in window form


                                                                                                                                          Previous...
                                                                                                                                             Next...

Here i will explain database value in textbox if selected value in list box. Before this i explained that how to link listbox with database.

Step(1):- Form
Drag and down a listbox from toolbox.
Step(2):- Code
Click on list box and write this code.

Private Void listBox1.SelectedIndexChanged(object sender , EventArgs e)
{
    string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=123;";
    SqlConnection con = new SqlConnection(str);
    string query = "select * from tablename where name = '"+listBox1.text+"' ";
    SqlCommand cmd = new SqlCommand( query,con);
    sqldatareader dbr;
    try
    {
        con.open();
        dbr = cmd.executereader();
        while(dbr.read())
        {
            string sID = (string) dbr["ID"].tostring;
            string sname = (string) dbr["name"// name is string value
             string ssurname = (string) dbr["surname"] 
            string sage = (string) dbr["age"].tostring;
            textbox1.text = sid;
            textbox2.text = sname;
            textbox3.text = ssurname ;
            textbox4.text = sage;

        }
        catch(execption es)
        {
           messagebox.show(es.message);
        }
    }
}

Step(3):- Output
Run your application.
                                                                                                                                          Previous...
                                                                                                                                             Next...

Link list box with DataBase in window form


                                                                                                                                               Previous..
                                                                                                                                                     Next..

Here i will explain How to connect ListBox with database.It is same as Link a combo Box with database.

Step(1):- Form
Drag and down a List box from Toolbox.
Step(2):- Code
Now make a function for filling the list box from database and call this method in constructor.


Void fillListBox()
{
    string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=123;";
    SqlConnection con = new SqlConnection(str);
    string query = "select * from tablename";
    SqlCommand cmd = new SqlCommand( query,con); 
    sqldatareader dbr;
    try
    {
        con.open();
        dbr = cmd.executereader();
        while(dbr.read())
        {
           string sname = (string) myreader["name"];; //name is coming from database
           Listbox1.items.Add(sname);
         }
         catch(execption es)
         {
              messagebox.show(es.message);
         } 
    }
}

Now call this method.
Public Form2()
{
InitializeComponent();
FillListBox();
}
Step(3):- Run 
Now Run your application
                                                                                                                                               Previous..
                                                                                                                                                     Next..

Friday, 18 April 2014

Database values in textbox if select Combobox in window form



                                                                                                                                            previous....
                                                                                                                                                  Next..

Here i will explain that when i select any name from combobox then is all detail show in respective textboxes.

Step(1):-Form with combobox
drag and down a combobx from toolbox.
Step(2):-Coding
Click on combobox and write this coding.


Private Void comboBox1.SelectedIndexChanged(object sender , EventArgs e)
{
    string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=123;";
    SqlConnection con = new SqlConnection(str);
    string query = "select * from tablename where name = '"+combobox1.text+"' ";
    SqlCommand cmd = new SqlCommand( query,con);
    sqldatareader dbr;
    try
    {
        con.open();
        dbr = cmd.executereader();
        while(dbr.read())
        {
            string sID = (string) dbr["ID"].tostring;
            string sname = (string) dbr["name"// name is string value
             string ssurname = (string) dbr["surname"] 
            string sage = (string) dbr["age"].tostring;
            textbox1.text = sid;
            textbox2.text = sname;
            textbox3.text = ssurname ;
            textbox4.text = sage;

        }
        catch(execption es)
        {
           messagebox.show(es.message);
        }
    }
}
Step(3):-Output
Run your application and click on name which is in combobox.

                                                                                                                                            previous....
                                                                                                                                                  Next..

Link ComboBox with database in window form


                                                                                                                                           Previous...
                                                                                                                                               Next...

Here i will explain how to Link combobox with database. Before this o explained that How to use combobox.

Step(1) :- Form with ComboBox
Drag and down a combobox from tool Box.
Step(2) :- Coding
Now at coding section make a function which is connected with database. And call this method in constructor.

Void fillcombo()
{
    string str = "server = MUNESH\\SQL2008R2;Database=datastore;UID=sa;Password=123;";
    SqlConnection con = new SqlConnection(str);
    string query = "select * from tablename";
    SqlCommand cmd = new SqlCommand( query,con); 
    sqldatareader dbr;
    try
    {
        con.open();
        dbr = cmd.executereader();
        while(dbr.read())
        {
           string sname = (string) myreader["name"];; //name is coming from database
           combobox1.items.Add(sname);
         }
         catch(execption es)
         {
              messagebox.show(es.message);
         } 
    }
}

Step(3) :- Output
When you run your application the data(we are fatching name from database) will be store in combobox.
                                                                                                                                           Previous...
                                                                                                                                               Next...


Thursday, 17 April 2014

How to Use comboBox in window form


                                                                                                                                     previous
                                                                                                                                         Next

Here i will explain how to use combo box in window application. Before this i explain that  delete selected data from database.

Step(1) :- registration form with a combo box 
 Drag and down a Combo box and a button from tool Box. And give the name of button As "Add string"
Step(2) :- Code1
 At first step i want that when i enter name and click on Button(Add string) then that name show in combo box.
So click on Add String button and write this code.

    private void Addstring_Click(object sender, EventArgs e)
{
string namestr = Name_txt.Text;
comboBox1.Items.Add(namestr);
}

Step(3) :- OutPut for code1
 when you enter name textbox and click on button(add string) you will see taht name will show in comboBox.

Step(4) :- Code2
Now if we want that when i click on any name which is in combobox that name show in a textbox. so code for that.Click on combo box and write this code. I want that selected name from combo box show in Id textbox.

 private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    Eid_txt.Text = comboBox1.text;
}
Step(5) :- OutPut for code2
Now select any name in combobx that name will show in Id text box.
                                                                                                                                                                                                           previous
                                                                                                                                         Next

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

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...