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


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