Thursday, 18 January 2018

Learn C language Part 39 Structures in C language





#include <stdio.h>

#include <conio.h>

#include <string.h>

struct employee

{

int id ;

char name[50];

float salary;

char department[50] ;



};



main()

{



struct employee e1;

struct employee e2;



e1.id=101;

strcpy(e1.name,"Ali");

e1.salary=50000;

strcpy(e1.department,"HR");



//2ND RECORD..........................



e2.id=102;

strcpy(e2.name,"BILAL");

e2.salary=60000;

strcpy(e2.department,"IT");





//PRINTING........................



printf("Employee ID: %d\nName of Employee  : %s \nSalary:%f\nDepatment: %s",e1.id,e1.name,e1.salary,e1.department);



printf("Employee ID: %d\nName of Employee  : %s \nSalary:%f\nDepatment: %s",e2.id,e2.name,e2.salary,e2.department);













}

part 27-SQL injection and prevention In asp.net



backend code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication17
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string q = "select * from tblProductInventory";
                displayrecord(q);
            }
           
        }
        

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(cs);
            try
            {
                SqlCommand cmd = new SqlCommand("spsqlinjectionofproduct", con);
                cmd.Parameters.AddWithValue("@productname", TextBox1.Text);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                GridView1.DataSource = rdr;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write("ERROR IS FOUND " + ex);
            }
            finally
            {
                con.Close();
            }
           
            
        }



         public void displayrecord(string q)
        {

            SqlConnection con = new SqlConnection(cs);
            try
            {
                SqlCommand cmd = new SqlCommand(q, con);
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                GridView1.DataSource = rdr;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write("ERROR IS FOUND " + ex);
            }
            finally
            {
                con.Close();
            }

        }




    }
}

Front end code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication17.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Width="268px"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />

        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Height="441px" Width="861px">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
        </asp:GridView>

        <h1>

            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </h1> 

    </div>
    </form>
</body>
</html>

web.config code:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

<connectionStrings>
  <add name="test" connectionString="Data Source=faculty-35;Initial Catalog=ADODOTNET_LEARNING;Persist Security Info=True;User ID=sa;Password=aptech" providerName="System.Data.SqlClient"/>  

</connectionStrings>  

</configuration>



part 27-SQL injection and prevention In asp.net



backend code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WebApplication17
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        string cs = ConfigurationManager.ConnectionStrings["test"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string q = "select * from tblProductInventory";
                displayrecord(q);
            }
           
        }
        

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection(cs);
            try
            {
                SqlCommand cmd = new SqlCommand("spsqlinjectionofproduct", con);
                cmd.Parameters.AddWithValue("@productname", TextBox1.Text);
                cmd.CommandType = CommandType.StoredProcedure;
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                GridView1.DataSource = rdr;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write("ERROR IS FOUND " + ex);
            }
            finally
            {
                con.Close();
            }
           
            
        }



         public void displayrecord(string q)
        {

            SqlConnection con = new SqlConnection(cs);
            try
            {
                SqlCommand cmd = new SqlCommand(q, con);
                con.Open();
                SqlDataReader rdr = cmd.ExecuteReader();
                GridView1.DataSource = rdr;
                GridView1.DataBind();
            }
            catch (Exception ex)
            {
                Response.Write("ERROR IS FOUND " + ex);
            }
            finally
            {
                con.Close();
            }

        }




    }
}

Front end code:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication17.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="TextBox1" runat="server" Width="268px"></asp:TextBox>
        <asp:Button ID="Button1" runat="server" Text="Search" OnClick="Button1_Click" />

        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Height="441px" Width="861px">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
        </asp:GridView>

        <h1>

            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </h1> 

    </div>
    </form>
</body>
</html>

web.config code:

<?xml version="1.0"?>

<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->

<configuration>
    <system.web>
      <compilation debug="true" targetFramework="4.5" />
      <httpRuntime targetFramework="4.5" />
    </system.web>

<connectionStrings>
  <add name="test" connectionString="Data Source=faculty-35;Initial Catalog=ADODOTNET_LEARNING;Persist Security Info=True;User ID=sa;Password=aptech" providerName="System.Data.SqlClient"/>  

</connectionStrings>  

</configuration>



Wednesday, 17 January 2018

Part-26 Sql Data Reader in asp.net with Try and Catch exception





back end code :



using System;

using System.Collections.Generic;

using System.Data.SqlClient;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;



namespace ADO1

{

    public partial class ado2 : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

            SqlConnection con = new SqlConnection("Data Source=FACULTY-35;Initial Catalog=ADODOTNET_LEARNING;User ID=SA;Password=aptech");

            try

            {

                SqlCommand cmd = new SqlCommand("SELECT * FROM TBL_PRODUCT", con);

                con.Open();

                SqlDataReader rdr = cmd.ExecuteReader();

                GridView1.DataSource = rdr;

                GridView1.DataBind();

            }

            catch (Exception ex)

            {



                Response.Write("Error is " + ex);

            }

            finally

            {

                con.Close();

            }







        }

    }

}

Front end code :



<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ADO1.aspx.cs" Inherits="ADO1.ADO1" %>



<!DOCTYPE html>



<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2">

            <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />

            <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />

            <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />

            <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />

            <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />

            <SortedAscendingCellStyle BackColor="#FFF1D4" />

            <SortedAscendingHeaderStyle BackColor="#B95C30" />

            <SortedDescendingCellStyle BackColor="#F1E5CE" />

            <SortedDescendingHeaderStyle BackColor="#93451F" />

        </asp:GridView>

    </div>

    </form>

</body>

</html>

web.config code :

<?xml version="1.0" encoding="utf-8"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
  
    <connectionStrings>
      <add name="testdbms" connectionString="Data Source=FACULTY-35;Initial Catalog=ADODOTNET_LEARNING;User ID=SA;Password=aptech" providerName="System.Data.SqlClient"/>
    </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.5" />
    <httpRuntime targetFramework="4.5" />
  </system.web>
</configuration>

HOW ATM MACHINE WORKS (A prototype code in C language)





#include <stdio.h>

#include <conio.h>



main()

{

l3:

int amount[3]{1000,2000,3000};

int code[3]{1234,2134,5555};

int c,i,index=-1,input;

l1:

printf("Enter the Code: ");

scanf("%d",&c);



for(i=0;i<3;i++)

{



if(c==code[i])

{

index=i;

}





}



if(index==-1)

{

printf("\nINVALID CODE.......\nEnter the code again........\n");

goto l1;

}

l2:

printf("\n-------------------------------------\n");

printf("\t\twelcome to ATM");

printf("\n-------------------------------------\n");

printf("press 1 for Deposite Amount---->\n");

printf("press 2 for Cash With Drawl---->\n");

printf("press 3 for Check Balance---->\n");

scanf("%d",&input);



switch(input)

{



case 1:

printf("Enter the amount that you want to deposite : ");

int deposite;

scanf("%d",&deposite);

amount[index]=amount[index]+deposite;

printf("%d credited in your account %d \n",deposite,&amount[index]);

break;





case 2:

printf("Enter the amount that you want to draw : ");

int draw;

scanf("%d",&draw);

if(draw>amount[index])

{

printf("YOU ARE EXCEEDING YOUR BALANCE AMOUNT......\n");

}

else{



amount[index]=amount[index]-draw;

printf("%d credited in your account %d \n",draw,&amount[index]);

}

break;





case 3:

printf("Your Balance Amount is %d",amount[index]);

break;



default:





break;



}





printf("\nDo you want to Continue.....(y/n)\n");

char e;

e=getchar();

scanf("%c",&e);

if(e=='y')

{

goto l2;

}

else

{

index=-1;

goto l3;

}



}

Tuesday, 16 January 2018

Learn C language Part 38 Pointers of Pointers in C language





#include <stdio.h>

#include <conio.h>



main()

{



int value=100;

int *ptr;

int **pofptr;





ptr=&value;

pofptr=&ptr;





printf("value of variable is %d \n",value);

printf("value of variable through ptr %d \n",*ptr);

printf("value of variable through pofptr %d ",**pofptr);











}

Learn C language Part 38 Pointers of Pointers in C language





#include <stdio.h>

#include <conio.h>



main()

{



int value=100;

int *ptr;

int **pofptr;





ptr=&value;

pofptr=&ptr;





printf("value of variable is %d \n",value);

printf("value of variable through ptr %d \n",*ptr);

printf("value of variable through pofptr %d ",**pofptr);











}

Pass Dynamically Added Html Table Records List To Controller In Asp.net MVC

Controller Code: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using ...