Search (Article Or Program)

07 October 2014

Read & Insert Data Using Stored Procedures in C#

This article will show you in a simple manner how to read data from SQL data base by using stored procedures and how to insert data through a stored procedure.

Background

There is a description to explain how to write stored procedures as well. Since it is a complex area for beginners I have explained it in very simple manner.

Using the code

Step 1 : Create the table(user_tab) to insert data
CREATE TABLE [dbo].[user_tab](
    [U_name] [varchar](50) NULL,
    [U_pwd] [varchar](50) NULL,
    [U_type] [varchar](10) NULL
)

Step 2 : Create the stored procedure to insert data to user table
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE AddUser
    @name varchar(50),
    @pwd varchar(50),
    @type varchar(10)
AS
BEGIN

    SET NOCOUNT ON;

    insert into user_tab (U_name,U_pwd,U_type)
    values(@name,@pwd,@type)

END
GO

Step 3 :Add windows form
Create a windows form to enter the details which we are going to save in database.
textbox1=> to enter name
textbox2=> to enter password
combobox => to select user type
button(btnSave)=> to save data to database through stored procedure


Step 3 : Add a class called DBConnect
class DBConnect
    {
        public static SqlConnection myCon = null;

        public void CreateConnection()
        {
            myCon = new SqlConnection("Data Source=GAYANI-PC;Initial Catalog=soft1;Integrated Security=True");
            myCon.Open();

        }
    }

Step 4 : Button click event
Click on button. Then add this code inside that event.
private void btnAdd_Click(object sender, EventArgs e)
        {
            SqlConnection con = DBConnect.myCon;
            SqlCommand cmd = con.CreateCommand();
            cmd.CommandText = "Execute AddUser @name,@pwd,@type";

            cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value = textBox1.Text.ToString();
            cmd.Parameters.Add("@pwd", SqlDbType.VarChar, 50).Value = textBox2.Text.ToString();
            cmd.Parameters.Add("@type", SqlDbType.VarChar, 10).Value = comboBox1.Text.ToString();
            
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
            
        }
You can insert data to the database now ! Check it.

Step 5 : Stored procedure to Read data
GO
SET QUOTED_IDENTIFIER ON
GO

CREATE PROCEDURE ReadUser
    
AS
BEGIN
    
    SET NOCOUNT ON;

    select U_name,U_type from user_tab
END
GO

Step 6 : Read data through Stored procedure(ReadUser) and display on a grid view
public void LoadGrid()
        {
            SqlConnection con = DBConnect.myCon;
            SqlDataReader rd;

            using(con)
            {
                SqlCommand cmd = new SqlCommand("ReadUser",con); // Read user-> stored procedure name
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    dataGridView1.Rows.Add(rd[0].ToString(), rd[1].ToString()); //gridview has 2 columns only(name, type)
                }
                rd.Close();
            }
            con.Close();
        }

Step 7 : Call above method to form load event or any button click event.
private void Form1_Load(object sender, EventArgs e)
        {
            this.LoadGrid();           
        }
 Now you can read and insert data through a stored procedure.


, 6 Oct 2014  on codeproject

30 January 2014

15 hot programming trends -- and 15 going cold


Programming trends that might surprise the boss, but shouldn't surprise you in the year ahead

Programmers love to sneer at the world of fashion where trends blow through like breezes. Skirt lengths rise and fall, pigments come and go, ties get fatter, then thinner. But in the world of technology, rigor, science, math, and precision rule over fad.
That's not to say programming is a profession devoid of trends. The difference is that programming trends are driven by greater efficiency, increased customization, and ease-of-use. The new technologies that deliver one or more of these eclipse the previous generation. It's a meritocracy, not a whimsy-ocracy.
What follows is a list of what's hot -- and what's not -- among today's programmers. Not everyone will agree with what's A-listed, what's D-listed, and what's been left out. But that's what makes programming an endlessly fascinating profession: rapid change, passionate debate, sudden comebacks.
Hot: Preprocessors
Not: Full language stacks

24 January 2014

How to write a scientific calculator with an implementation in c++


Table of contents 

1. Abstract 
2. Introduction
3. Coding Mathematics' problems 
3.1. Inputs' wide possibilities 
3.2. Agreement of input formulas with standard rules of math.
3.3. Length of input formula  
 4. Overview on the Implementation
4.1. Data class
4.2.   MyChecker class 
4.3.   MyFixer class 
4.4.   Calculator class  
5. How to use the implementation  
6. Appendix  
6.1. Linked lists  
6.2. Bisection method    
7. conclusion   

 

1. Abstract  

Have you tried before to put the upper formula in one of your program, but you found its hard to calculate it? Have you stopped codding one of your programs because you need to treat with complex formulas and their isn't a route to calculate it in your programming language? have you gone to use programming language such as fortran or mathlab, because you need to use some of its mathematical power, as the same time if you have this power in your main language you may produce a better program? If you are one these slides I have told about, this article will be good for you inshallah as its give you two solutions to your problems; the first is how to program mathematical programs and the second a header file in c++ that you can use in order to calculate formula such as which in Fig.1, in order to facility your creative c++ program.   

2. Introduction  

Mathematical programming is the backbone of any programming language and the one of the most important reasons to invite computers and with subordination programming language, that is because all science in the world are depending on mathematics, so needing of easier routes to do it is continue one of the most required things in these days and will still obtainable until the end of the world, so from this point we are starting in order to implement a scientific calculator coded by myself me and will be coded by your self in the coming days inshallah, except you don't wanna.
In c++ there is some libraries work on mathematics such as math.h and c math, but these libraries don't give you all operations you may need, but at the same time they are the soul which we plant our implementation on.   

