Saturday, 13 October 2012

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();
    }
}