Sunday, 26 March 2023

Data binding with grid view

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

Create procedure Studententry  

(  

@fName Varchar (50),  

@MName varchar (50),  

@LName Varchar (50)  

  

)  

as  

begin  

Insert into StudentNew (Fname,MName,LName) values (@fName,@MName,@LName)  

End  


select * from StudentNew;

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


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

<head runat="server">

    <title></title>

</head>

<body bgcolor="#ccccff">

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

    <h4 style="color: #800080"> Employee Search Directory Created by Vithal wadje for C# corner</h4>

     <div>

        <table class="style1">

            <tr>

                <td class="style3" style="color: #800000; font-size: large;">

                Search</td>

                <td class="style2">

                 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

                 </td>

                <td>

                    <asp:Button ID="Button1" runat="server" Text="Go" onclick="Button1_Click" />

                </td>

            </tr>

           

        </table>

    <p> 

        <asp:Label ID="Label1" runat="server" Text="Label" ForeColor="Maroon"></asp:Label></p>

    </div>

    <div>

     <asp:GridView ID="GridView1" runat="server"  BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" 

        CellPadding="3" CellSpacing="2" Height="90px" Width="260px">

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

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

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

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

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


        </asp:GridView>


    </div>

    </form>

</body>

</html>

 


using System;

using System.Data;

using System.Configuration;

using System.Data.SqlClient;


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

{

    public SqlConnection con;

    public string constr;


    public void connection()

    { 

     constr=ConfigurationManager.ConnectionStrings["dbcs"].ToString();

      con = new SqlConnection(constr);

        con.Open();

    

    }


    protected void Page_Load(object sender, EventArgs e)

    {


        Label1.Visible = false;

    

    

    }


                    public void rep_bind()

                    {

                        connection();

                        string query = "select * from StudentNew where FName like'" + TextBox1.Text + "%'";


                        SqlDataAdapter da = new SqlDataAdapter(query, con);

                        DataSet ds = new DataSet();

                        da.Fill(ds);

                        GridView1.DataSource = ds;

                        GridView1.DataBind();

                    }


  

    protected void Button1_Click(object sender, EventArgs e)

    {

        connection();


        string query = "select FName  from StudentNew where FName like'" + TextBox1.Text + "%'";

        SqlCommand com = new SqlCommand(query, con);


        SqlDataReader dr;

        dr = com.ExecuteReader();



        if (dr.HasRows)

        {

            dr.Read();


            rep_bind();

            GridView1.Visible = true;


            TextBox1.Text = "";

            Label1.Text = "";

        }

        else

        {

            GridView1.Visible = false;        

            Label1.Visible = true;

            Label1.Text = "The search Term " + TextBox1.Text + " &nbsp;Is Not Available in the Records"; ;

        

        }




    }

}



Grid view Add

 







Crurd operation in Asp.net



 using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Data.SqlClient;


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

{

    SqlConnection con;

    SqlCommand cmd;

    String sql, name, f_name, add,qua, gen, dob,mail,id,pho;

    protected void Page_Load(object sender, EventArgs e)

    {

        con = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\Database.mdf;Integrated Security=True;User Instance=True");

        con.Open();


    }

    protected void Button1_Click(object sender, EventArgs e)

    {

        id = id_txt.Text;

        name = name_txt.Text;

        f_name = fname_txt.Text;

        add = add_txt.Text;

        pho = phone_txt.Text;

        dob = dob_txt.Text;

        mail = mail_txt.Text;

        qua += DropDownList1.SelectedItem.Text.ToString();

        gen = RadioButtonList1.SelectedItem.Value.ToString();

        sql = "insert into log values('"+id+"','" + name + "','" + f_name + "','" + add + "','" + pho + "','" + gen + "','" + qua + "','" + dob + "','"+mail+"')";

        cmd = new SqlCommand(sql, con);

        cmd.ExecuteNonQuery();

        id_txt.Text = "";

        name_txt.Text = "";

        fname_txt.Text = "";

        add_txt.Text = "";

        phone_txt.Text = "";

        dob_txt.Text = "";

        mail_txt.Text = "";

        Response.Write("Value Saved Successfully");


    }

    protected void Button2_Click1(object sender, EventArgs e)

    {

        sql = "delete from log where ID='" + id_txt.Text + "'";

        cmd = new SqlCommand(sql, con);

        cmd.ExecuteNonQuery();

        Response.Write("Record is Deleted");

    }

    protected void Button3_Click(object sender, EventArgs e)

    {

        SqlDataReader dr;

        int MyId;

        MyId = int.Parse(id_txt.Text);

        sql = "select * from log where ID='" + MyId + "'";

        cmd = new SqlCommand(sql, con);

        dr = cmd.ExecuteReader();

        if (dr.Read() == true)

        {

            name_txt.Text = dr["Name"].ToString();

            fname_txt.Text = dr["F_Name"].ToString();

            add_txt.Text = dr["Address"].ToString();

            phone_txt.Text = dr["Phone"].ToString();

            RadioButtonList1.SelectedValue = dr["Gender"].ToString();

            DropDownList1.SelectedItem.Text = dr["Qualification"].ToString();

            dob_txt.Text = dr["DOB"].ToString();

            mail_txt.Text = dr["Mail"].ToString();



        }


        else

            Response.Write("value not found");

        name_txt.Text = " ";

        fname_txt.Text = " ";

        add_txt.Text = " ";

        phone_txt.Text = " ";

        dob_txt.Text = " ";

        mail_txt.Text = " ";

    }

    protected void Button4_Click(object sender, EventArgs e)

    {

        sql = "update log set Name='" + name_txt.Text + "',F_Name='" + fname_txt.Text + "',Address='" + add_txt.Text + "',Phone='" + phone_txt.Text + "',Gender='" + RadioButtonList1.SelectedItem.Value + "',Qualification='" + DropDownList1.SelectedItem.Text + "',DOB='" + dob_txt.Text + "',Mail='" + mail_txt.Text + "' where ID='"+id_txt.Text+"'";

        cmd = new SqlCommand(sql, con);

        cmd.ExecuteNonQuery();

        Response.Write("Record Successfully Updated");

    }

    protected void Button5_Click(object sender, EventArgs e)

    {

        if (FileUpload1.HasFile)

        {


            string FileName = "";

            string FilePath = "";

            FilePath = Server.MapPath("~\\photo\\");

            FileName = FileUpload1.FileName;

            FilePath += FileName;

            FileUpload1.PostedFile.SaveAs(FilePath);


            Image1.ImageUrl = "~/photo/" + FileName;


            Label1.Visible = true;

            Label1.Text = "Record Display & Save";



        }


        else

        {

            Label1.Text = "You have not specified a file.";

        }

    }

}



