Simple cpp program using break keyword


Code :
#include<iostream>
using namespace std;
int main() {
    int a=1;
    while (a>0){
        if (a==9)
            break;
        cout<<a<<endl;
        a++;
    }
    return 0;
}


Output : 
1
2
3
4
5
6
7
8

IDE Used To Test This Code : Dev C++

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

How to read TXT file content using CPP Program


Code : 
/*
* Author: Soumyodeep Mondal
*/

//header file to include all standard library header file
#include <bits/stdc++.h>
using namespace std;

int main() {
 
    //file stream declaration
    fstream txtfile;
    string stringcontent, targetfilename;
 
    //name of the file which we will read through cpp program
    targetfilename = "targetfile.txt";
 
    //open the targetfile.txt
    txtfile.open(targetfilename.c_str());
 
    //reading the string of the targetfile.txt
    while (txtfile >> stringcontent) {
        cout << stringcontent;
    }
    return 0;
}

Output :
www.codetheta.com

IDE Used To Test This Code : Dev C++

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Program to generate right triangle Star Pattern in CPP.


 Code :
/*
* Author: Soumyodeep Mondal
*/
#include<iostream>
#include<conio.h>
using namespace std;
int main(){
    int i,j;
    for(i=1; i<=5; i++) {
        for(j=1; j<=i; j++) {
            cout<<"*";
        }
        cout<<"\n";
    }
}


Output :
*
**
***
****
*****

IDE Used To Test This Code : Dev C++.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

How to get data in drop down menu from another sheet in Excel


Here this tutorial i have two excel sheet, "front" and "main" as you can able to see in the picture.

main sheet contains five persons name and the other sheet called front where we want to show this persons name in drop down manner.


Step 1 : Select the cell where you want to appear drop down menu.

Step 2 : Then go to DATA tab in excel, after that go to Data Validation option under Data Validation menu.


Step 3 : Data Validation menu box will appear, choose Settings tab then choose List option in Allow section after that under Source section type formula : =main!A1:A5


Step 4 : Then click OK button after that you will see drop down menu will appear in your selected cell which contains data of another sheet.

If you have any Question you can contact us or mail us.We will reply you as soon as possible.

how to show database value to ComboBox in C# ?


Put the below code in to separate class.

public DataTable Select() {
            SqlConnection conn = new SqlConnection(myconnstrng);
            DataTable dt = new DataTable();
            try {
                string sql = "SELECT * FROM tablename";
                SqlCommand cmd = new SqlCommand(sql, conn);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                conn.Open();
                adapter.Fill(dt);
            }
            catch (Exception ex){
            }
            finally{
                conn.Close();
            }
            return dt;
        }

Below code for form code section
private void frmDeparmentChoose_Load(object sender, EventArgs e) {
            DataTable dt = c.Select();
            comboBoxDepartmentChoose.DisplayMember = "department_name";
            comboBoxDepartmentChoose.DataSource = dt;
        }

IDE Used To Test This Code : Visual Studio.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

How to show Database value grid wise in to tabControl in C# ?


//tab control function
        private void TabPageDepartmentChoose() {
            DataTable dt = c.Select();
            FlowLayoutPanel flp = new FlowLayoutPanel();
            flp.Dock = DockStyle.Fill;
            foreach (DataRow dr in dt.Rows) {
                Button button = new Button();
                button.Size = new Size(100, 50);
                button.Text = dr["department_name"].ToString();
                flp.Controls.Add(button);
                button.Click += new EventHandler(DynamicButtonDepartment_Click);
            }
            tabPageDepartmentChoose.Controls.Add(flp);
        }
//dynamic button click event for tab control button
        protected void DynamicButtonDepartment_Click(object sender, EventArgs e) {
            this.Hide();
            frmDashboard frm = new frmDashboard();
            frm.Show();
        }
//code for datatable access and TabPageDepartmentChoose() show during form run time.
DataTable dt = c.Select();
TabPageDepartmentChoose();

IDE Used To Test This Code : Visual Studio.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

How to create Login with different users with checkbox using C# ?



1. First make separate Class for Login. before that first add ConfigurationManager to your References.

