web 2.0

Friday, April 16, 2010

how to bing Gridview with Paging ?

'The following example demonstrates how to build a grid view dynamically at runtime.
'Get the connection string, In this case it's comming from the web.config file

Dim strCon As String = System.Configuration.ConfigurationManager.ConnectionStrings("con").ConnectionString
Dim con As New SqlConnection(strCon)
Dim cmd As New SqlCommand("[SEL_Results]", con)

cmd.CommandType = Data.CommandType.StoredProcedure

Dim yourParameter As SqlParameter = cmd.Parameters.Add("@yourParameter", Data.SqlDbType.VarChar)
yourParameter.Value = theValue


'Setting the dataAdapter to the sqlCommand.
Dim da As New SqlDataAdapter(cmd)
Dim ds As New DataSet

Try

con.open()
da.Fill(ds)

Catch ex As Exception

Finally
con.Close()
End Try

Dim dc As DataColumn

If ds.Tables.Count > 0 Then

'Building all the columns in the table.

For Each dc In ds.Tables(0).Columns

Dim bField As New BoundField
'Initalize the DataField value.
bField.DataField = dc.ColumnName
'Initialize the HeaderText field value.
bField.HeaderText = dc.ColumnName
'Add the newly created bound field to the GridView.
grdView.Columns.Add(bField)
Next

End If
'Setting the dataSource of the grid here.

grdView.DataSource = ds.Tables(0)

grdView.dataBind()


'The following example demonstrates paging and sorting in the gridview



private string GetSortDirection()
{
switch (GridViewSortDirection)
{
case "ASC":
GridViewSortDirection = "DESC";
break;

case "DESC":
GridViewSortDirection = "ASC";
break;
}
return GridViewSortDirection;
}

protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
BindGridControl();
gridView.PageIndex = e.NewPageIndex;
gridView.DataBind();
}

protected DataView SortDataTable(DataTable dataTable, bool isPageIndexChanging)
{
if (dataTable != null)
{
DataView dataView = new DataView(dataTable);
if (GridViewSortExpression != string.Empty)
{
if (isPageIndexChanging)
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GridViewSortDirection);
}
else
{
dataView.Sort = string.Format("{0} {1}", GridViewSortExpression, GetSortDirection());
}
}
return dataView;
}
else
{
return new DataView();
}
}

Monday, March 8, 2010

Modeling Databases Using LINQ to SQL:

Visual Studio "Orcas" ships with a LINQ to SQL designer that provides an easy way to model and visualize a database as a LINQ to SQL object model. My next blog post will cover in more depth how to use this designer (you can also watch this video I made in January to see me build a LINQ to SQL model from scratch using it).

Using the LINQ to SQL designer I can easily create a representation of the sample "Northwind" database like below:

My LINQ to SQL design-surface above defines four entity classes: Product, Category, Order and OrderDetail. The properties of each class map to the columns of a corresponding table in the database. Each instance of a class entity represents a row within the database table.

The arrows between the four entity classes above represent associations/relationships between the different entities. These are typically modeled using primary-key/foreign-key relationships in the database. The direction of the arrows on the design-surface indicate whether the association is a one-to-one or one-to-many relationship. Strongly-typed properties will be added to the entity classes based on this. For example, the Category class above has a one-to-many relationship with the Product class. This means it will have a "Categories" property which is a collection of Product objects within that category. The Product class then has a "Category" property that points to a Category class instance that represents the Category to which the Product belongs.

The right-hand method pane within the LINQ to SQL design surface above contains a list of stored procedures that interact with our database model. In the sample above I added a single "GetProductsByCategory" SPROC. It takes a categoryID as an input argument, and returns a sequence of Product entities as a result. We'll look at how to call this SPROC in a code sample below.

Understanding the DataContext Class

When you press the "save" button within the LINQ to SQL designer surface, Visual Studio will persist out .NET classes that represent the entities and database relationships that we modeled. For each LINQ to SQL designer file added to our solution, a custom DataContext class will also be generated. This DataContext class is the main conduit by which we'll query entities from the database as well as apply changes. The DataContext class created will have properties that represent each Table we modeled within the database, as well as methods for each Stored Procedure we added.

