Saed Al Shaar Dev Tips and Tricks

.NET and SharePoint from a different angle!

Archive for the category “ASP .NET”

Date and Time manipulation

Hi Folks,

Recently I was working with dates and times inside my application, and it took a long time since I figured out how to deal with dates and time.

The senario is that I have an applciation where I store DateTime values in a SQL column, and I created a Form to filter the records based on From Date and To Date.

Here is the Table structure:

And here is sample of the data inside the table:

As you can see that it consists of 2 parts, Date and Time.
Then I created the following form to filter the date column between to dates:
And to filter the dates you can use the following code:
If FromDateTextBox.Text <> “” And ToDateTextBox.Text <> “” Then
           Dim fd As DateTime = DateTime.Parse(LTrim(RTrim(FromDateTextBox.Text)))
           Dim td As DateTime = DateTime.Parse(LTrim(RTrim(ToDateTextBox.Text)))
           criteria += ” AND (Convert(varchar(20), CreatedOn, 101) BETWEEN ‘” & fd & “” & ” And” & td & “)”
End If
The explination:
Convert(varchar(20), CreatedOn, 101 will take the Date only part from the CreatedBy column amdf then we can compare it.
Another note that you need to put the variables values between singlr brackets ‘value’.
Happy programming!
Saed
Advertisements

Report Viewer Parameters in Visual Studio 2010 – No Code

Hi Folks,

I’ve been trying since a long time finding an article of how to pass parameters to a Report Viewer control in Visual Studio 2010, but most of the articles needs lots of code to be written.

But, now I have found the way to create a report and pass parameters easily with only 1 line of code, yes I am serious 1 line of code!! 🙂

And below I will describe the steps with screen shots:
1- You need to create a new Visual Studio Web Site:

2- Then you will have an empty web site.

3- Now, I will create a new database with one table with three columns (ID, Name, City):

Then fill it with some data:

Now I will create a data set for the Contacts table:

Now I will Rebuild the web site:

Now, we need to add reference to Microsoft.ReportViewer.WebForms:
Make sure to select Version 10.0.0.0

Now, I will create a report:


Save the report.

Now, I will start to build the page of the report:
– Add a DropdownList and a Button:

– Drop a Script Manager control into the page:

– Drop a ReportViewer control:

– Choose the Report we created before, and an ObjectDataSource will be created directly:

– On the Click event of the button write the only Code you need to write to refresh the report:
ReportViewer1.LocalReport.Refresh()

Congratulations!! the web site is ready and the report takes the parameter and refreshes.

Happy programming!
Saed

Trick: Roles in ASP.NET and Windows Authentication

Hi Folks,

I have been reading about how to enable Roles in a Windows Authentication in an ASP.NET Web Application, and then I came throught this trick which I wanted to share with you, here it goes:

One tip/trick I like to use is to take advantage of the “Application_Start” event handler within Global.asax to setup my Roles if they don’t already exist, and map any initial users into them if necessary. To-do this, choose File – Add New Item and select the “Global.asax” file item. Then add this code to your Application_Start event handler:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
If (Roles.RoleExists(“Auditors”) = False) Then
    Roles.CreateRole(“Auditors”)
End If
If (Roles.RoleExists(“Approvers”) = False) Then
    Roles.CreateRole(“Approvers”)
End If
If (Roles.RoleExists(“Admins”) = False) Then
    Roles.CreateRole(“Admins”)
    Roles.AddUserToRole(“YOURCOMPANY\yourusername”, “Admins”)
End If
End Sub

Or, you can check the Scott’s full post in the following link:
Recipe: Implementing Role-Based Security with ASP.NET 2.0 using Windows Authentication and SQL Server

Happy programming!
Saed

Handling Delete Command in GridView

Hi Folks,

Here are how to handle the Delete Command in a GridView control.

Protected Sub GridView1_RowDeleted(sender As Object, e As System.Web.UI.WebControls.GridViewDeletedEventArgs) Handles GridView1.RowDeleted

If (e.Exception Is Nothing) AndAlso e.AffectedRows.Equals(1) Then
lblMessage.Text = “Deleted sccessfully.”
ElseIf (TypeOf e.Exception Is SqlException) AndAlso DirectCast(e.Exception, SqlException).Number.Equals(547) Then
lblMessage.Text = “Referential integrity issue.”
e.ExceptionHandled = True
Else
lblMessage.Text = “Cannot be deleted.”
e.ExceptionHandled = True
End If

End Sub

Happy programming!
Saed

Textbox to Accept only Number!

Hi Folks,
Many of the blogs or articles on the net talking about using JavaScript and type code to do that, there is an easier way to that with CompareValidator in ASP.NET.

Here is the CompareValidator you need to have:

ControlToValidate=TextBox1
Text=Enter Numeric Value
Operator=DataTypeCheck
Type=Integer

Happy programming!
Saed

LINQ Datasource and Error: Operator ‘=’ incompatible with operand types ‘String’ and ‘Int32’

Hi folks,

It’s an issue when you try to filter LINQ datasource within Visual Studio, where you choose Control and then it will show “==” in the Operator as follows:

When you try to run the page, it will give you and error saying:
Operator ‘=’ incompatible with operand types ‘String’ and ‘Int32’

All what you need to do is to change the Convert the Parameteres into the required data type as follows:

And it will magically work!

Happy programming!!
Saed

Report Viewer Control in Visual Studio

Hi Folks,

Here I would like to direct you to some of the useful video casts that I used to know more about the Report Viewer control that exists in Visual Studio 2005 till 2010.

First I will start with a simple report viewer video cast on Channel 9:
http://channel9.msdn.com/Blogs/trobbins/Introducing-the-Report-Viewer

Secondly, after that you need to know how to pass parameteres to the report, you can watch this in the following youtube video cast:
http://www.youtube.com/watch?v=sXJmRHgSAS8

I hope this will help!

Happy coding.

Changing Acive Directory Properties using ASP.NET VB.NET

Hi folks,

The IT Manager asked me to develop a new system for giving the employees the ability to change thier details in the Active Directory, and it took me aloooong time to figure out the way of doing this, in an easy way.
I would like to share with you the structore and the code of the application, for anyone who wants to do that 🙂
I posted the code into a project in CodePlex here:
Happy programming!
Saed

Post Navigation

%d bloggers like this: