Monday, 8 April 2013

gridview rowcommand with insert | update | delete in c#

gridview rowcommand with insert | update | delete in c#
In this article we will learn how to use grid view rowcommand event to insert, update and delete the record in asp.net with c#

now for that i have created table named as usertbl. and took three field first one is id , name and company.
same way three text box .to insert the value into table.
4    rajana    nit
38    dev    lolo
45    dev    lolol



using System;
using System.Configuration;
using System.Data;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI.WebControls;



public partial class _Default : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);


    protected void Page_Init(object sender, EventArgs e)
    {
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        display(); 

    }
    protected void btnsbmit_Click(object sender, EventArgs e) //for insertion
    {
        try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("insert_db", con);
            cmd.CommandType = CommandType.StoredProcedure;
           
            cmd.Parameters.AddWithValue("@nm", txtnm.Text);
            cmd.Parameters.AddWithValue("@comp", txtcom .Text );
            cmd.ExecuteNonQuery();
            cmd.Dispose();
        }
        catch { }

        finally
        {
            con.Close();
           
        }

        display();

    }
    protected void grd_RowCommand(object sender, GridViewCommandEventArgs e)
    {



        if (e.CommandName == "select")
        {

            ViewState["val"] = Convert.ToInt32(e.CommandArgument);
            GridViewRow row = (GridViewRow)(((LinkButton  )e.CommandSource).NamingContainer);
//taking gridview index so we can that row values.
            int selIndex = row.RowIndex;
            string last = grd.Rows[selIndex].Cells[2].Text;
            string comp = grd.Rows[selIndex].Cells[3].Text;
            txtnm.Text = last;
            txtcom.Text = comp;
         
      }
        if (e.CommandName =="del")
        {
            int del = Convert.ToInt32(e.CommandArgument);
            ViewState["val"] = del;
           SqlCommand cmd = new SqlCommand("delete from usertbl where id='" + del + "'", con);
            try
            {
                con.Open();
                cmd.ExecuteNonQuery();
                lblmsg.Text = "Record deleted succesfully";
            }
            catch { }
            finally
            {
                con.Close();
            }

            display();

           
        }

    }
    protected void btnupdate_Click(object sender, EventArgs e)
    {
        int i = 0;
        int last =Convert.ToInt32 ( ViewState["val"]);
        //string l1 = Replace(txtnm.Text, "", "-");
        string str = "update usertbl set name='" + txtnm .Text  + "', company ='" + txtcom .Text +"' where id='" + last + "'";
        con.Open();
        SqlCommand cndupdate = new SqlCommand(str, con);
        i=cndupdate.ExecuteNonQuery();
        if (i >= 1)
        {
            lblmsg.Text ="Record updated succesfully";
        }
        con.Close();
       
        display();

    }
    public  void  display()
    {
       
        DataSet ds = new DataSet();
        con.Open();
        SqlCommand cmd1 = new SqlCommand("select * from usertbl", con);
        SqlDataAdapter sda = new SqlDataAdapter(cmd1);
        sda.Fill(ds);
        ViewState["ds"] = ds;
        grd.DataSource = ds;
        grd.DataBind();
        con.Close();
    }

 
    protected void grd_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void grd_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataSet  ds = new DataSet ();
        ds=(DataSet )(ViewState["ds"]);
       // DataView dv = new DataView(ViewState ["ds"]);
       
    }
}





<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

  
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   <table>
   <tr><td>
   <asp:Label runat="server" ID="lbl">name</asp:Label></td>
   <td><asp:TextBox runat="server" ID="txtnm"></asp:TextBox></td>
   </tr>
    <tr><td>
   <asp:Label runat="server" ID="Label1">Company</asp:Label></td>
   <td><asp:TextBox runat="server" ID="txtcom"></asp:TextBox></td>
   </tr>
  <tr><td><asp:Button runat="server" ID="btnsbmit" onclick="btnsbmit_Click"
          Text="Submit" /></td> <td>
      <asp:Button runat="server" ID="btnupdate"
          Text="update" onclick="btnupdate_Click" Visible="False" /><td></tr>
          <tr><td><asp:Label runat ="server" ID="lblmsg"></asp:Label></td></tr>
   </table>
   <asp:GridView ID="grd" runat="server" onrowcommand="grd_RowCommand"
            onsorting="grd_Sorting">
   <Columns >
  
   <asp:TemplateField HeaderText ="Edit"  >
   <ItemTemplate >
  
<asp:LinkButton ID="edit" CommandName ="select" runat ="server" Text ="Edit"  CommandArgument  ='<%# DataBinder.Eval(Container, "DataItem.id") %>' ></asp:LinkButton>
<asp:LinkButton ID="delete" CommandName ="del" runat ="server" Text ="delete"  CommandArgument ='<%#DataBinder.Eval(Container,"DataItem.id") %>'></asp:LinkButton>
  </ItemTemplate>
     </asp:TemplateField>
   </Columns>
   </asp:GridView>
  
    </div>
    </form>
</body>
</html>


name
Company



Editidnamecompany
Edit delete 4rajananit
Edit delete 38devlolo
Edit delete 45devlolol

Thursday, 4 April 2013

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level. This error can be caused by a virtual directory

It is an error to use a section registered as   allowDefinition='MachineToApplication' beyond application level.  This error can be caused   by a virtual directory

