Monday 23 June 2014

Code To Insert Data From Any Class using Sp Automated

1)      No tension of Class Type for Calling Sp
2)      Write 1 function for Data Base insertion and call that from  other classes .
3)      No tension for remembering column names.
4)      No Lengthy code
5)      Write small function
6)      Write Stored Procedure
7)      Just Remember : Create a class that must has the property  equivalent to table column name.
8)      Same name of the property and equivalent data type  then you don’t need to type cast by extra coding.
Code Like This Will Make a Structure of Your Application That will enable you to write small Code.
That powerful function is :
  public static Boolean ExecuteSp(List<Object> paramlist, String SpName)
        {

    SqlConnection Conn = new SqlConnection(DbAcccess.ConnectionString());
            try 
            {
                foreach (var param in paramlist)
                {
                   
                    SqlCommand Comb = new SqlCommand(SpName, Conn);
                    Comb.CommandType = CommandType.StoredProcedure;
                    Type ObjectType = param.GetType();
                    IList<PropertyInfo> props = new   List<PropertyInfo>(ObjectType.GetProperties());

                    foreach (PropertyInfo ItemProp in props)
                    {
                        object Propvalue = ItemProp.GetValue(param, null);
Comb.Parameters.Add("@" + ItemProp.Name.ToString(),ItemProp.PropertyType).Value = Propvalue;
                    }
                    Conn.Open();
                    Comb.ExecuteNonQuery();
                    Conn.Close();
                }
                return true;


            }
            catch (Exception Ex)
            {
                return false;
            }
            finally
            {
                Conn.Close();
            }
        }



To Work with this code you have to import the name space  System.Reflection.
This Namespace has the definition  of PropertyInfo.
Using this you can just get the value of the properties , name and type of the properties. This will do the magic for you.
You will pass the List of objects and SP name.

That means If you want to save more than 1 objects in a single function you don’t need to write loop explicitly you just pass the array of object and your work will be done.

Sample User Saving Code:
       public String SaveUser(clsUser paramUser)
        {
            List<object> lstUser = new List<object>();
            lstUser.Add(paramUser);
            Boolean Res = DbAcccess.ExecuteSp(lstUser, StaticSpName.SpInsertUser);
            if (Res == false)
            {
                return "Error In Saving User.";
            }
            else
            {
                return "User Saved Successfully.";
            }
  }

Here I am saving single object .You can save multiple objects using List<object>.


Try this code and please give your comments

Monday 17 February 2014

.net code generator

How to generate the classes with .net code generator?
Working with .net code generator in some easy steps:
Then install the software

Run the software:
Click on the button MS access 2003.
Click on the select The Database Button.

Select the Database.
After selection the tables will come in the grid in this way
Now save to any folder .

Then Press OK.



Now your class will be generated and ready to work with your application.
NOW THE QUESTION IS HOW?

This is my application screen:
Now if you want your class to work just add existing Item in this manner.

YOUR CLASS IS NOW READY.
Please use pk_ for Primary key in all tables and try to make them integer or auto number.
Pk_  is mandatory to generate properly all classes and its related function.

