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.