It is an error to use a section registered as allowDefinition='MachineToApplication' beyond application level.  This error can be caused by a virtual directory not being configured as an application in IIS.
E:\project\myproject\web.config 68


solution :-
If you are getting error like this and after  running your application you are getting error
liker <authentication mode="Forms"/> so  i have Solution just check your project or website
which you have opened . may be there will another web.config file.
where you have pasted your application or website folder open only than folder don't open
sub folder .


Example you  want to open your primenumberprogram website ,means  primenumberprogram  is
your website folder and there is web.config file , you kept your primenumberprogram   folder
in mywebsite folder and you are opening mywebsite folder instead of primenumberprogram  
folder then you will get erro like It is an error to use a section registered as
allowDefinition='MachineToApplication' beyond application level.  This error can be caused
by a virtual directory not being configured as an application in IIS.

E:\project\myproject\web.config.

so check acccurately where you have kept your website and from where you are opening that folder.
hope it will work

Monday, 1 April 2013

Maximum request length exceeded. | asp.net | vb.net

Maximum request length exceeded. | asp.net | vb.net

i am designing crystal report , for single or less record its working proper ,
means it is displaying proper but at time of
displaying lacs record at a time its showing error

Maximum request length exceeded. 

so what i did , i just pasted one line code and its started working fine.
how to paste that code and where .
go into web.config file and search for <system.web>
 paste

<httpRuntime maxRequestLength="1000000" />
brfore closing system.web

<system.web>

other code ----------------
---------------------------
<httpRuntime maxRequestLength="1000000" />
</system.web>

i am sure it will work.

About maxRequestLength i think it will related something if we are posting  huge data to
server and it cause , so to avoid we provide  length='1000000'
The value must be inside the range 0-2097151.

Wednesday, 13 March 2013

Perfect Number | program for single number | 1 to 100 | in c# with source code | output

Perfect Number  | program for  single number | 1 to 100 | in c# with source code | output

Perfect Number  | single number | 1 to 100 | in c# with source code | output with explanation
In this program i have shown how to check whether entered number is perfect number or not.
Logic i have developed is so simple .Anyone can learn easily.
so first i will show you how to check perfect  number logic for single number.
Definition of perfect number:- Perfect number is number which is equal to  its factor's addition.
example 6 has 3 factor 3 and 2 and 1 now if i will add 1 +2+ 3=6 which is number itself.so , we can say
6 is perfect number.
now check for 8.
1,2,2,2 here if i will add 4 number i will get 1+2+2+2=7 which is not equal to 8.so, 8 is not perfect number.
Here is source code in c# .similar code we can write in c and other language just change the
printing syntax.

  protected void Page_Load(object sender, EventArgs e)
    {
        Response.Write("</br>");
        Response.Write("How to check The given number is Perfect number or not");
        Response.Write("</br>");
        Response.Write("----------------------------------------------------------");
        Response.Write("</br>");
        int num = 10;
        int sum = 0;
        for (int i = 1; i < num; i++)
        {
            if (num % i == 0) // finding its factor
            {
                sum = sum + i;
            }
        }
            if(sum==num)
            {
                Response.Write (num+ "is Perfect Number ");
            }
            else
            {
                Response.Write (num+ " is  Not Perfect Number ");

            }
       

    }


output:-
How to check The given number is Perfect number or not
----------------------------------------------------------
10 is Not Perfect Number


Now i want to print list of perfect number from 1 to 100 .
logic will be same just include one more for loop.




   protected void Page_Init(object sender, EventArgs e)
    {
        Response.Write("</br>");
        Response.Write ("Print List oF  perfect Number from 1 to 100");
        Response.Write("</br>");
        Response.Write("----------------------------------------------");
        Response.Write("</br>");
        int num = 100;
        int sum = 0;
        for (int j = num; j > 0;)
        {
            for (int i = 1; i < j; i++)
            {
                if (j % i == 0)
                {
                    sum = sum + i;

                }
               

            }
            if (j == sum)
            {
                Response.Write(j + " is Perfect Number");
                Response.Write("</br>");
            }
            j--;

            sum = 0;
           



        }

    }
     

output:-


Print List oF perfect Number from 1 to 100
----------------------------------------------
28 is Perfect Number
6 is Perfect Number

Tuesday, 12 March 2013

Sum Of series in c#.net source code with output and explanation

Sum Of series in c#.net source code with output and explanation

Sum Of series in c#.net source code with output and explanation
in this program we will see how to calculate sum of series till sum number like 10 , 20 , etc....
Now what we will do we will just take two value assign them as 0  and 1. so , at least we will get
some value at start.
In this article i have shown here how  to calculate sum of series in c# .net on Page Init
 event.


 protected void Page_Init(object sender, EventArgs e)
    {
       
     
       int a=0;   //here i have initialize two valus as 0 and 1.
        int b=1;
        int num=20;
        Response.Write("Sum Of series Before:"+num);
        Response.Write("</br>");
       int  sum=0;
        for (int j=1;j<=num;j++)
        {
            sum=a+b; //swappping.............
            a=b;
            b=sum;
            Response.Write(sum+"-");  // printing the values.......
        }

       
        }


OUTPUT
Sum Of series Before:20
1-2-3-5-8-13-21-34-55-89-144-233-377-610-987-1597-2584-4181-6765-10946  

IF YOU WILL TAKE NUM=10
THEN OUTPUT WILL

Sum Of series Before:10
1-2-3-5-8-13-21-34-55-89