Sunday, 23 August 2015

INSERT-3 TIER

INSERT
DAL:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;


public class insertdal
{
      SqlCommand cmd;

      connectiondal conobj = new connectiondal();

      public void insertdetails(string name, int age, System.DateTime dob)
      {
            cmd = new SqlCommand("stp_insert", conobj.Connection);
            cmd.CommandType = CommandType.StoredProcedure;

            try {

                  cmd.Parameters.AddWithValue("@name", name);
                  cmd.Parameters.AddWithValue("@age", age);
                  cmd.Parameters.AddWithValue("@dob", dob);
                  cmd.ExecuteNonQuery();


            } catch (Exception ex) {
                  throw ex;
            }


      }

}
BAL:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
public class insertbal
{
      insertdal obj = new insertdal();
      public void insertdetails(string name, int age, System.DateTime dob)
      {
            obj.insertdetails(name, age, dob);
      }
}
PL:


using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.SqlClient;
public class Form1
{
      insertbal OBJ = new insertbal();

      private void Button1_Click(System.Object sender, System.EventArgs e)
      {
            OBJ.insertdetails(TextBox1.Text, TextBox2.Text, DateTimePicker1.Value);

      }
}


Wednesday, 19 August 2015

Fastest method to trim all whitespace from Strings in .NET

 Method 1

        static Regex whitespace = new Regex(@"\s+");

        private void btnOK_Click(object sender, EventArgs e)
          
        {
            string str = txt1.Text;
            txt2.Text = whitespace.Replace(str, "");
        }


Method 2


 private void btnOK_Click(object sender, EventArgs e)
                 
 {
            string str = txt1.Text;
            txt2.Text = string.Join("", str.Split(default(string[]), StringSplitOptions.RemoveEmptyEntries));
       
}


Occurance Of A string

<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
   
    </div>
    </form>
</body>


    protected void Button1_Click(object sender, EventArgs e)
        {
            int occuranceCount = 0;

            //Checking Occurance
            occuranceCount = Regex.Matches(TextBox1.Text.ToLower().Trim(), TextBox2.Text.ToLower().Trim()).Count;
            if (occuranceCount > 0)
                Label1.Text = "The given string pattern <i>" + TextBox2.Text + "</i> Contains " + occuranceCount + " times in  <i>"
                    + TextBox1.Text + "</i><br/><br/> Thank you for visiting. Please Visit Again!!!";
            else
                Label1.Text = "The given string pattern <i>" + TextBox2.Text + "</i> Is not occured  in  <i>"
                    + TextBox1.Text + "</i><br/><br/> Thank you for visiting. Please Visit Again!!!";
            //End

        }

Tuesday, 11 August 2015

Upload image to a pictureBox in Windows Forms

// open file dialog 
 OpenFileDialog open = new OpenFileDialog(); 
 // image filters
 open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg; *.jpeg; *.gif; *.bmp"; 
 if (open.ShowDialog() == DialogResult.OK)
{
   // display image in picture box
   pictureBox1.Image = new Bitmap(open.FileName);

  } 

Monday, 10 August 2015

Insert To DB

cmd = new SqlCommand("stp_insert", conobj.Connection);
cmd.CommandType = CommandType.StoredProcedure;

try {

cmd.Parameters.AddWithValue("@name", name);
cmd.Parameters.AddWithValue("@age", age);
cmd.Parameters.AddWithValue("@dob", dob);
cmd.ExecuteNonQuery();


} catch (Exception ex) {
throw ex;
}

Connection Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using System.Data;
using System.Data.SqlClient;

namespace stockexpert1.Datalayer
{
    public class connection
    {
        SqlConnection con = new SqlConnection("Data Source=AABASOFT-02;Initial Catalog=stockexpert;Integrated Security=True");
        public SqlConnection serverConnection()
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            return con;


        }
    }

}

Tuesday, 28 July 2015

Checking future date in DateTimePicker

 private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {
            DateTime check;

            bool valid = DateTime.TryParse(dateTimePicker1.Text, out check);
            if (valid && (check > DateTime.Now))
            {
                lblMesage.Text = "Future Date is not Allowed";
            }
        }