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());
        }    



Saturday 21 December 2013

Cute Download Manager

Cute Download Manager

A free tool to download your files very quickly .It is not only free it is very useful .You can give your list of downloads here and download them sequentially one by one serially. Please select the location where you want to save the files. Multiple files in the same name will be overwritten.
A best thing of this software is that it is very light weighted and simple. If you are working in an office and you boss will kick you if you download something from office then just use Cute Download Manager. If you press the cross button then the download will run in background.
So just download and enjoy.




Wednesday 18 December 2013

Entity Framework In Some Simple Steps

Entity Framework: Entity Framework is the Hot-Cake of Today. This enables programmer a Bug free database operation using simple some commands. It is a Strong framework and easy to implement.

Definition : Entity framework is an Object Relational Mapper (ORM).It is basically generates business objects and entities according to database tables and provides the process for CRUD  (Create Operation, Read Operation , Update Operation  And Also  Delete Operation. )


Above diagram explain the structure of Entity Framework in the respect of .net Technology.
Then it is clear that Entity Framework resides on or above the ADO.net. That means we are using the Entity Framework as usual but the difference is that we are not handling the connection the command directly.
Process of adding an EDMX file: [Note: To use entity framework you must install the .net Entity Framework.]
Click the Finish button to Finish the operation.
Here I have Entered “Contact.edmx “ .
I have the following Field in My Table Contact.
To Add
A single Contact:
TestEntities DB = new TestEntities();
Contact Con = new Contact();[This is the object of Contact Model]
Con.lname = txtLname.Text.ToString();
Con.fname = txtFname.Text.ToString();
Con.phone = txtPhone.Text.ToString();
Con.Roll = txtRoll.Text.ToString();
TestEntities OB = new TestEntities();[TestEntities Object Will Handle The DataBase Operation And Will Generated Automatrically in The EDMX file]
OB.Contacts.AddObject(Con);[Adding a New Object For New Contact]
OB.SaveChanges();[This will affect the Real DataBase ]

Try your Self to Delete, and Update Data
If you made any changes in the database  then just :-
Click the Update Model from DataBase
Changes will be made automatically.








Friday 13 December 2013

Datatable To XML string in .net Using Gridview

I have Used a data Gridview to make my own  edited xml string which I will use in SQL server stored procedure as argument .
Now the code is here :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        DataTable DT = new DataTable();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            DT.Columns.Add("Name", typeof(string));
            DT.Columns.Add("Phone", typeof(string));
            DT.Columns.Add("Address", typeof(string));
            dataGridView1.DataSource = DT;
        }

        private void btnXml_Click(object sender, EventArgs e)
        {
            DataSet DS = new DataSet();
            DS.Tables.Add(DT);
            MessageBox.Show(DS.GetXml());
        }
    }
}




The result will be in XML

After this how you will use this XML string in SQL server that you can find in my article Using XML string in Stored Procedure(SP) in Sql Server