2. Add connectionStrings to App.config File

<connectionStrings>
    <add name="connstrng" connectionString="Data Source=.;Initial Catalog=mmis;Integrated Security=True;"/>
  </connectionStrings>

3. Example : ClassFolder > MainClass.cs
4. Create getter setter method to MainClass.cs

//login getter setter
public int login_id { get; set; }
public string login_username { get; set; }
public string login_password { get; set; }
public string login_role { get; set; }

//connection string
static string myconnstrng=ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

//function for login section
        public bool Login(MainClass c){
            bool isSuccess = false;
            SqlConnection conn = new SqlConnection(myconnstrng);
            DataTable dt = new DataTable();
            try{
                string sql = "SELECT * FROM tbllogin WHERE login_username=@login_username AND login_password=@login_password AND login_role=@login_role";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@login_username", c.login_username);
                cmd.Parameters.AddWithValue("@login_password", c.login_password);
                cmd.Parameters.AddWithValue("login_role", c.login_role);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                conn.Open();
                adapter.Fill(dt);
                int count = dt.Rows.Count;
                if (count == 1){
                    isSuccess = true;
                }
                else{
                    isSuccess = false;
                }
            }
            catch (Exception ex){
            }
            finally{
                conn.Close();
            }
            return isSuccess;
        }

After that create a Login form.

Add using MMIS.ClassFolder; to predefined section
MainClass c = new MainClass();

        private void btnLogin_Click(object sender, EventArgs e)
        {
            c.login_username = txtUsername.Text;
            c.login_password = txtPassword.Text;
            if (checkBoxLoginUser.Checked == true) {
                c.login_role = checkBoxLoginUser.Text;
            }
            else if (checkBoxLoginAdmin.Checked == true) {
                c.login_role = checkBoxLoginAdmin.Text;
            }
            else {
               c.login_role = checkBoxLoginController.Text;
            }
            Boolean success = c.Login(c);
            if (success == true)
            {
                MessageBox.Show("Login Success.");
            }
            else
            {
                MessageBox.Show("Invalid Credentials. Please provide correct information.");
            }
        }

IDE Used To Test This Code : Visual Studio.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

How to extract numbers using RIGHT function in Excel?


RIGHT( ) is a useful function in excel that is used to extract number or text or string from right side of the string or text.

Example -
A
__________________
www.codetheta.com 111
www.codetheta.com 112
www.codetheta.com 113
www.codetheta.com 114
www.codetheta.com 115
www.codetheta.com 116
www.codetheta.com 117
www.codetheta.com 118
www.codetheta.com 119
www.codetheta.com 120
www.codetheta.com 121
www.codetheta.com 122
www.codetheta.com 123
www.codetheta.com 124
www.codetheta.com 125

=RIGHT(A1,3)
We want to extract last 3 digits numbers.

Result is - 

A                                      B
_______________________
www.codetheta.com 111 111
www.codetheta.com 112 112
www.codetheta.com 113 113
www.codetheta.com 114 114
www.codetheta.com 115 115
www.codetheta.com 116 116
www.codetheta.com 117 117
www.codetheta.com 118 118
www.codetheta.com 119 119
www.codetheta.com 120 120
www.codetheta.com 121 121
www.codetheta.com 122 122
www.codetheta.com 123 123
www.codetheta.com 124 124
www.codetheta.com 125 125


If you have any Question you can contact us or mail us.We will reply you as soon as possible.

How to create login using separate Class in C#


1. Create a folder within your project folder / solution.
2. Example : MainClasses. Within this folder create a class named CoreLIB.cs

Put the connectionString to your App.config file.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup> 
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="connstrng" connectionString="Data Source=.;Initial Catalog=database;Integrated Security=True;"/>
</connectionStrings>
</configuration>

use Getter and Setter to CoreLIB.cs file.
public string Username { get; set; }
public string Password { get; set; }
static string myconnstrng = ConfigurationManager.ConnectionStrings["connstrng"].ConnectionString;

use following namespace system CoreLIB.cs file
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

Use login function
public bool Login(CoreLIB c)
        {
            bool isSuccess = false;
            SqlConnection conn = new SqlConnection(myconnstrng);
            DataTable dt = new DataTable();
            try
            {
                string sql = "SELECT * FROM tablename WHERE Username=@Username AND Password=@Password";
                SqlCommand cmd = new SqlCommand(sql, conn);
                cmd.Parameters.AddWithValue("@Username", c.Username);
                cmd.Parameters.AddWithValue("@Password", c.Password);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                conn.Open();
                adapter.Fill(dt);

                if(dt.Rows[0][0].ToString() == "1")
                {
                    isSuccess = true;
                }
                else
                {
                    isSuccess = false;
                }

            }
            catch(Exception ex)
            {

            }
            finally
            {
                conn.Close();
            }
            return isSuccess;
        }

Login button code
use the following namespace system
using IJIRALIB.IJIRAClasses;
using System.Configuration;
using System.Data;


CoreLIB c = new CoreLIB();
private void btnLogin_Click(object sender, EventArgs e)
        {
            c.Username = txtUsername.Text;
            c.Password = txtPassword.Text;
            bool success = c.Login(c);
            if(success==true)
            {
                this.Hide();
                frmDashboard frm = new frmDashboard();
                frm.Show();
            }
            else
            {
                MessageBox.Show("Invalid Login");
            }
        }


IDE Used To Test This Code : Visual Studio 2012.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

How to declare connectionStrings in C# App.config file?


<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  
  <startup> 
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
  </startup>
  
  <connectionStrings>
    <add name="connstrng" connectionString="Data Source=.;Initial Catalog=database;Integrated Security=True;"/>
  </connectionStrings>
  
</configuration>

IDE Used To Test This Code : Visual Studio 2012.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

How to find all name start with N in table in Microsoft SQL Server


Database Name - demo
Table Name - employee

id name      phone      city       salary     company
1 suresh     8100011485 mumbai     20000      abc       
2 subhash    8511144785 kolkata    25000      bcd       
3 nitesh     5844411572 delhi      24000      nhc       
4 nirabh     1245810145 goa        26900      njo       
5 sourav     2544844152 kashmir    24120      lpf       
6 bonny      6245874125 bihar      24500      rfg

Code :
SELECT [id]
      ,[name]
      ,[phone]
      ,[city]
      ,[salary]
      ,[company]
  FROM [demo].[dbo].[employee]
  WHERE name LIKE 'n%'

Output :
id name phone city salary company
3 nitesh    5844411572 delhi      24000      nhc     
4 nirabh    1245810145 goa        26900      njo

IDE Used To Test This Code : Microsoft SQL Server Management Studio 2014

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

How to show first 3 rows of data in database in Microsoft SQL Server


Database Name - demo
Table Name - employee

id name      phone      city       salary     company
1 suresh     8100011485 mumbai     20000      abc       
2 subhash    8511144785 kolkata    25000      bcd       
3 nitesh     5844411572 delhi      24000      nhc       
4 nirabh     1245810145 goa        26900      njo       
5 sourav     2544844152 kashmir    24120      lpf       
6 bonny      6245874125 bihar      24500      rfg       

Code :
SELECT TOP 3 [id]
      ,[name]
      ,[phone]
      ,[city]
      ,[salary]
      ,[company]
  FROM [demo].[dbo].[employee

Output :
id name      phone      city       salary     company
1 suresh     8100011485 mumbai     20000      abc       
2 subhash    8511144785 kolkata    25000      bcd       
3 nitesh     5844411572 delhi      24000      nhc       

IDE Used To Test This Code : Microsoft SQL Server Management Studio 2014.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.

Program to calculate Factorial of a Number in C++


Code : 
#include<iostream>
using namespace std;
int main(){
int number, factorial = 1, i;
cout<<"Enter a positive number: ";
cin>>number;
for(i=1;i<=number;i++) {
factorial = factorial *i;
}
cout<<"Factorial is: "<<factorial;
return 0;
}

Output :
Enter a positive number: 3
Factorial is:6

IDE Used To Test This Code : Dev C++.

Try this code in your computer for better understanding. Enjoy the code. If you have any Question you can contact us or mail us.We will reply you as soon as possible.