For example, below is the NorthwindDataContext class that is persisted based on the model we designed above:

LINQ to SQL Code Examples

Once we've modeled our database using the LINQ to SQL designer, we can then easily write code to work against it. Below are a few code examples that show off common data tasks:

1) Query Products From the Database

The code below uses LINQ query syntax to retrieve an IEnumerable sequence of Product objects. Note how the code is querying across the Product/Category relationship to only retrieve those products in the "Beverages" category:

C#:

VB:

2) Update a Product in the Database

The code below demonstrates how to retrieve a single product from the database, update its price, and then save the changes back to the database:

C#:

VB:

Note: VB in "Orcas" Beta1 doesn't support Lambdas yet. It will, though, in Beta2 - at which point the above query can be rewritten to be more concise.

3) Insert a New Category and Two New Products into the Database

The code below demonstrates how to create a new category, and then create two new products and associate them with the category. All three are then saved into the database.

Note below how I don't need to manually manage the primary key/foreign key relationships. Instead, just by adding the Product objects into the category's "Products" collection, and then by adding the Category object into the DataContext's "Categories" collection, LINQ to SQL will know to automatically persist the appropriate PK/FK relationships for me.

C#

VB:

4) Delete Products from the Database

The code below demonstrates how to delete all Toy products from the database:

C#:

VB:

5) Call a Stored Procedure

The code below demonstrates how to retrieve Product entities not using LINQ query syntax, but rather by calling the "GetProductsByCategory" stored procedure we added to our data model above. Note that once I retrieve the Product results, I can update/delete them and then call db.SubmitChanges() to persist the modifications back to the database.

C#:

VB:

6) Retrieve Products with Server Side Paging

The code below demonstrates how to implement efficient server-side database paging as part of a LINQ query. By using the Skip() and Take() operators below, we'll only return 10 rows from the database - starting with row 200.

C#:

VB:

Summary

LINQ to SQL provides a nice, clean way to model the data layer of your application. Once you've defined your data model you can easily and efficiently perform queries, inserts, updates and deletes against it.

Hopefully the above introduction and code samples have helped whet your appetite to learn more. Over the next few weeks I'll be continuing this series to explore LINQ to SQL in more detail.

Hope this helps,

What Is LINQ to SQL?

LINQ to SQL is an O/RM (object relational mapping) implementation that ships in the .NET Framework "Orcas" release, and which allows you to model a relational database using .NET classes. You can then query the database using LINQ, as well as update/insert/delete data from it.

LINQ to SQL fully supports transactions, views, and stored procedures. It also provides an easy way to integrate data validation and business logic rules into your data model.

Monday, January 18, 2010

Email function in asp.net

//Before this you must install IIS in your computer
using System.Web.Mail;
namespace EmailApplication
{
///
/// Summary description for _Default.
///
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
}
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
}
#endregion
// button used to send the email
protected void Button1_Click(object sender, System.EventArgs e)
{
MailMessage mail = new MailMessage();
mail.To = txtTo.Text;
mail.From = txtFrom.Text;
mail.Subject = txtSubject.Text;
mail.Body = txtBody.Text;
MailAttachment attachment=null;
try
{
// check to see if there are any attachments or not
if(FileList.Items.Count > 0)
{
foreach(ListItem l in FileList.Items)
{
// Attaches a new attachment contained in the List
Response.Write(l.Text);
attachment = new MailAttachment(l.Text);
mail.Attachments.Add(attachment);
}
}
//mail.Attachments.Add(attachment);
Response.Write("Number of attachments are"+mail.Attachments.Count);
SmtpMail.SmtpServer = "localhost";

SmtpMail.Send(mail);
}
catch(Exception ex)
{
Response.Write(ex.Message);
// Cacthes the exception here
}
}
// Button used to attach a file
protected void Button2_Click(object sender, System.EventArgs e)
{
// gets the file name with the whole path
string attachedFile = File1.PostedFile.FileName;
// Adds the item in the list of attached files
if(attachedFile != "")
{
FileList.Items.Add(attachedFile);
}
}
}
}

Calendar Control in javascript

Calendar <><><> <><><> <><><> <><><> <><><> <><><> <><><> <><><><><><> <><><> <><><> <><><> <><><> <><><> <><><>