-->

Saturday, December 20, 2014

How to insert data into multiple table in ASP.NET

I have two table, such as complaint table and status table. Now, i want to add data in both table through same SqlCommand class instance. Now, first to prepare the Connection using SqlConnection class, after that design the query for first table after inserting the data, we can add data into other table.



You can check the complete.

Source Code

<table style="width:100%;">
    <tr>
        <td class="style3">
            Complaint against To</td>
    </tr>
    <tr>
        <td class="style1">
            Name :&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pname" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator7" runat="server" 
                ControlToValidate="pname" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Business&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pbusiness" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator8" runat="server" 
                ControlToValidate="pbusiness" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Address&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="padd" runat="server" Height="61px" Width="248px" 
                TextMode="MultiLine" ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator9" runat="server" 
                ControlToValidate="padd" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Phone Number&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pnumber" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator10" runat="server" 
                ControlToValidate="pnumber" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Faulty Proff&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pfproff" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator11" runat="server" 
                ControlToValidate="pfproff" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Faulty Proff Image&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:FileUpload ID="pproffimage" runat="server" />
        </td>
    </tr>
    <tr>
        <td class="style1">
            Review Against&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="preview" runat="server" Height="55px" Width="248px" 
                TextMode="MultiLine" ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator12" runat="server" 
                ControlToValidate="preview" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style1">
            Fir Number (if any)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
            <asp:TextBox ID="pfir" runat="server" Height="24px" Width="245px" 
                ValidationGroup="cr"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RequiredFieldValidator13" runat="server" 
                ControlToValidate="pfir" ErrorMessage="Consumer Name Required" ForeColor="Red" 
                ValidationGroup="cr">*</asp:RequiredFieldValidator>
        </td>
    </tr>
</table>
<p>
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
</p>
<p>
    <asp:Label ID="Label1" runat="server"></asp:Label>
</p>

<asp:ValidationSummary ID="ValidationSummary1" runat="server" ForeColor="Red" 
    ValidationGroup="cr" />

Design View Of Source code

Complaint form

Complete Business Logic 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.SqlClient;
using System.Configuration;

public partial class UserControl_complaint : System.Web.UI.UserControl
{
    string savefile = string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string un=System.Guid.NewGuid ().ToString ();

        if (pproffimage.HasFile)
        {
            pproffimage.SaveAs(Server.MapPath("~/images/" + pproffimage.FileName));
            savefile = "~/images/" + pproffimage.FileName;

        }
        SqlConnection con = new SqlConnection();
        con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "insert into complaint(Party_Name,Business,Address,Faulty_proff,Image,Review,Fir_Numer,Consumer_Id,date,uniquet) values(@cuoname,@cuofname,@cuodob,@cuoadd,@cuoimage,@cuophone,@cuoemail,@cuoph,@cuodate,@cuoun)";
        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@cuoname",pname.Text);
        cmd.Parameters.AddWithValue("@cuofname",pbusiness.Text);
        cmd.Parameters.AddWithValue("@cuodob", padd.Text);
        cmd.Parameters.AddWithValue("@cuoadd",pfproff .Text);
        cmd.Parameters.AddWithValue("@cuoimage", savefile);
        cmd.Parameters.AddWithValue("@cuophone",preview.Text);
        cmd.Parameters.AddWithValue("@cuoemail", pfir .Text);
        cmd.Parameters.AddWithValue("@cuoph",Session["cons_id"].ToString());
        cmd.Parameters.AddWithValue("@cuodate", DateTime.Now.ToString ());
         cmd.Parameters.AddWithValue("@cuoun", un);
        int a = cmd.ExecuteNonQuery();
        if (a > 0)
        {
            Label1.Text = "Data Inserted Sucessfully";
            Label1.ForeColor = System.Drawing.Color.Green;
        }
        cmd.Parameters.Clear();

        cmd.CommandText = "insert into status(status,Complaint_Id,consumer_id) values(@stname,@comid,@cunsid)";
        cmd.Parameters.AddWithValue("@stname", "pending");
        cmd.Parameters.AddWithValue("@comid",un);
        cmd.Parameters.AddWithValue("@cunsid", Session["cons_id"].ToString());
        cmd.ExecuteNonQuery();

    }
}

