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);
Saturday, 13 October 2012
Thursday, 4 October 2012
c# for dummies: C# video tutorials
c# for dummies: C# video tutorials: How to create C# solutions and Projects
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.
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();
}
}
Wednesday, 19 September 2012
c# for dummies
string or binary would be truncated:
the solution is to check the columns of the table you are trying to insert into and make sure it there is
a match between the datatype you are using to insert from you User interface and the one on the database table.
for example:
if you are using string say .text
on the database table use nvarchar(50).
the solution is to check the columns of the table you are trying to insert into and make sure it there is
a match between the datatype you are using to insert from you User interface and the one on the database table.
for example:
if you are using string say .text
on the database table use nvarchar(50).
Subscribe to:
Posts (Atom)
