Tuesday, March 27, 2012

Add SOAP Header to your XML WebService

This will show how to add a SOAP Header to a WebService.
//namespace
using System.Web.Services.Protocols;

Creating the Header.
Create a Class which is Inherited from SoapHeader Class.


public class UserAuthHeader : SoapHeader
{
public string UserName;
public string Password;
}



[WebService(Namespace = "http://www.smilu.net/")]
[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 UserAuthHeader userauthentication;
public Service () {

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

[WebMethod,SoapHeader("userauthentication")]
public string HelloWorld() {
if (userauthentication.UserName == "smilu" && userauthentication.Password == "pass")
{
return "Hello World";
}
else
{
return "access denied";
}

}

}



-------------------------------------------------------------------------------------

For calling the same inside a Website check the code below

protected void Button1_Click(object sender, EventArgs e)
{
WithHeader.Service sc = new WithHeader.Service();
WithHeader.UserAuthHeader uh = new WithHeader.UserAuthHeader();
uh.UserName = "smilu";
uh.Password = "pass";
sc.UserAuthHeaderValue = uh;
Response.Write(sc.HelloWorld());
}

Here UserAuthHeader is the Class name of the Header which we created.

Wednesday, November 23, 2011

Dynamically create a Report in ReportViewer

Here im going to tell you about how to create a report using a report viewer by coding.

First you should create a typed DataSet with the structure of your Table(or how you need the table to be).
Now the next step is to create a report

Website/Project -> Add New Item -> Report
Give a name for it.
After this Select the Dataset from the New menu displayed in the rdlc file
The dataset will be diplayed in the report data window.
You must have to provide a Name for the dataset. In DataSource you will get the Typed DataSet you created.
Select the DataTable you created in the Dataset in the AvailableDatasets section and click OK.
Now, You will get the information in the left pane window.

Above you can see the Dataset added to the Table. Here the DataSet name given by me is DataSetSmilu. You should remember this name when we Bind the data from the program....

Now you can right click on the report create a table in the Report page and add the Fileds shown above like this.


Now you have the report file to be attached with the Report Viewer. You can add images, header etc to this page...

Now create a new WebForm.
From the ToolBox add a Ajax ScriptManager into the page
Now from the Reports Section add a Report Viewer into the page.
Select the ToolTip of the ReportViewer and Select the .rdlc file that you have created.

Now whenever you add a Report.rdlc file an ObjectDataSource will be added to the page. We dont need that objectdatasource for the working here...
So You can delete the ObjectDataSource.
Now you should go to Source of the Page and remove the ObjectDataSource Link on the LocalReport section of Reportviewer


        <rsweb:ReportViewer ID="ReportViewer1" runat="server" Font-Names="Verdana"
            Font-Size="8pt" InteractiveDeviceInfos="(Collection)"
            WaitMessageFont-Names="Verdana" WaitMessageFont-Size="14pt" Width="651px">
            <LocalReport ReportPath="rerportsmilu.rdlc">
                <DataSources>
                   <rsweb:ReportDataSource />
                </DataSources>
            </LocalReport>
         </rsweb:ReportViewer>

After editing the form should look like this.

Now to our coding part.
Goto your codebehind file. ie the .cs file of the page and bind the data to the dataset.


using System.Data.SqlClient;
using Microsoft.Reporting.WebForms;
using System.Web.Configuration;

//code in page load or some methods..

 smiluDS tDs = new smiluDS();
            SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["NationalBankConnectionString"].ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("Select * from Employee", con);
            da.Fill(tDs, tDs.Tables[0].TableName);


            ReportDataSource rDs = new ReportDataSource("DataSetSmilu", tDs.Tables[0]);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rDs);
            ReportViewer1.LocalReport.Refresh();
//code complete

In the above code the We are using a SqlDataAdapter to get the data.
Now since we are using a TypedDataSet you should create an instance of the TypedDataSet itself.. Here it is smiluDS. Now  a ReportDataSource should be created for the reportviewer. In the report viewer we should specify the DataSet which we have created in the RDLC file and data to be Binded into it..

Try to run it.. you will get the d





Monday, November 21, 2011

VB to C# Application Path in WindowsService

This method in VB.NET can be written like the below reflection method. The below method gets you the location of your application
My.Application.Info.DirectoryPath


System.Reflection.Assembly.GetExecutingAssembly().Location;

Tuesday, August 31, 2010

Insertion Using Entity Model



This is the Entity data Model NationalBankEntities3 in the program.
We are going to add Items into the table EmployeeDetail

Now we must write the code like below

NationalBankModel3.NationalBankEntities3 m = new NationalBankModel3.NationalBankEntities3();
NationalBankModel3.EmployeeDetail Emp = new NationalBankModel3.EmployeeDetail() { EmpName = "Smilu", EmpAddress = "EKM", EmpPhNo = "987654", EmpDob =Convert.ToDateTime("01/01/01") };

m.AddToEmployeeDetails(Emp);
m.SaveChanges();

Here AddToEmployeeDetails will be the method which will insert the data of the Entity we send.
SaveChages() will store the data into the table

Tuesday, August 10, 2010

Linq to XML

Here we are just going to see how we can associate XML with LinQ.

First create an XML file with the structure like this CLICK HERE

Now add a GridView to your design.

We need

System.Xml.Linq;

namespace for working of XLINQ

Now in the page load event add the code like this..
------------------------------------------------------
var query = from s in XElement.Load(MapPath("Students.xml")).Elements("Student1")
select new
{
Name = (string)s.Element("StName"),
Address = (string)s.Element("StAddress"),
PhNo = (string)s.Element("StPHNO"),
flag = (int)s.Element("Flag")
};

GridView1.DataSource=query;
GridView1.DataBind();
------------------------------------------------------
In the above code we are using XElement.Load method to Load the Students.xml file and we must mention the Node inside which we have put the informations.

(string)s.Element("StName")
The above method converts the data inside the Node to the type we have specified and stores into it....
When we execute it we will get a GridView which contains columns Name, Address, PhNo, and Flag..


Students.XML File

<Students>
<Student>
<StID>S001</StID>
<StName>Simi</StName>
<StAddress>EKM</StAddress>
<StPHNO>987654123</StPHNO>
<Flag>1</Flag>
</Student>
<Student>
<StID>S002</StID>
<StName>Ravi</StName>
<StAddress>EKM</StAddress>
<StPHNO>654987</StPHNO>
<Flag>0</Flag>
</Student>
<Student>
<StID>S003</StID>
<StName>Manu</StName>
<StAddress>TVM</StAddress>
<StPHNO>987654123</StPHNO>
<Flag>1</Flag>
</Student>
<Student>
<StID>S004</StID>
<StName>Sooraj</StName>
<StAddress>EKM</StAddress>
<StPHNO>64568</StPHNO>
<Flag>0</Flag>
</Student>
<Student>
<StID>S005</StID>
<StName>Remya</StName>
<StAddress>EKM</StAddress>
<StPHNO>987654123</StPHNO>
<Flag>1</Flag>
</Student>
</Students>

Monday, August 9, 2010

JOIN - Linq to Objects

We have two List<> methods namely GetStudents() which get us the information about the Students and GetFlagNames() which give us information about Flag.

public List GetStudents()
{
return new List
{
new Student{Name="Smilu",Address="EKM",PhNo="987654",flag=1},
new Student{Name="JK", Address="Parur",PhNo="897643",flag=0},
new Student{Name="Kiran", Address="EKM",PhNo="798654",flag=1}
};
}

public List GetFlagNames()
{
return new List
{
new FlagName{flagID=0,flagName="CS"},
new FlagName{flagID=1,flagName="Network"}
};
}
---------------------------------------------------
We store the list object into a variable called "mystudents" and "flags" respectively...
var mystudents = GetStudents();
var flags = GetFlagNames();

This query represents the Joining using LinQ
var query = from s in mystudents
join f in flags on s.flag equals f.flagID
select new { s.Name, s.PhNo, f.flagName };