Friday, July 13, 2012

To check is the Text Property has some value inside of it, and its not null, - C#


if(!String.IsNullOrEmpty(textBox.text)
{
   //code here
}
else
   MessageBox.Show(Please insert some text...");


//or:
if(textBox.Text != Stirng.Empty)
{
   //code here
}
else
   MessageBox.Show(Please insert some text...");

Sunday, July 8, 2012

Retrieve System Date to a String C#

String s = DateTime.Now.ToString("dd/MM/yyyy");
MessageBox.Show(s);

Read all the Records in a Database to a GridView



 private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection connection = new SqlConnection();

            SqlDataAdapter dataAdapter;
            DataSet dataSet;
            connection.ConnectionString = Trial.Program.Globals.db.connectionString;

            connection.Open();

            dataAdapter = new SqlDataAdapter("select * from Item", connection);

            dataSet = new DataSet();
            DataTable t = new DataTable();

            dataAdapter.Fill(t);
            dgvAllItems.DataSource = t;

            connection.Close();  }

Index ID incrementer for writing new Data to SQL using C# form



private void Form1_Load(object sender, EventArgs e)
        {
            SqlDataAdapter dataAdapter;
            DataSet dataSet;
            //IEnumerator iEnumerator;
            SqlConnection connection = new SqlConnection(Trial.Program.Globals.db.connectionString);
            dataAdapter = new SqlDataAdapter("select * from Cust", connection);
            dataSet = new DataSet();
            dataAdapter.Fill(dataSet);
            int rows = dataSet.Tables[0].Rows.Count;          
            //MessageBox.Show("No. of Rows are : " + rows.ToString());
            int rowcount = rows + 1;
            txtCustNo.Text = rowcount.ToString();
        }


this code helps to generate the index number which is there to to add data next... :)

Retrieve Data from a Database ( Data Adapter / Data Reader) without using a DataGridView


//using System.Data.SqlClient; //header

using (SqlConnection connection = new SqlConnection(Trial.Program.Globals.db.connectionString))
using (SqlCommand command = new SqlCommand(" select itemName from Item ", connection))
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                           listBox1.Items.Add(reader["itemName"].ToString());
                        }
                    }
                    reader.Close();
                }
                connection.Close();
            }

here you can use a data reader(SqlDataReader) instead of using a data adapter(SqlDataAdapter).

Thursday, July 5, 2012

Live Running System Clock in C# form


public partial class parent : Form
{
        Timer clock;

        public parent()
        {
            InitializeComponent();    
        }

       private void parent_Load(object sender, EventArgs e)
       {
           clock = new Timer();
           clock.Interval = 1000;
           clock.Start();
           clock.Tick += new EventHandler(Timer_Tick);
       }

     
       public void Timer_Tick(object sender, EventArgs eArgs)
       {  
           if (sender == clock)
           {
              lblLiveUp.Text = DateTime.Now.ToString();
           }
       }
}

Make a Form a Parent (MDIParent) for a child form in C#

Type this inside the event which you call the child MDI form
from the parentMDI form.

Inside Parent Form:

            frmSearchCust frmCSearchCust = new frmSearchCust();
            frmCSearchCust.MdiParent = this;
            frmCSearchCust.StartPosition = FormStartPosition.CenterScreen;
            frmCSearchCust.Show();

Read from a database table (SQL) in C#

using System.Data.SqlClient;

private void button1_Click(object sender, EventArgs e)
{
            //to read from DB
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Thisal Lekamge\Desktop\Shohan\new\ChainMan.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            using (SqlConnection connection = new SqlConnection(connectionString))
            using (SqlCommand command = new SqlCommand(" select * from Item ", connection))
            {
                connection.Open();

                using (SqlDataReader reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            MessageBox.Show(reader["itemName"].ToString());
                            MessageBox.Show(reader["itemDescription"].ToString());
                        }
                    }
                    reader.Close();
                }
                connection.Close();
            }


}

Write to a SQL Database using C#

using System.Data.SqlClient;

private void btn_ok_Click(object sender, EventArgs e)
{
            //write to db
            string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Thisal Lekamge\Desktop\Shohan\new\ChainMan.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO Item (itemName, itemDescription) VALUES (@itemName, @itemDescription);");

                cmd.CommandType = CommandType.Text;
                cmd.Connection = connection;

                cmd.Parameters.AddWithValue("@itemName", textBox3.Text);
                cmd.Parameters.AddWithValue("@itemDescription", textBox5.Text);

                connection.Open();

                cmd.ExecuteNonQuery();

                connection.Close();
              }

}

Where & How to create a global variable in for a Connection String in C#

Declare your Global Variable in "Program.cs" file


public static class Globals
{
            public static class db
            {
                public const string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Thisal Lekamge\Desktop\Shohan\new\ChainMan.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";
            }
 }


Use it as follows in other .cs files

Trial.Program.Globals.db.connectionString "

Here;



Trial is the name of the Visual Studio Project
Program stands as its a programme.