Friday, 20 December 2019

C# Enumeration


An enumeration is used in C# programming language to define a constant set of values. Example, the days of the week can be defined as an enumeration and used anywhere in the program. In C#, the enumeration is defined with the help of the 'enum' keyword



Example:

class Program 
 {
  enum Days{Sun,Mon,tue,Wed,thu,Fri,Sat};
  
  static void Main(string[] args) 
  {
   Console.Write(Days.Sun);
   
   Console.ReadKey();
  }
 }




Output: Sun

Friday, 11 August 2017

What is Indexer and why we use in C#.

C# has introduced  a new concept Indexer.Indexer an object to be indexed in the same way as an array.Indexer modifier can be private, public, protected or internal.The return type can be any valid C# types.Indexers in C# must have at least one parameter. Else the compiler will generate a compilation error at runtime error. Indexers are always created with this keyword.Indexers are implemented through the get and set accessors for the [ ] operator.








Syntax-----

Public <return type> this[<parameter type> index]
{
    
Get
{
        // return the value from the specified index

    }

    Set
{
       
 // set values at the specified index

    }
}

Example. 
using System;
using System.Collections;

class MyClass
{
    private string[] data = new string[5];
    public string this[int index]
    {
        get
        {
            return data[index];
        }
        set
        {
            data[index] = value;
        }
    }
}

class MyClient
{
    public static void Main()
    {
        MyClass mc = new MyClass();
        mc[0] = "Rakesh";
        mc[1] = "A3-126";
        mc[2] = "shambhu";
        mc[3] = "Pappu";
        mc[4] = "Mumbai";
        Console.WriteLine("{0},{1},{2},{3},{4}", mc[0], mc[1], mc[2], mc[3], mc[4]);
        Console.Read();
    }

}

output:
  

Friday, 28 July 2017

Collection ,Stacks, queues, lists, and hash Tables in C#.

Collection classes are specific classes for data storage and retrieval.Collections are similar to Arrays, it provide a more flexible way of working with a group of object. These classes provide maintain for stacks, queues, lists, and hash tables. Collections are similar to Arrays, it provide a more flexible way of working with a group of objects.



There are the normally two types of Collections in .Net Frame work.

 1.Non-generic     2.  Generic
1  .In non-genrics each element can represent a value of a different type. In this non-genric elements can be added / removed at runtime & most important thing is array Size are  not fixed.

2 .Generic Collections work on the specific type that is specified in the program whereas non-generic Stacks, queues, lists, and hash tables are also known as Generic Collections.Collections work on the object type.

1.1:. Specific type(Like int ,string etc)
1.2: Array Size is not fixed
1.3: Elements can be added / removed at runtime.

A.Stacks .B Queues C. lists.D Hash tables.

A: The stack is a unique case collection which represents a last in first out (LIFO) concept. We can say that we have a special type of collection which stores elements in LIFO form (Last In First Out) in C#.  C# includes a generic and non-generic Stack.
Stack allows null value and also duplicate values.
It provide various method for their functionality methrod name are:
It provides a Push() method to add a value and Pop() or Peek() methods to retrieve values.
Declaration of the stack – A stack is produced with the help of the Stack Data type.
The keyword "new" is used to create an object of a Stack. 

Stack st = new Stack().

Adding  values into Stack:

The Push() method adds values into the Stack.

Stack.push(element) & Peek()

The Peek() method returns the last (top-most) value from the stack. Calling Peek() method on empty stack will throw InvalidOperationException. 

Pop():

You can also retrieve the value using the Pop() method. The Pop() method removes and returns the value that was added last to the Stack.
 The Pop() method call on an empty stack will raise an InvalidOperationException
The Contains() method checks whether the specified item exists in a Stack collection or not. It returns boolean valve.

Clear:

The Clear() method removes all the values from the stack.

Count – This property is used to get the number of things in the Stack..

Stack.Count

Contains - This method is used to see if an element is present in the Stack.

Contains(element).


Example:

using System;
using System.Collections;

public class Program
{
    public static void Main()
    {
        Stack myStack = new Stack();  //The keyword "new" is used to create an object of a Stack. 
        myStack.Push(1);       //      The Push() method adds values into the Stack.
        myStack.Push(2);
        myStack.Push(3);
        myStack.Push(4);
        myStack.Push(5);

        Console.WriteLine("total elements in Stack are: {0}", myStack.Count);

        while (myStack.Count > 0)       //You can also retrieve the value using the Pop() method
            Console.WriteLine(myStack.Pop());
   

 Console.Write("total elements in Stack are after result: {0}", myStack.Count);

   //Count – This property is used to get the number of things in the Stack..
        Console.Read();
    }

}


Output:




Advertisement