C# Advance Functions





Def:

public void fun_name(string s)
        {
        }

Calling:
fun_name("abcd");


public string fun_name(string s,string s2="kart")
        {
            return s = s +s2;
        }

Calling:
fun_name("abcd");
fun_name("abcd",”12345”);

public  string fun_name(params object[] values)
        {
            return values[0].ToString();
        }

Calling:
fun_name("abcd");
fun_name(50);
fun_name(50.5568);
fun_name("abcd",50,50.2145);

public static class ObjectExtensions
    {
        /// <summary>
        /// convert object to string with null safe and it return string
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
    
        public static string ToSafeString(this object obj)
        {
            return obj != null ? obj.ToString() : String.Empty;
        }
        /// <summary>
        /// convert object to string with null safe, if can't return 0
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static string ToSafeString_0(this object obj)
        {
            return obj != null ? obj.ToString() : "0";
        }
    }


Calling:
.ToSafeString();


int a=null;
a.ToSafeString();
a.ToSafeString_0();

windows form application C# with SQL Database

windows form application C# with SQL Database




SQLCE
Queries protection from injection
Group of Queries execute in single Transaction
---------------------------------------------------------------------


private void button1_Click(object sender, EventArgs e)
        {

            SqlCeCommand[] cmd = new SqlCeCommand[500];
            for (int i = 0; i <3; i++)
            {               
                cmd[i] = new SqlCeCommand();               
                cmd[i].CommandText = "insert into kart(name, city)values(@name,@city)";
                cmd[i].Parameters.AddWithValue("@name", name_txt.Text);
                cmd[i].Parameters.AddWithValue("@city", city_txt.Text);              
            }
            arrycmd(cmd);
        }


        void arrycmd(SqlCeCommand[] cmd_array)
        {
            //connection check and open
            if (con.State == ConnectionState.Open) { con.Close(); } con.Open();
                       
            //create transaction
            SqlCeTransaction transaction;
            transaction = con.BeginTransaction(IsolationLevel.ReadCommitted);
          
            int m = cmd_array.Length;
            try
            {
                for (int i = 0; i < m; i++)
                {
                    if (cmd_array[i] == null)
                        break;
                    // Assign connection  and transaction to the current command
                    cmd_array[i].Connection = con;
                    cmd_array[i].Transaction = transaction;
                    cmd_array[i].ExecuteNonQuery();

                    cmd_array[i].Dispose();
                }

                transaction.Commit();
            }
            catch
            {
                transaction.Rollback();
            }

            con.Close();
            MessageBox.Show("done");
        }
      

    }

carry forward brought forward formula in crystal report

Crystal Reports carry forward totals from the previous page


I want to show brought forward total on the header in "total_header" field in the header and carried forward total in "total_footer" field on the footer.
I used the following code in crystal reports in a formula field:
WhilePrintingRecords;
NumberVar
total;
total := total + <fieldToTotal>;
The field with above mentioned code will be in details section and following code in both fields "total_header" and "total_footer":
WhilePrintingRecords;
NumberVar total;
total := total;

numerology

c# tutorial for beginners

 c# tutorial for beginners


WebService




file->new website->ASP.NET web service  language: C#

using System;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service : System.Web.Services.WebService
{
    public Service () {

        //Uncomment the following line if using designed components
        //InitializeComponent();
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public double f_c(double t)
    {
        return(((t-32)*5)/9);
    }
    [WebMethod]
    public double c_f(double t)
    {
        return (((t*9)/5)+32);
    }


}


save and run (press F5)




file->new website->ASP.NET web service  language: C#

design 




add web reference:



Website
using System;
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;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox1.Focus();
    }
    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
       
       
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        double z;
        temp.Service obj = new temp.Service();
        if (TextBox1.Text=="")
        {
            Type cstype = this.GetType();
            ClientScriptManager cs = Page.ClientScript;
            if (!cs.IsStartupScriptRegistered(cstype, "PopupScript"))
            {
                String cstext = "alert('Please Enter Value');";
                cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);

            }
        }
        else
        {
            if (DropDownList1.Text == "Fahrenheit to Celsius")
            {
                z = obj.f_c(double.Parse(TextBox1.Text));

                Label2.Text = z.ToString() + "  C";
            }
            else if (DropDownList1.Text == "Celsius to Fahrenheit")
            {
                z = obj.c_f(double.Parse(TextBox1.Text));
                Label2.Text = z.ToString() + "  F";
            }
            else
            {
                Type cstype = this.GetType();
                ClientScriptManager cs = Page.ClientScript;
                if (!cs.IsStartupScriptRegistered(cstype, "PopupScript"))
                {
                    String cstext = "alert('Please Select Conversion');";
                    cs.RegisterStartupScript(cstype, "PopupScript", cstext, true);

                }
            }
        }

    }
}

save and run (press F5)
output




COM COMPONENT TO PERFORM LOGOCAL OPERATIONS USING DOTNET



file->new project->Visual C#->ClassLibrary


DLL
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ClassLibrary1
{
    public class Class1
    {
        public
          int  andop(int a,int b)
        {
            return(a*b);
        }


        public
            int orop(int a, int b)
        {
            if (a == 0 && b == 0)
                return (0);
            else
                return (1);
        }


        public
            int notop(int a)
        {
            if (a == 0)
                return (1);
            else
                return (0);
        }


        public
            int nandop(int a, int b)
        {
            if (a == 1 && b == 1)
                return (0);
            else
                return(1);
        }
        public
            int norop(int a, int b)
        {
            if (a == 0 && b == 0)
                return (1);
            else
                return (0);
        }


        public
            int xorop(int a, int b)
        {
            if (a == b)
                return (0);
            else
                return (1);
        }

        public
            int xnorop(int a, int b)
        {
            if (a == b)
                return (1);
            else
                return (0);
        }

    }
}

 build a class library (press F5)

--------------------------------------------------------------------------
 file->new project->Vb->WindowsApplication

add a dll file
project->add reference->browse to select dll and clik ok

CODING
Imports ClassLibrary1

Public Class Form1
    Dim obj As Class1 = New Class1
    Dim a, b As Integer


    Private Sub ComboBox3_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox3.SelectedIndexChanged
        a = Val(ComboBox1.Text)
        b = Val(ComboBox2.Text)

        If ComboBox3.Text = "AND" Then
            TextBox1.Text = obj.andop(a, b)
            ComboBox2.Visible = True

        ElseIf ComboBox3.Text = "OR" Then
            TextBox1.Text = obj.orop(a, b)
            ComboBox2.Visible = True

        ElseIf ComboBox3.Text = "NOT" Then
            ComboBox2.Visible = False
            TextBox1.Text = obj.notop(a)

        ElseIf ComboBox3.Text = "NAND" Then
            TextBox1.Text = obj.nandop(a, b)
            ComboBox2.Visible = True

        ElseIf ComboBox3.Text = "NOR" Then
            TextBox1.Text = obj.norop(a, b)
            ComboBox2.Visible = True
      
        End If



    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        ComboBox2.Visible = True

    End Sub
End Class





OUTPUT





Your IP Address is:

Browser: