Friday, 5 February 2021

Insert Checkbox List Value into Database using XML

 --step 1:Create table

create table tblAddressProof(id int identity(1,1)primary key,name varchar(50),add_proof_id int,add_prof_value int)


--step 2: create proc

alter proc pr_saveAddressProof (

@p_name varchar(50)=null,

@p_xml xml=null,

@p_mode INT = null

)

as

begin

if @p_mode=0 --for insert

begin

insert into tblAddressProof(name,add_proof_id,add_prof_value)

select @p_name,

addressproofdetails.value('@add_proof_id','varchar(100)') AS add_proof_id,

addressproofdetails.value('@add_prof_value','varchar(100)') AS add_prof_value

from

@p_xml.nodes('/addressdetails/addressproofdetails') as temptable(addressproofdetails)

end

else if @p_mode=1 --for update

begin

delete from tblAddressProof where name=@p_name


insert into tblAddressProof(name,add_proof_id,add_prof_value)

select @p_name,

addressproofdetails.value('@add_proof_id','varchar(100)') AS add_proof_id,

addressproofdetails.value('@add_prof_value','varchar(100)') AS add_prof_value

from

@p_xml.nodes('/addressdetails/addressproofdetails') as temptable(addressproofdetails)

end

else if @p_mode=2 --for search

begin

select add_proof_id,add_prof_value from tblAddressProof where name=@p_name

end

end



exec pr_saveAddressProof

@p_name='Vicky',

@p_xml='<addressdetails>

<addressproofdetails add_proof_id="1" add_prof_value="0"></addressproofdetails>

<addressproofdetails add_proof_id="2" add_prof_value="0"></addressproofdetails>

<addressproofdetails add_proof_id="3" add_prof_value="1"></addressproofdetails>

</addressdetails>'



select *from tbladdressproof


--step 3: Write Code for Front End

<!DOCTYPE html>


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

<head runat="server">

    <title></title>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" />

    

</head>

<body>

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

        <div class="card-header text-center">

            <h3>Insert Checkbox List Value into Database using XML</h3>

        </div>

        <table class="col-3 table table-responsive" align="center">

            <tr>

                <td>Name</td>

                <td>

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

                </td>

            </tr>

            <tr>

                <td>Address Proof</td>

                <td>

                    <asp:CheckBoxList ID="cbAddressList" runat="server">

                        <asp:ListItem Value="1">Aadhar Card</asp:ListItem>

                        <asp:ListItem Value="2">Driving Licence</asp:ListItem>

                        <asp:ListItem Value="3">Voter ID</asp:ListItem>

                    </asp:CheckBoxList>

                </td>

            </tr>

            <tr>

                <td>&nbsp;</td>

                <td>

                    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" />

                </td>

            </tr>

        </table>

    <div>

    

    </div>

    </form>

</body>

</html>


--Step 4: Write code for Back end (Code behind page)

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

