This post is the demo for displaying data from WCF in datagrid in silverlight 4. I have used Department data for demo. Please follow points below:
1. Create a class named DepartmentClass.cs (WCF part)
public class DepartmentClass {
public string DepartmentName { get; set; }
public string Description { get; set; }
}
2. Write a select query in .svc.cs file as per below method, we need to define this methid in interface also. The below method is to show how we will send the values to silverlight from WCF. Instead of ObservableCollection we can take list also. da = DataAdapter and con = SqlConnection (WCF part)
public ObservableCollection SelectDepartment(DepartmentClass objDept){
da.SelectCommand = new SqlCommand(“SELECT departmentName, description FROM tblDepartment WHERE isActive=’true’”);
da.SelectCommand.Connection = con;
using (var deptData = da.SelectCommand.ExecuteReader()){
while (deptData.Read()){
_departmentClasses.Add(new DepartmentClass(){
DepartmentName = deptData.GetString(1),
Description = deptData.GetString(2)});
}
}
con.Close();
return _departmentClasses;
}
3. Write following code in .xaml file. In binding we will provide variable name used in Class. (silverlight part)
<sdk:DataGrid AutoGenerateColumns="False" Grid.Row="6"
Height="187" HorizontalAlignment="Left"
Margin="116,13,0,0" Name="dataGrid1"
VerticalAlignment="Top" Width="433">
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Width="150" Header="Name"
Binding="{Binding DepartmentName}" />
<sdk:DataGridTextColumn Width="280" Header="Description"
Binding="{Binding Description}" />
</sdk:DataGrid.Columns>
</sdk:DataGrid>
4. Following code will be written in .xaml.cs file in constructor.(silverlight part)
public Department()
{
InitializeComponent();
var depProxy = new AdminServiceReference.IadminClient();
depProxy.SelectDepartmentCompleted +=new EventHandler(depProxy_SelectDepartmentCompleted);
depProxy.SelectDepartmentAsync();
}
private void depProxy_SelectDepartmentCompleted(object sender, SelectDepartmentCompletedEventArgs e)
{
if (e.Result != null)
{
dataGrid1.ItemsSource = e.Result;
}
}
Filed under: ASP.NET, SQL, XML | Tags: ASP.NET C#, SQL Server, Stored Procedure, XML
CREATE PROCEDURE [dbo].[Student]
– Add the parameters for the stored procedure here
@studentlXML XML
AS
BEGIN
– SET NOCOUNT ON added to prevent extra result sets from
– interfering with SELECT statements.
SET NOCOUNT ON;
— Insert statements for procedure here
Declare @CountHandler int
Exec SP_XML_PrepareDocument @CountHandler OUTPUT,@studentlXML
INSERT INTO tblStudent –Table name where we have to save xml data
SELECT * FROM OPENXML(@CountHandler,”/friends/friend”,2)
WITH tblStudent
Exec SP_XML_RemoveDocument @CountHandler
END
END
NOTE: @studentlXML is a xml file can be passed as a string or file
friends is the parent node of xml and friend is a node under friends node, which will be excluded and rest nodes will be treated as column. Please note that node name should be exactly same as column name in table tblStudent
Filed under: ASP.NET, XML | Tags: ASP.NET C#, Dataset, DataView, Dropdown List, XML
If XML is saved as Employees.xml in C Drive:-
Code to be written on PAGE LOAD
DataSet ds = new DataSet();
ds.ReadXml(MapPath(“C:\\Employees.xml”));
DataView dv = ds.Tables["Employee"].DefaultView;
dv.Sort = “Name”;
ddlCountry.DataTextField = “Name”;
ddlCountry.DataValueField = “ID”;
ddlCountry.DataSource = dv;
ddlCountry.DataBind();
Format Style Output
MM-dd-yyyy : 04-09-2010
MMM dd, yyyy : Apr 09, 2010
ddd MM, yyyy : Fri 04, 2010
dddd MM, yyyy : Friday 04, 2010
MMM ddd dd, yyyy : Apr Fri 09, 2010
MMMM ddd dd, yyyy : April Fri 09, 2010
MMMM dddd dd, yyyy : April Friday 09, 2010
Filed under: Uncategorized
string myDateTime;
myDateTime= “19/02/2008 05:44:00″;
IFormatProvider myculture = new CultureInfo(“fr-Fr”, true);
dt = DateTime.ParseExact(myDateTime, “dd/MM/yyyy hh:mm:ss”, myculture, DateTimeStyles.NoCurrentDateDefault);
Following line will return complete url of the current page in asp.net 2.0
Request.Url.AbsoluteUri
Filed under: SQL | Tags: Date, DateTime, Insert, Select, SQL, SQL Server, Store, Time, Update
please check the following link for any type of date and time problem:-
http://www.codeproject.com/KB/showcase/RedGate_DateTime.aspx
Filed under: Ajax
Filed under: Uncategorized
http://aspguy.wordpress.com/2008/10/17/reading-and-writing-images-fromto-database/
http://www.codeproject.com/KB/database/ImageSaveInDataBase.aspx
http://www.beansoftware.com/asp.net-tutorials/images-database.aspx
Filed under: JavaScript | Tags: ClientID, Control ID, getElementById, HTML, JavaScript
There are two ways of getting the id of dotnet control in javascript
First
document.getElementById(“ctl00$ContentPlaceHolder$TextBox1″)
SECOND
document.getElementById(“<%=TextBox1.ClientID %>”)
If you want to get the id of html control then just write control id in double quotes.For example:-
document.getElementById(“TextBox1″)