This is default featured post 1 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 2 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 3 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 4 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

This is default featured post 5 title

Go to Blogger edit html and find these sentences.Now replace these sentences with your own descriptions.

Showing posts with label Dot Net Code. Show all posts
Showing posts with label Dot Net Code. Show all posts

Working with Windows Workflow Foundation in ASP.NET

In September of 2005, Microsoft unleashed Windows Workflow Foundation (Windows WF) at its semi-annual Professional Developer's Conference. As one of the pillars of the WinFX APIs, Windows WF provides developers with a common framework on which to develop process driven and workflow-centric applications.
Currently, when organizations wish to automate business processes the standard answer is to assemble a team of developers to write the appropriate code. While this approach has generally served organizations well, it has some inherent problems. To understand why, you need to understand some fundamental characteristics of a workflow.
A workflow is essentially a way of documenting the activities involved in completing a unit of work. Typically, work "flows" through one or more activities during processing. These activities can be performed by either machines or by people, and can be as simple as defining the sequence of pages in an Internet application, or as complex as managing documents or products that must be seen, altered, and approved by any number of people.

Because so many workflows must allow for human involvement, they can take long periods of time to complete, ranging from hours to months or longer. For example, people involved in the process may be unavailable, out-of-town, or busy with other tasks; therefore, workflows must be able to persist themselves during periods of inactivity. Furthermore, processes implemented solely using code can be difficult for non-technical people to understand and for developers to change. This and other factors are the target of generic workflow frameworks such as Windows WF, which aim to make creating, altering, and managing workflows easier, both by giving them a visual interface and by defining a common API
You can host Windows WF workflows in any type of .NET application, including Windows Forms, console applications, Windows Services, and ASP.NET Web applications. Each type requires special considerations. While examples that showcase hosting workflows in Windows Forms and console applications are already plentiful, this article focuses on issues facing ASP.NET developers who wish to integrate workflow into their applications.
Windows WF and the MVC Pattern
One common way in which you might use Windows WF in an ASP.NET application is to implement a Model-View-Controller (MVC) approach. Essentially, the goal of MVC is to separate the presentation layer, application logic, and application flow logic from each other.

To illustrate how this would be useful in an ASP.NET application, consider a help desk ticket workflow scenario. A business user starts the workflow by filling out an ASP.NET Web form and clicking a submit button. Next, a server notifies a help desk employee using a Windows Forms application that a new ticket is available. The help desk employee would then work on the problem, and eventually close the ticket. If this workflow scenario were developed using Windows WF, all the process logic and flow could be contained in the workflow itself, and the ASP.NET application would need to know nothing about the logic.

This scenario provides some solid evidence that separating presentation from logic is a good thing. Because the process of handling help desk requests is common, if the logic were implemented using C# or VB.NET code in several different .NET applications, you'd run the risk of duplicate code or worse, different implementations of the same business process in completely different code. But if you implement the process in Windows WF, the developers of applications that need the process would need to modify the steps in only one place—the workflow itself—without worrying about changing logic in their applications. Code duplication and the ambiguity of where to implement process can be mitigated with Windows WF.

When implementing the MVC architecture in ASP.NET using Windows WF, developers should attempt to build workflows independent of the applications in which they will be hosted. This will assist in keeping logic separate from presentationand maintain a high degree of independence between the order of workflow steps and page flow in the Web application.

A novice Windows WF developer might be tempted to develop a workflow with a set number of activities in a certain order and afterward, develop a set of ASP.NET Web forms that flow from one to another in the same order. Unfortunately, while that seems logical, it's counterproductive, because you would be implementing the workflow logic twice. Web page X should not need to know whether it needs to go to Page Y or Page Z to implement the workflow steps correctly. Instead, the workflow (the model) should tell ASP.NET (the controller) what the next step is, and then ASP.NET should determine which page (the view) to display. In this way, each page requires little knowledge of the overall process; it only needs to know how to complete one distinct activity and let the workflow worry about how the pages flow from one to the next. This separation provides developers with a great deal of flexibility related to the order of page flow. For example, if you decided to change the page display sequence, you could easily do that in the workflow, without changing one line of code in the ASP.NET application.

For Sample Application and Code ....> next Article

Select Distinct From DataSet


This step-by-step article illustrates the equivalent to select distinct values from one or more column of a DataSet or DataTable
Example :

NOTE: Add the following to the top of the code window:
 

using System.Data;

//Add the following constructor code :

System.Data.DataSet ds = new System.Data.DataSet();

// Create source table

System.Data.DataTable dt = new System.Data.DataTable("MyTableName");
dt.Columns.Add("ColumnA", Type.GetType("System.Int32"));
dt.Rows.Add(1);
dt.Rows.Add(2);
dt.Rows.Add(3);
dt.Rows.Add(1);
Ds.Tables.Add(dt);
System.Data.DataView Dv = Ds.Tables["MyTableName"].DefaultView;
System.Data.DataTable DtD = Dv.ToTable(true, "ColumnA");
//DtD contains only Distinct values (1,2 and 3)

The command Dv.ToTable(true, .. help you to Select Distinct from Ds DataSet the column : "ColumnA"

Check If User Name Exists Using AJAX And C#

You could make sure the user name entered during the registration process of your website is unique before the submit button is clicked using AJAX and C#. To accomplish this create two aspx pages. The first (default.aspx in this example) is the registration page, and it contains a TextBox control with an onkeyup element that calls the updateOutput JavaScript function and a span tag to display the HTML returned by the XMLHttpRequest.send function. The second page (doajaxstuff.aspx) should only contain following tags:

<@Page Language="C#" AutoEventWireup="true" CodeFile="doajaxstuff.aspx.cs" Inherits="VUsername_doajaxstuff" >


<asp:literal runat="server" id="literal1"/>



The html, title and body tags should be removed because the page contents will be added to the default.aspx page, which already has these tags. The C# code that checks if the user name exists is in the Page_Load event of doajaxstuff.aspx. It gets the user name from the query string and displays the result in a Literal control. To keep the example simple I returned "User name exists" or "OK", but this could be changed to return an img tag to show a red cross image or a green tick respectively.

view plaincopy to clipboardprint?

string[] usernames = { "admin", "administrator", "user1","user2",
"guest1", "guest2"};
protected void Page_Load(object sender, EventArgs e)
{
string newUsername = Request.QueryString["q"];

if(usernames.Contains(newUsername))
{
literal1.Text = "User name exists";
}
else
{
literal1.Text = "OK";
}
}

string[] usernames = { "admin", "administrator", "user1","user2",
"guest1", "guest2"};
protected void Page_Load(object sender, EventArgs e)
{
string newUsername = Request.QueryString["q"];

if(usernames.Contains(newUsername))
{
literal1.Text = "User name exists";
}
else
{
literal1.Text = "OK";
}
}

The JavaScript code below does the AJAX work. The updateOutput function checks first if the TextBox is empty, and exits if true. Then it creates an object called xmlHttp which is an XMLHttpRequest object or an ActiveXObject, depending on the user's browser. XMLHttpRequest is supported in browsers from Internet Explorer 6, Opera 7.60, Mozilla 1.0, Netscape 7, Safari 1.2 and Konqueror 3.2. If the user has Internet Explorer 5 an ActiveXObject will be created, which basically does the same job. If the xmlHttp object is created the variable url will be assigned a string containing the page to call plus the query string, which is the string entered by the user in usernameTextBox.

The onreadystatechange event handler is assigned the name of the function StateChanged. This event occurs five times: when the request is initialized, set up, sent, being processed and completed. You could check which stage the request is in using the readyState variable (0, 1, 2, 3 and 4 respectively.) The if statement checks for 4 which is set when the HTML is downloaded to xmlHttp.responseText. The if statement also makes sure that xmlHttp.status is 200, which means no error occurred. If both these conditions are true the contents of xmlHttp.responseText are displayed in the span tag.

The open method sets the parameters of the request and contains three parameters; the request method, the URL of the request and a Boolean parameter that specifies the request should be executed asynchronously or not. The first parameter could be "GET", "POST", "HEAD" or "PUT". The second parameter is the relative or full URL of the page to request. And the third parameter should be true so the page could be called asynchronously.

The send method executes the request. Because the open method's first parameter is "GET", send's parameter is null. If open's first parameter was "POST" it would be a string containing the request parameters.
 

Uploading Files using FileUpload control in Update Panel in ASP.Net AJAX

By default, FileUpload control will not work inside an UpdatePanel control for uploading files using Asynchronous postback. This is because, the file uploading and file manipulations is restricted by default in client side for security reasons. Hence it is not possible to upload files using asynchronous postback in UpdatePanel.

 

To upload files inside UpdatePanel control we need to rely upon a standard postback i.e. we need to set the button that is uploading the file to be PostBack trigger instead of AsyncPostBack trigger. This will initiate a normal postback whenever we click the upload button and it is possible to upload the file.

 

Refer the below code for clear understanding,

 

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

            <ContentTemplate>              

                <asp:FileUpload ID="fuUpload" runat="server" />

                <asp:Button ID="btnUpload" runat="server" OnClick="btnUpload_Click" Text="Upload" />

            </ContentTemplate>

            <Triggers>

                <asp:PostBackTrigger ControlID="btnUpload" />               

            </Triggers>

        </asp:UpdatePanel>

 

 

 protected void btnUpload_Click(object sender, EventArgs e)

    {

        string filename = System.IO.Path.GetFileName(fuUpload.FileName);

        fuUpload.SaveAs("C:\temp" + filename);

    }

 

To simulate an AJAX file upload we can use iframes. In this approach, the page that is contained in the iframe will contain the FileUpload control and it will be posted with a normal postback to the server and hence provides a feeling like AJAX request. We will see about this in future code snippets.

Inserting Dynamic Text Box Data Into A Database

Generally Every one can create daynamic text box and they want to store the text which has been entered in to the textbox by the user and they feel its very hard to read the data from the text boxes which are created dynamically.
This blog explains you how to insert daynamic text box data into a SQL Server database.
here I have text boxes that are being generated dynamically . text boxes will be generated while user clicks on Add new Row button and as well as textbox will be removed when the user click on the remove Row button. and I have explained each of those values inserted into the database that hold the results.
The Text boxs will be generated number of times. Here I've wrote you a sample code. It can dynamcially create rows of textboxes and add those values into database. I hope my example will help you to figure out what you should do next.

i have took one hidden control to hold the number of dynamic controls. and i am creating the text box befor the page is post backing. Client side javascript call will help to to increase the
hidden feild value. place below java script in header tag. (click on the code to view enlarged..)


once increment script done. creat a hiddent control with a value as "0" (zero) . and create a asp.net table to hold the dynamic controls. as well s create a button to call the client side script to add new row. and creat one more button to save the data which has entered in to dynamic controls. (click on the code to view enlarged..)




Once we added the controls we need to add the code snippet to create the dynamic controls for each and every time when the user click on the Add new Row button. here if we write the code in page is post back the dynamic controlls will not create for each client side click . to hold the the control view state and creating the dynamic controll i am writing the code in Page Load event.


if (HiddenField1.Value != "")
{
rows = int.Parse(HiddenField1.Value);
for (int i = 0; i <>

{

TableRow tr = new TableRow();

TableCell tc = new TableCell();

TextBox tb = new TextBox();

tb.ID = "FORM" + i.ToString();

tc.Controls.Add(tb);

CheckBox tb2 = new CheckBox();

tb2.Text = "Check if it is a Report";

tb2.ID = "RChk" + i.ToString();

TableCell tc2 = new TableCell();

tc2.Controls.Add(tb2);

tr.Cells.Add(tc); tr.Cells.Add(tc2);

Table1.Rows.Add(tr);

}

if (rows <>

{

btnRemove.Visible = false;

frmname.Visible = false;

}

else

{

frmname.Visible = true; btnRemove.Visible = true;

}

}

here i am making the form name Title visible true only when atleast one row is crated.

How to Retrive the dynamic Control data:

So far we have seen how the dynamic controll will be created while user clicks on a button controll now i am going to give a clear picture how to retive the data from the dynamic control.

as we have took hidden control and assigend the number for ID as prefix of the perticular control.

so using a small for loop we can get back the perticular dynamic controll data. for more inoformation look below code.


for (int i = 0; i <>

{

TextBox Form = Table1.Rows[i].Cells[0].FindControl("FORM" + i.ToString()) as TextBox;

CheckBox F_type = Table1.Rows[i].Cells[1].FindControl("RChk" + i.ToString()) as CheckBox;

if (F_type.Checked)

{

string _F_Type= "REPORT Checked";

}

string _Form_name = Form.Text;

if (Form.Text.Length > 0)
{
result = SaveFormName(_F_Type,_Form_name);
}
}
HiddenField1.Value = Convert.ToString(0);
Table1.Rows.Clear();

Here Save formname will have the dyanamic control data as function arguments to be saved in data base. Hope you enjoyed with the above code.

Share

Twitter Delicious Facebook Digg Stumbleupon Favorites