{

    SqlConnection con;

    SqlCommand cmd;

    protected void Page_Load(object sender, EventArgs e)

    {

        string str = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

        con = new SqlConnection(str);

        if (!IsPostBack)

        {            

            BindAddress();

        }

    }


    protected void btnSubmit_Click(object sender, EventArgs e)

    {

        con.Open();

        cmd = new SqlCommand("pr_saveAddressProof", con);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@p_name", txtName.Text);

        cmd.Parameters.AddWithValue("@p_xml", GetXMLForAddressProof());

        cmd.Parameters.AddWithValue("@p_mode", 0);

        cmd.ExecuteNonQuery();

        Response.Write("<script language=javascript>alert('Record Inserted');</script>");

        con.Close();

    }

    string GetXMLForAddressProof()

    {

        StringBuilder xml = new StringBuilder();

        xml.Append("<addressdetails>");

        foreach (ListItem item in cbAddressList.Items)

        {

            if (item.Selected)

            {                

                xml.AppendFormat("<addressproofdetails add_proof_id={0} add_prof_value={1}>", "\""+ item.Value+ "\"", "\"" + 1 + "\"");

                xml.Append("</addressproofdetails>");

            }

            else

            {                               

                xml.AppendFormat("<addressproofdetails add_proof_id={0} add_prof_value={1}>", "\"" + item.Value + "\"", "\"" + 0+ "\"");

                xml.Append("</addressproofdetails>");

            }

        }

        xml.Append("</addressdetails>");

        return xml.ToString();

    }


    protected void btnUpdate_Click(object sender, EventArgs e)

    {

        con.Open();

        cmd = new SqlCommand("pr_saveAddressProof", con);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@p_name", txtName.Text);

        cmd.Parameters.AddWithValue("@p_xml", GetXMLForAddressProof());

        cmd.Parameters.AddWithValue("@p_mode", 1);

        cmd.ExecuteNonQuery();

        Response.Write("<script language=javascript>alert('Record Updated');</script>");

        con.Close();

    }


    protected void btnSearch_Click(object sender, EventArgs e)

    {

        BindAddress();

    }

    public void BindAddress()

    {        

        con.Open();

        cmd = new SqlCommand("pr_saveAddressProof", con);

        cmd.CommandType = System.Data.CommandType.StoredProcedure;

        cmd.Parameters.AddWithValue("@p_name", txtName.Text);

        cmd.Parameters.AddWithValue("@p_mode", 2);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();

        da.Fill(ds);

        con.Close();

        

        if (ds.Tables[0].Rows.Count > 0)

        {

            

            for (int i = 0; i < cbAddressList.Items.Count; i++)

            {                

                string a = ds.Tables[0].Rows[i]["add_prof_value"].ToString();

                if(a=="1")

                {

                    cbAddressList.Items[i].Selected = true;

                }

                else

                {

                    cbAddressList.Items[i].Selected = false;

                }

            }            

        }

    }

}




Thursday, 30 January 2020

Collection in C#

As we know, variable holds a single value. C# includes specialized that hold many values and objects. Collection classes are specialized classes for data storage and retrieval. These classes provide support for stacks, queues, lists, and hash tables
There are two types of collections available in C#: non-generic collections and generic collection

   Non-Generic                                       Generic
1. ArrayList                                                         1. List
2. HashTable                                                        2. Dictionary
3. SortedList                                                        3. SortedList
4. Stack                                                                4. Stack
5. Queue                                                               5. Queud


Array:- Type Safe but fixed length

Collection:- Auto Resizing but not type safe
Generic Collection:- Auto Resizing and type safe

Generic Collection:- A generic collection is strongly typed (you can store one type of objects into it)


Example Code of each collections and Generic

class CollectionInCSharp
    {
        public static void Main()
        {
            Console.WriteLine("Non-Generic          Generic");
            Console.WriteLine("ArrayList----------->List");
            Console.WriteLine("HashTable----------->Dictionary");
            Console.WriteLine("SortedList---------->SortedList");
            Console.WriteLine("Stack--------------->Stack");
            Console.WriteLine("Queue--------------->Queue");
            //Collections:- Varrying length but not fixed type
            //ArrayList
            Console.WriteLine("******************************Collection Examples******************************");
            Console.WriteLine("------------Array List------------");
            ArrayList al = new ArrayList();
            al.Add("Parag");
            al.Add(31);
            al.Add("Nagpur");
            al.Add(45000);

            foreach (object o in al)
            {
                Console.WriteLine(o);
            }


            //HashTable
            Console.WriteLine("------------Hash Table------------");
            Console.WriteLine("DictonaryEntry: is a class whose object represents the data in a combination of key & value pairs");
            Hashtable ht = new Hashtable();
            ht.Add("Name", "Parag");
            ht.Add("Age", 31);
            ht.Add("City", "Nagpur");
            ht.Add("Salary", 45000);
            ht.Add("Married", "Yes");
            foreach(DictionaryEntry d in ht)
            {
                Console.WriteLine(d.Key+":"+d.Value);
            }


            //SortedList
            Console.WriteLine("------------Sorted List------------");
            SortedList sl = new SortedList();
            sl.Add("A", "Apple");
            sl.Add("C", "Cat");
            sl.Add("D", "Dog");
            sl.Add("E", "Elephant");
            sl.Add("B", "Ball");
            foreach(DictionaryEntry d in sl)
            {
                Console.WriteLine(d.Key + ":" + d.Value);
            }


            //Stack
            Console.WriteLine("------------Stack------------");
            Stack s = new Stack();
            s.Push("Bottle");
            s.Push("Keyboard");
            s.Push("Mouse");
            s.Push(45000);
            //s.Pop();
            foreach (object o in s)
            {
                Console.WriteLine(o);
             
            }


            //Queue
            Console.WriteLine("------------Queue------------");
            Queue q = new Queue();
            q.Enqueue("Mobile");
            q.Enqueue("Bottle");
            q.Enqueue(30000);
            q.Enqueue("Keyboard");
            //q.Dequeue();
            foreach (object o in q)
            {
                Console.WriteLine(o);

            }


            //Generic:- Specific Type, ArraySize is not fixed, Elements can be added/removed at runtime
            Console.WriteLine("************************************Generic Examples************************************");
            Console.WriteLine("------------List<T>------------");
            List<int> obj = new List<int>();
            obj.Add(13);
            obj.Add(08);
            obj.Add(1988);
            foreach (int i in obj)
            {
                Console.WriteLine(i);
            }

            Console.WriteLine("------------Dictionary<int,string>------------");
            Dictionary<int, string> dict = new Dictionary<int, string>();
            dict.Add(1, "Sunday");
            dict.Add(2, "Monday");
            dict.Add(3, "Tuesday");
            dict.Add(4, "Wednesday");
            dict.Add(5, "Thusday");
            dict.Add(6, "Friday");
            dict.Add(7, "Saturday");
            foreach (KeyValuePair<int, string> kvp in dict)
            {
                Console.WriteLine(kvp.Key+":"+kvp.Value);
            }


            Console.WriteLine("------------SortedList<string,string>------------");
            SortedList<string, string> sl1 = new SortedList<string, string>();
            sl1.Add("A", "Apple");
            sl1.Add("B", "Ball");
            sl1.Add("C", "Cat");
            foreach(KeyValuePair<string,string> kvp in sl1)
            {
                Console.WriteLine(kvp.Key+":"+kvp.Value);
            }

            Console.ReadKey();
        }

    }

Wednesday, 4 December 2019

Interface

A interface is also a user-defined type. Interface can have methods, properties, events, and indexers as its members. But interfaces will contain only the declaration of the members. Interface contains only Abstract Methods (Methods without method body)
Every abstract method  of an interface should be implemented by the child class of the interface without fail (Mandatory).

Note: A class can inherit from a class and interface at a time.
Note: We can't declare any fields/variables under an interface.

The default scope of the member of an interface is public whereas its private in case of a class. By default every member of an interface is abstract so we don't require to use abstract modifier again just like we do in case of abstract class.

Code Example:
interface ITestInterface1
    {
        void add(int a,int b);
    }
    interface ITestInterface2 : ITestInterface1
    {
        void sub(int a, int b);
    }

    class ImplementationClass : ITestInterface2
    {
        public void sub(int a, int b) //public modifier is mandatory
        {
            Console.WriteLine(a - b);
        }

        public void add(int a, int b)
        {
            Console.WriteLine(a + b);
        }
        static void Main(string[] args)
        {
            ImplementationClass obj = new ImplementationClass();
            obj.add(10, 20);
            obj.sub(20, 10);

            //Or we can create an parent instance using child reference
            ITestInterface2 i = obj; //i is an parent instance creating using child "obj" reference
            i.add(30, 40);
            i.sub(30, 10);
            Console.ReadLine();
        }

    }

Abstract class in c#

An abstract class in an incomplete class or special class and we can't be instantiated. The purpose of abstract class is to provide a blueprint for derived classes and set some rules what the derived classes must implement when they inherit an abstract class.
1. Cannot create an instance of abstract class
2. If a class is an abstract class then it can contain abstract methods and non-abstract methods.
3. If we declare method as a abstract in base class (abstract class) then it is mandatory for all derived class to implement abstract methods.

Abstract Class Contains:
--Abstract Methods
--Non-abstract Methods

Code Example:-

public abstract class AbstractClassExample
    {
        public abstract void add(int a, int b); //abstract method
        public abstract void sub(int a, int b); //abstract method

        public int div(int a, int b) //non-abstract method of parent class
        {
            return a / b;
        }
    }

 
    class ChildClass : AbstractClassExample
    {
        public override void add(int a, int b)  //mandatory to implement
        {
            Console.WriteLine("Addition:" + (a + b));
        }
        public override void sub(int a, int b)  //mandatory to implement
        {
            Console.WriteLine("Substraction:" + (a - b));
        }

        public void mul(int a, int b)   //method of child class
        {
            Console.WriteLine("Multiplication:" + (a * b));
        }
        static void Main(string[] args)
        {
            //AbstractClassExample abc = new AbstractClassExample(); //cannot create the instance of abstract class

            ChildClass c = new ChildClass();
            c.add(10, 20);
            c.sub(20, 10);
            Console.WriteLine("Division:" + c.div(20, 4));
            c.mul(10, 5);
            Console.ReadLine();
        }
    }


Output:



Wednesday, 30 January 2019

Install R and RKWard KDE in Ubuntu

To install R language in Ubuntu do the following steps:

1. Open Terminal
          Press Alt+Ctrl+T
2. Type "sudo apt-get install r-base" then press enter


3. To run R statistical package, execute R in the Terminal


4. In ubuntu, To install RStudio search for RKWard KDE on ubuntu software center, do the following:

  1. Open Ubuntu Software Center
  2. Search for RKWard
  3. Then click Install



Tuesday, 28 August 2018

SQL Queries

Scalar Function:- Oracle Scalar Functions allow you to perform different calculations on data values. There are different types of Scalar Functions.

1. String Function - functions that perform operations on character values.
2. Numeric Function - functions that perform operations on numeric values.
3. Date Function - functions that perform operations on date values.
4. Conversion Function - functions that convert column data types.
5. NULL-related Function - functions for handling null values.

Oracle String Functions:-
1. CONCAT - Returns text strings concatenated
     select concat('Hello','World') from dual;

2. INSTR - Returns the location of a substring in a string.
     select instr('parag','r') from dual;

3. LENGTH - Returns the number of characters of the specified string expression.
     select length('parag') from dual;

4. RTRIM - Returns a character string after truncating all trailing blanks.
     select rtrim('  parag      ') from dual;

5. LTRIM - Returns a character expression after it removes leading blanks.
    select ltrim('    parag    ') from dual;

6. REPLACE - Replaces all occurrences of a specified string value with another string value.
    select replace('hello','e','$') from dual;

7. REVERSE - Returns the reverse order of a string value.
    select reverse('parag') from dual;

8. SUBSTR - Returns part of a text.
     select substr('hello',2,3) from dual;

9. LOWER - Returns a character expression after converting uppercase character data to lowercase.
     select lower('PARAG') from dual;

10. UPPER - Returns a character expression with lowercase character data converted to uppercase.
     select upper('parag') from dual;

Oracle Numeric Functions:-
1. TRUNC - Returns an integer that is less than or equal to the specified numeric expression.
    select trunc(69.9) from dual;

2. CEIL - Returns an integer that is greater than, or equal to, the specified numeric expression.
    select ceil(69.1) from dual;

3.ROUND - Returns a numeric value, rounded to the specified length or precision.
    select round(69.9) from dual;

How to insert date and time in oracle?
Using to_date() function we can insert date and time in oracle. To insert date and time we need to use date as a data type.
CREATE TABLE WorkOn ( StaffNo NCHAR(4), MechanicName VARCHAR(50), DateTime DATE, Hours VARCHAR(2) ) ;
insert into WorkOn values('101','Parag',to_date('22/06/2018 8:30:00AM','DD/MM/YYYY HH24:MI:SS'),3);