19 December 2013

Shoot'em Up .NET


Before Introduction

If you only want to download the code and see what's happening, then the important things are:
  • There's a solution for Silverlight 5 and one for WPF, but most units are simply used by both;
  • The game uses the directional keys to move, Space to shot and either EscP or Enter to pause;
  • The game has three levels. Between the levels you may acquire upgrades to your ships, so if you think the initial ships are too weak, well, that's on purpose.
You can play the Silverlight game by opening this link:http://paulozemek.azurewebsites.net/ShootEmUpTestPage.2013_10_10.html
Or you can download the WPF version by clicking on the next image:

Introduction

I know that many programmers would love to write games. I myself started to program computers because I wanted to create games and, even if I already wrote some games, I've spent most of my time dealing with systems and solving problems related to database connectivity and better caching mechanisms.
One of the problems I see to write games is the lack of material. In my particular case, I usually look for graphics and sounds, as those are the things that I am not able to do. Yet, I think there's missing material for games in general. It is hard to find game tutorials and, when I find something, I only see material to write games using XNA or Unity. There is almost no material on how to write games using WPF or Silverlight or other similar technologies.
And, considering that I usually write games using those technologies, I though it will be a good idea to present a solution on how to write games using non-game specific technologies.

Why not use XNA or Unity?

My idea on writing games without using XNA or Unity is to show that you can use normal UI technologies to write games, which in many cases simplify the job as you don't need to care about how to render things to the screen or to recreate layout controls. Also, by doing this you will be able to create games without having to install such libraries/engines and you will be able to write games even for Windows 8, as using C# + XAML(which is what I am going to use in this article) is a trait of SilverlightWPF and of Windows 8 applications.

16 September 2013

Create Tic Tac Toe Game


Hello Guys, I am going to show you How to Create Tic Tac Toe for 2-Players.
Before Going to start, a short description on What is Tic Tac Toe?
Tic-tac-toe (or Noughts and crossesXs and Os) is a paper and pencil for two players, X and O, who take turns marking the spaces in a 3×3 grid. The player who succeeds in placing three respective marks in a horizontal, vertical, or diagonal row wins the game.

Let's Start:

Create a New project and select windows application.Give it name and then press enter.(All of us know this step).
After that you see a blank form like that:
image1
Now add 9 buttons on your form using tools.
image2

Select button then delete the text from properties(i.e. button1,button2).Actually we want no text on buttons.
Now, Put a label on your form set backcolor according to your wish(here i choose skyblue color from properties), set textalign to middlecenter and set text to "Turn".Actually We are creating a box for showing the player's Turn.

11 September 2013

Short Intro of OOP and Its Basic Principle

What is OOP?
OOP is a design philosophy. It stands for Object Oriented Programming. Object-Oriented Programming (OOP) uses a different set of programming languages than old procedural programming languages (C, Pascal, etc.). Everything in OOP is grouped as self sustainable "objects". Hence, you gain re-usability by means of four main object-oriented programming concepts.
In order to clearly understand the object orientation, let’s take your “hand” as an example. The “hand” is a class. Your body has two objects of type hand, named left hand and right hand. Their main functions are controlled/ managed by a set of electrical signals sent through your shoulders (through an interface). So the shoulder is an interface which your body uses to interact with your hands. The hand is a well architected class. The hand is being re-used to create the left hand and the right hand by slightly changing the properties of it.

Basic Principle of oops:- There are main three core principles of  any object oriented languages.
  • ENCAPSULATION:- Encapsulation provides the ability to hide the internal details of an object from its users.The concept of encapsulation is also known as data hiding or information hiding. In c# , Encapsulation is implemented using the access modifier keywords.
  1. Public
  2. Private
  3. protected
  4. Internal
  5. Protected Internal
    • POLYMORPHISM:- It is also concept of oops. It is ability to take more than one form. An operation may exhibit different behaviour in different situations.                       Ex:-  An addition operation involving two numeric values will produce sum and the same addition operation will produce a string.If we pass parameter numeric value then method return sum(numeric value) and if we pass parameter  String then same method is return string value .this called Polymorphism.
    • INHERITANCE:-  One class can include the feature of another class by using the concept of inheritance.In c# a class can be inherit only from one class at a time.Whenever we create class that  automatic inherit from System.Object class,till the time the class is not inherited from any other class.

    Easy Guide to Method Overriding in C#


    Method Overriding in C#

    When one class inherit from another class ,all the member of parent class become the member of child class .If there is a method of parent class that we want to predefined in child class. we can implement the concept of method overriding ,it means a method overriding of parents class and child class can have same method name with same parameter but parent class method will have "VIRTUAL"Keyword and child class method have "OVERRIDE"Keyword.


    EXAMPLE:-
    ?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    using System;
    namespace methodoverriding
    {
        class Program
        {
            static void Main(string[] args)
            {
                employee obj = new employee();
                obj.display();
                Console.ReadLine();
            }
        }
    public class cls
    {
        public virtual void display()
        {
            Console.WriteLine("hello");
        }
    }
    public class employee : cls
    {
        public override void display()
        {
            Console.WriteLine("welcome");
        }
    }
    }

    Description:- Here ,I have maked the object of employee class. The display method of only employee class will get in memory,because the "display method" of cls  class has been over-hidden by the employee class.

    Follow these steps to run this program:-

    Step1:- Open your visual studio and go File->New Project->Click Console Application.
    See it:-



    Step2:- copy the whole program code in program.cs file.
    See it:-

    Step3:-  Now Run the Application(Press F5).
    OUTPUT:-


    I hope this is helpful for you

    ref: http://www.msdotnet.co.in/2012/06/method-overriding-in-c.html