Tuesday 23 October 2012

c# for dummies: c sharp classes

c# for dummies: c sharp classes: Cannot insert null value Step 1:         Open the sql server studio Step 2:          open the table in particular i.e unionTB Step3:    ...

c sharp classes

Cannot insert null value

Step 1:
        Open the sql server studio

Step 2:
         open the table in particular i.e unionTB

Step3:
        Tick the column affected allow null




see the pictures.

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Step1:      If  you are using class go to the class open it Step2:      delete the database linq to sql classes and re add them. i.e ...

c sharp programming


Step1:
     If  you are using class go to the class open it

Step2:
     delete the database linq to sql classes and re add them. i.e munedb.dml


see pictures

Tuesday 16 October 2012

c# for dummies: c sharp programming

c# for dummies: c sharp programming: // BY THE SPIRIT THE CREATOR OF THE WHOLE WORLD using System; using System.Collections.Generic; using System.ComponentModel; using Syste...

c sharp programming


// BY THE SPIRIT THE CREATOR OF THE WHOLE WORLD
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace testingFormuneDB
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlConnection sqconnection;
        SqlCommand sqlcommand;
        string connectionstring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=c:\users\abayomi\documents\visual studio 2010\Projects\testingFormuneDB\testingFormuneDB\testingMUNEProjDB.mdf;Integrated Security=True;User Instance=True";
        bool flag = false;
        private void btnsubmit_Click(object sender, EventArgs e)
        {

          bool resp=  saveTotalbalance();
          MessageBox.Show(resp.ToString());
        }
        public bool saveTotalbalance()
        {
            int mandatory = int.Parse(txtboxmandatory.Text);
            int voluntary = int.Parse(txtboxvoluntary.Text);
            int prevoiusbalance = int.Parse(txtboxpreviousbalance.Text);

            int totalbalance = mandatory + voluntary + prevoiusbalance;
            txtboxtotalbalance.Text = totalbalance.ToString();
                    
            sqconnection = new SqlConnection(connectionstring);
            sqconnection.Open();
            string commanntxt = "insert into repaymentTB (totalbalance) values('" + totalbalance + "') ";

            sqlcommand = new SqlCommand(commanntxt, sqconnection);
            sqlcommand.ExecuteNonQuery();
            flag = true;
            return flag;

        }
        public void retrivebalance()
        {
            sqconnection = new SqlConnection(connectionstring);
            sqconnection.Open();
            string commandtxt = "select totalbalance from repaymentTB ";
            sqlcommand = new SqlCommand(commandtxt, sqconnection);

            DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            SqlDataAdapter sqldatadapter = new SqlDataAdapter(sqlcommand);
            sqldatadapter.Fill(ds);
            dt = ds.Tables[0];
            foreach (DataRow r in dt.Rows)
            {
                lblpreviousbalance.Text = r["totalbalance"].ToString();
            }
          
            sqconnection.Close();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            retrivebalance();
        }
       
    }
}

c# for dummies: c sharp classes

c# for dummies: c sharp classes: USING LINQ TO SQL: nwind.Northwind db =    new nwind.Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind"); db.Log = Console.Out;...

c sharp classes

USING LINQ TO SQL:

nwind.Northwind db = 
  new nwind.Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind");


db.Log = Console.Out;

var custs = from c in db.Customers
            where c.Region == "WA"
            select new { Id = c.CustomerID, Name = c.ContactName };

foreach (var cust in custs)
{
  Console.WriteLine("{0} - {1}", cust.Id, cust.Name);
}

c# for dummies: c sharp classes

c# for dummies: c sharp classes: how to use expression trees int[] nums = new int[] { 6, 2, 7, 1, 9, 3 }; IEnumerable numsLessThanFour = nums   .Where(i => i < 4)   .Or...

c sharp classes

how to use expression trees



int[] nums = new int[] { 6, 2, 7, 1, 9, 3 };
IEnumerable<int> numsLessThanFour = nums
  .Where(i => i < 4)
  .OrderBy(i => i);

c# for dummies: c sharp programming

c# for dummies: c sharp programming: How  To Use Lambda Expressions int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int[] oddNums = Common.FilterArrayOfInts(nums, i => ((i & 1) ...

c sharp programming

How  To Use Lambda Expressions

int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

int[] oddNums = Common.FilterArrayOfInts(nums, i => ((i & 1) == 1));

foreach (int i in oddNums)
  Console.WriteLine(i);


1
3
5
7
9

c# for dummies: c sharp classes

c# for dummies: c sharp classes: How to Use the DataContext Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind"); db.Log = Console.Out; IQuery...

c sharp classes

How to Use the DataContext


Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial
Catalog=Northwind");

db.Log = Console.Out;

IQueryable<Order> orders = from c in db.Customers
                           from o in c.Orders
                           where c.Country == "USA" && c.Region == "WA"
                           select o;

foreach(Order item in orders)
  Console.WriteLine("{0} - {1} - {2}", item.OrderDate, item.OrderID,
item.ShipName);

OUTPUT:

SELECT [t1].[OrderID], [t1].[CustomerID], [t1].[EmployeeID], [t1].[OrderDate],
 [t1].[RequiredDate], [t1].[ShippedDate], [t1].[ShipVia], [t1].[Freight],
 [t1].[ShipName], [t1].[ShipAddress], [t1].[ShipCity], [t1].[ShipRegion],
 [t1].[ShipPostalCode], [t1].[ShipCountry]
FROM [dbo].[Customers] AS [t0], [dbo].[Orders] AS [t1]
WHERE ([t0].[Country] = @p0) AND ([t0].[Region] = @p1) AND ([t1].[CustomerID] =
 [t0].[CustomerID])
-- @p0: Input String (Size = 3; Prec = 0; Scale = 0) [USA]

c# for dummies: c sharp programming

c# for dummies: c sharp programming: Using the Cast: ArrayList arrayList = new ArrayList(); arrayList.Add("Adams"); arrayList.Add("Arthur"); arrayList.Add("Buchanan"); IEnumerab...

c sharp programming

Using the Cast:


ArrayList arrayList = new ArrayList();
arrayList.Add("Adams");
arrayList.Add("Arthur");
arrayList.Add("Buchanan");

IEnumerable<string> names = arrayList.Cast<string>().Where(n => n.Length < 7);
foreach(string name in names)
  Console.WriteLine(name);

 ArrayList arrayList = new ArrayList();
arrayList.Add("Adams");
arrayList.Add("Arthur");
arrayList.Add("Buchanan");

IEnumerable<string> names = arrayList.OfType<string>().Where(n => n.Length < 7);
foreach(string name in names)
  Console.WriteLine(name);

OUTPUT:

Adams
Arthur

c# for dummies: c sharp classes

c# for dummies: c sharp classes: Using the var Keyword Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial Catalog=Northwind"); IEnumerable orders = db.Cu...

c sharp classes

Using the var Keyword

Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial
Catalog=Northwind");

IEnumerable<Order> orders = db.Customers
  .Where(c => c.Country == "USA" && c.Region == "WA")
  .SelectMany(c => c.Orders);

foreach(Order item in orders)
  Console.WriteLine("{0} - {1} - {2}", item.OrderDate, item.OrderID,
item.ShipName);

OUTPUT:

3/21/1997 12:00:00 AM - 10482 - Lazy K Kountry Store
5/22/1997 12:00:00 AM - 10545 - Lazy K Kountry Store

4/17/1998 12:00:00 AM - 11032 - White Clover Markets
5/1/1998 12:00:00 AM - 11066 - White Clover Markets

Saturday 13 October 2012

c sharp programming

Using the var Keyword

Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial
Catalog=Northwind");

IEnumerable<Order> orders = db.Customers
  .Where(c => c.Country == "USA" && c.Region == "WA")
  .SelectMany(c => c.Orders);

foreach(Order item in orders)
  Console.WriteLine("{0} - {1} - {2}", item.OrderDate, item.OrderID,
item.ShipName);

OUTPUT:

3/21/1997 12:00:00 AM - 10482 - Lazy K Kountry Store
5/22/1997 12:00:00 AM - 10545 - Lazy K Kountry Store

4/17/1998 12:00:00 AM - 11032 - White Clover Markets
5/1/1998 12:00:00 AM - 11066 - White Clover Markets

c# for dummies: c sharp classes

c# for dummies: c sharp classes: USING LINQ namespace LINQDev.HR {   public class Employee   {     public int id;     public string firstName;     public string lastName;   ...

c sharp classes

USING LINQ

namespace LINQDev.HR
{
  public class Employee
  {
    public int id;
    public string firstName;
    public string lastName;

    public static ArrayList GetEmployees()
    {
      //  Of course the real code would probably be making a database query
      //  right about here.
      ArrayList al = new ArrayList();

      //  Man, do the C# object initialization features make this a snap.
      al.Add(new Employee { id = 1, firstName = "Joe", lastName = "Rattz"} );
      al.Add(new Employee { id = 2, firstName = "William", lastName = "Gates"} );
      al.Add(new Employee { id = 3, firstName = "Anders", lastName = "Hejlsberg"}
);
      return(al);
    }
  }
}




namespace LINQDev.Common
{
  public class Contact
  {
    public int Id;
    public string Name;

    public static void PublishContacts(Contact[] contacts)
    {
      //  This publish method just writes them to the console window.
      foreach(Contact c in contacts)
        Console.WriteLine("Contact Id:  {0}  Contact:  {1}", c.Id, c.Name);
    }
  }
}
\


LINQDev.Common.Contact[] contacts = alEmployees
  .Cast<LINQDev.HR.Employee>()
  .Select(e => new LINQDev.Common.Contact {
                 Id = e.id,
                 Name = string.Format("{0} {1}", e.firstName, e.lastName)
               })
  .ToArray<LINQDev.Common.Contact>();