The below class is auto generated.
And it will going to generate for all your tables.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace GeneratedModuleClasses
{
    class tblClassDetails
    {
        public System.Int32 pk_intRecId { get; set; }
        public System.Int32 intRoll { get; set; }
        public System.String strClass { get; set; }
        public System.String strDesc { get; set; }
        public System.DateTime strDate { get; set; }
        public System.Byte[] Image { get; set; }
        public System.String hyperLink { get; set; }
        public Boolean Create(String Conn)
        {
            try
            {
                DataTable DT = new DataTable();
                DT.Clear();
                OleDbDataAdapter ADP = new OleDbDataAdapter("Select * from tblClassDetails", Conn);
                ADP.Fill(DT);
                DataRow Row = DT.NewRow();
                Row["pk_intRecId"] = pk_intRecId;
                Row["intRoll"] = intRoll;
                Row["strClass"] = strClass;
                Row["strDesc"] = strDesc;
                Row["strDate"] = strDate;
                Row["Image"] = Image;
                Row["hyperLink"] = hyperLink;
                DT.Rows.Add(Row);
                System.Data.OleDb.OleDbCommandBuilder Comb = new System.Data.OleDb.OleDbCommandBuilder(ADP);
                ADP.Update(DT);
            }
            catch (Exception Ex)
            {
                System.Windows.Forms.MessageBox.Show(Ex.ToString());
                return false;
            } return true;
        }
        public static Boolean Edit(Int32 Pk_value, tblClassDetails EditObject, String Conn)
        {
            try
            {
                DataTable DT = new DataTable();
                DT.Clear();
                OleDbDataAdapter ADP = new OleDbDataAdapter("Select * from tblClassDetails where pk_intRecId=pk_intRecId", Conn);
                ADP.Fill(DT);
                if (DT.Rows.Count > 0)
                {
                    DataRow Row = DT.Rows[0];
                    Row["intRoll"] = EditObject.intRoll;
                    Row["strClass"] = EditObject.strClass;
                    Row["strDesc"] = EditObject.strDesc;
                    Row["strDate"] = EditObject.strDate;
                    Row["Image"] = EditObject.Image;
                    Row["hyperLink"] = EditObject.hyperLink;

                    System.Data.OleDb.OleDbCommandBuilder Comb = new System.Data.OleDb.OleDbCommandBuilder(ADP);
                    ADP.Update(DT);
                }
                else
                {
                    return false;
                }
            }
            catch (Exception Ex)
            {
                System.Windows.Forms.MessageBox.Show(Ex.ToString());
                return false;
            } return true;
        }
        public static List<tblClassDetails> GetnerateList(String Conn)
        {
            List<tblClassDetails> tblClassDetails = new List<tblClassDetails>();
            DataTable DT = new DataTable();
            DT.Clear();
            OleDbDataAdapter ADP = new OleDbDataAdapter("Select * from tblClassDetails", Conn);
            ADP.Fill(DT);
            foreach (DataRow Row in DT.Rows)
            {
                tblClassDetails ob = new tblClassDetails(); try
                {
                    ob.pk_intRecId = (System.Int32)Row["pk_intRecId"];
                }
                catch (Exception Ex) { } try
                {
                    ob.intRoll = (System.Int32)Row["intRoll"];
                }
                catch (Exception Ex) { } try
                {
                    ob.strClass = (System.String)Row["strClass"];
                }
                catch (Exception Ex) { } try
                {
                    ob.strDesc = (System.String)Row["strDesc"];
                }
                catch (Exception Ex) { } try
                {
                    ob.strDate = (System.DateTime)Row["strDate"];
                }
                catch (Exception Ex) { } try
                {
                    ob.Image = (System.Byte[])Row["Image"];
                }
                catch (Exception Ex) { } try
                {
                    ob.hyperLink = (System.String)Row["hyperLink"];
                }
                catch (Exception Ex) { } tblClassDetails.Add(ob);
            }
            return tblClassDetails;
        }
    }
}


DOWNLOAD THE CODE FROM HERE : https://drive.google.com/folderview?id=0B3IxuTdeDTLTN1puUXZnVk9MT0U&usp=sharing

I am creating an automated tool step by step.
I need proper feed back. Then those people who really need this kind of tool could send me their feedback.








Thursday 9 January 2014

Extension Method in .net C#

Extension Method:

[There is no hole in my piggy bank but the money is still being inserted]
Extension Method is that kind of feature. Suppose we have a base class Employee.
This class should not be modified but I need to insert some methods in the Employee class.
No need for deriving the Employee class.
Now what is the solution?
The solution is Extension Method. Extension methods enable you to add methods to existing types without creating new derived type, recompiling or modifying the original type. Extension Methods are a special kind of static method.
Example:-
I will add the following method to String class in my program without derivation or modifying the String Class.
public static String GetFirstThreeCharacters(this String str)
        {

            if (str.Length < 3)
            {
                return str;
            }
            else
            {
                return str.Substring(0, 3);
            }
        }
Just  I have to add a static class like this .
public static class Extension
    {
        public static String GetFirstThreeCharacters(this String str)
        {

            if (str.Length < 3)
            {
                return str;
            }
            else
            {
                return str.Substring(0, 3);
            }
        }
    }

Then I can use the  GetFirstThreeCharacters in this manner in my Form Load Event.
   private void Form1_Load(object sender, EventArgs e)
        {
            String st = "My Name is Arindam";
            MessageBox.Show(st.GetFirstThreeCharacters());
        }