Friday, December 19, 2014

How to run Applet in Netbean IDE

We are very familiar with java programming, when we come to applet programming then we need some resources like browsers, AppletViewer etc to run the applet code. When i run the applet code in command prompt using following command:
appletviewer filename.html
 Applet viewer was not appear in windows 8.1. Then lastly i was decide that i shall install Netbean IDE for applet code.

import java.awt.*;
 import java.applet.*;

public class design extends Applet
 {
 public void paint(Graphics g)
{
g.drawString("Hello World",50,100);
}
}

Now, copy this code and following some instructions which is given in the youtube video

Monday, December 15, 2014

Prevention from SQL Injection attack in ASP.NET

In myPrevious article, we have already learn about SQL injection attack. We saw that if we use Text Box for retrieving data from the database then other queries also perform with the same database. So, Microsoft provide, Parameterized query for DML and DQL statements. Like
Replace this statement with the parameterized query

Direct Interface with TextBox (SQL Injection Attack Possible)

cmd.CommandText = "Select * from [TableName] where name='"+TextBox1.Text+"'";

Resolve this problem by the parameterized query

cmd.CommandText = "Select * from [TableName] where name=@name1";
cmd.Parameter.AddWithValue("@name1",TextBox1.Text);

Now that video give more clearance :   

Saturday, December 13, 2014

Program to add the numbers which are divisible by 5 in C

Let us write a program to print all the numbers between N1 and N2 which are divisible by 5. Then, compute the sum of those numbers. The numbers between N1 and N2 can be added using the following statement:

sum =0;
for(i=n1; i<=n2; i++)       /* i start from n1 and goes up to n2 */
{
sum =sum+i;                    /* Can be written as sum +=i */
}

But, it is required to add those which are divisible by 5 only. This can be achieved by taking only those values of i which are divisible by 5 and the program can be modified as follows:

sum =0;
for(i=n1; i<=n2; i++)
{
if(i%5 == 0)
{
sum +=i;
}
}

The complete algorithm are shown below:

Algorithm : DIVBY5.

[This algorithm prints the numbers divisible by 5 between n1 and n2]
Step 1: [Enter lower & upper limits]
Read : n1, n2

Step 2: [Initialize]
sum =0

Step 3: [Generate and add the number if divisible by 5]
for i=n1 to n2 in step 1
if(i%5 ==0)
sum = sum+i
[End of if]
[End of for]

Step 4: [Output the result]
Write : sum

Step 5: [Finished]
Exit.

The complete program to find the sum of all those numbers divisible by 5 is shown below:
Example : Program to add the numbers which are divisible by 5.
PROGRAM

#include<stdio.h>
main( )
{
int n1, n2, i, sum;
printf("Enter range N1 and N2 (N1 < N2): \n");
scanf("%d%d",&n1,&n2);
sum=0;
for(i=n1;i<=n2;i++)
{
if((i%5)==0)
{
printf("%d",i);
sum +=i;
}
}
printf("Total sum =%d\n",sum);
}

TRACING

Execution starts from here
Non-executable statement
Input
Enter the N1 and N2 (N1<N2):
4 20
Computations
sum=0
i= 5 10 15 20
Output
5 10 15 20
sum = 0+5+10+15+20

Total sum = 50

Friday, December 12, 2014

Program to find sum of squares of first N natural numbers in C

Let us design the algorithm and write the program to find the sum of the following series:
12+22+32+42+....................................................N2
We know the program segment to add all the numbers from 1 to N. The program segment is:
sum=0;
for(i=1; i<=n; i++)
{

sum = sum+i;

}
But, to add 12 , 22 , 32 , 42 , ...............N2    it is necessary to replace the statement:
sum = sum+i;

by the statement:

sum = sum+i*i;

The algorithm and flowchart to find the sum of squares of all natural numbers are as follows:

Algorithm : SERIESSUM

Step 1: [Input the number of terms]
Read: n
Step 2: [Initialization]
sum =0
Step 3: [Find the sum of all terms]
for i=1 to n in steps of 1 do
sum = sum + i*i

[End of for i]

Step 4: [Output the sum]
Write : sum
Step 5: [Finished]
Exit.

The complete program to find the sum of the given series is shown below:
Example : Program to add the series  12+22+32+42+....................................................N2

PROGRAM

#include<stdio.h>
main( )
{
int n, i, sum;
printf("Enter the number of terms \n");
scanf("%d",&n);
sum =0;
for(i=1; i<=n; i++)
{
sum = sum+(i*i);
}
printf("Sum of squares of numbers = %d",sum);
}

TRACING

Execution starts from here:
Non-executable statement
Input
Enter the number of terms
5
Computations
sum=0
              i= 1 2 3 4 5
sum =0+12+22+32+42+52
Output
Sum of squares of numbers =55

Thursday, December 11, 2014

Program to simulate calculator using switch in C

The input is the expression of the form (a op b) where a and b are the operands and op is an operator. For example 10+20. Based on the operator, the operation is performed and the result is displayed. The complete algorithm along with the flowchart is shown below:

Algorithm: CALCULATOR

Step 1: [Read expression of from (a+b)]
Read: a, op, b
Step 2: [perform the required operation]
switch(op)

case "+" : res = a+b
case "-" : res = a-b
case "*" : res= a*b
case "/" : if (b==0)
Write : "Divide by 0"
Exit
else

res = a/b
[End of if]
default : Write: ' Invalid operator'
Exit
[End of switch]
Step 3: [Output the result]
Write: res
Step 4: [Finished]
Exit.


The equivalent C program to perform various operations such as addition, subtraction, multiplication and division is shown below:

Program

#include<stdio.h>
#include<process.h>
main( )
{
int a, b, res;
char op; /* can be +, - , *,, or / */
printf("Enter the expression");
scanf("%d%c%d",&a, &op, &b);
switch(op)
{
case '+' :
res = a+b;
break;
case '-':
res= a-b;
break;
case '*':
res=a*b;
break;
case '/' :
if(b!=0)
res=a/b;
else
{
printf("Div by 0\n");
exit(0);
}
default:
printf("Invalid operator \n");
}
/* end of switch statement */
printf("%d %c %d = %d\n",a,op,b,res);  /* Display the result */
}

Tuesday, December 9, 2014

Program to find numbers within range n1 and n2 in C

Just now, we have seen, how to check whether a given number m is prime or not. Let us modify this program to generate prime numbers within the range n1 and n2.
Procedure: Assume m varies from n1 to n2. If m is prime let us display it. Otherwise, take the next number in the range and check for prime number. If it is prime, display it; otherwise, take the next number and repeat the procedure. Thus, all prime numbers are displayed in the range n1 to n2.

So, when m is prime, in place of displaying the message " The number is prime", let us print the prime number. If m is not prime, instead of displaying the message "The number is not prime", take the next number in the range. If these modifications are done to the program discussed in the previous section, then prime numbers within the range are generated. The modified program is shown below:

#include<stdio.h>
void main( )
{
int n, n1, n2, i, count, prime;
printf("Enter n1 and n2:\n");
scanf("%d%d",&n1,&n2);
count=0;
printf("Primes between %d to %d are: \n", n1, n2);
for (n=n1; n<=n2; n++)
{
prime =1; /* Assume m is prime */

for (i=2; i<=n/2; i++)
{
if(n%i == 0)
{
/* n is not prime */
prime =0;
break;
}
}
/* if m is not prime, take next m, */
if(prime  == 0) continue;
/* print the prime number */
printf("%d",n);
count++; /* Update the counter */
}
printf("No. of primes = %d\n",count);
}

Tracing 

Input 
Enter n1 and n2:
2 9
Computations
count = 0
m=2 3 4 5 6 7 8 9

4 6 8 9
are not prime numbers and they are skipped

Output
2 3 5 7
No. of primes = 4
© Copyright 2013 Computer Programming | All Right Reserved