LINQDev.Common.Contact.PublishContacts(contacts);


OUTPUT:

Contact Id:  1  Contact:  Joe Rattz
Contact Id:  2  Contact:  William Gates
Contact Id:  3  Contact:  Anders Hejlsberg

c# for dummies: c sharp programming

c# for dummies: c sharp programming: HOW TO QUERY LINQ TO SQL DATABASE using System; using System.Linq; using System.Data.Linq; using nwind; Northwind db = new Northwind(@"Data ...

c sharp programming

HOW TO QUERY LINQ TO SQL DATABASE


using System;
using System.Linq;
using System.Data.Linq;

using nwind;

Northwind db = new Northwind(@"Data Source=.\SQLEXPRESS;Initial
Catalog=Northwind");

var custs =
  from c in db.Customers
  where c.City == "Rio de Janeiro"
  select c;

foreach (var cust in custs)
  Console.WriteLine("{0}", cust.CompanyName);

c# for dummies: c sharp classes

c# for dummies: c sharp classes: HOW TO QUERY  XML USING LINQ using System; using System.Linq; using System.Xml.Linq; XElement books = XElement.Parse(   @"       <boo...

c sharp classes

HOW TO QUERY  XML USING LINQ


using System;
using System.Linq;
using System.Xml.Linq;


XElement books = XElement.Parse(
  @"<books>
      <book>
          <title>Pro LINQ: Language Integrated Query in C# 2010</title>
          <author>Joe Rattz</author>
      </book>
      <book>
          <title>Pro .NET 4.0 Parallel Programming in C#</title>
          <author>Adam Freeman</author>
      </book>
      <book>
          <title>Pro VB 2010 and the .NET 4.0 Platform</title>
          <author>Andrew Troelsen</author>
       </book>
    </books>");

var titles =
  from book in books.Elements("book")
  where (string) book.Element("author") == "Joe Rattz"
  select book.Element("title");

foreach(var title in titles)
  Console.WriteLine(title.Value);

c# for dummies: c sharp programming

c# for dummies: c sharp programming: how to use linq using System; using System.Linq;   string[] greetings = {"hello world", "hello LINQ", "hello Apress"}; var items =    fr...

c sharp programming

how to use linq



using System;
using System.Linq;
 
string[] greetings = {"hello world", "hello LINQ", "hello Apress"};

var items = 
  from s in greetings
  where s.EndsWith("LINQ")
  select s;

foreach (var item in items)
  Console.WriteLine(item);

Tuesday 2 October 2012

c# for dummies: Inserting into database using WCF

Insert into Database using WCF.

Write These Code :  App_Code/Service.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data;

// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service" in code, svc and config file together.
public class Service : IService
{
  

    bool flag = false;
    public bool saveStudent(string matricno,string name)
    {
        string conectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Abayomi\Documents\Visual Studio 2010\WebSites\UnderstandingWCF2\App_Data\studentDB.mdf;Integrated Security=True;User Instance=True";
        SqlConnection sqlconn = new SqlConnection(conectionString);
        sqlconn.Open();
        string command = "insert into studentTB(MatricNo,name)values('"+matricno+"','"+name+"')";
        SqlCommand sqlcooma = new SqlCommand(command, sqlconn);
        sqlcooma.ExecuteNonQuery();
        flag = true;
        return flag;
    }
 
      

          }
}




Write these code in Interface: App_Code/IService.cs




public interface IService
{

   

    [OperationContract]
    bool saveStudent(string matricno,string name);

 }



Then create another windows application


Go to add  service refrence on the project solution explorer and enter the wcf address url.


Adding to columns together to produce a new column






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.Data;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnload_Click(object sender, EventArgs e)
    {
        string constring = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Abayomi\Documents\Visual Studio 2010\WebSites\calculatedcolumns2\App_Data\product.mdf;Integrated Security=True;User Instance=True";

        SqlConnection conn = new SqlConnection(constring);
        string sqlcat = "select categoryID,categoryName from CategoriesTB";
        string sqlprod = "select productName,categoryID,UnitPrice from productsTB";

        SqlDataAdapter da = new SqlDataAdapter(sqlcat, conn);
        DataSet ds = new DataSet();

        conn.Open();
        da.Fill(ds, "CategoriesTB");
        da.SelectCommand.CommandText = sqlprod;
        da.Fill(ds, "productsTB");
        conn.Close();

        // Define the realationships between Categories and Products
        DataRelation relat = new DataRelation("CatProds", ds.Tables["CategoriesTB"].Columns["categoryID"], ds.Tables["productsTB"].Columns["categoryID"]);
        ds.Relations.Add(relat);

        DataColumn count = new DataColumn("Product(#)", typeof(int), "SUM(Child(CatProds).categoryID)");

        ds.Tables["categoriesTB"].Columns.Add(count);

        GridView1.DataSource = ds.Tables["CategoriesTB"];
        GridView1.DataBind();
    }
}