Thursday, April 14, 2011

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


Wednesday, April 13, 2011

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

Monday, April 11, 2011

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

Saturday, April 9, 2011

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

Friday, April 8, 2011

Calculated Columns in SharePoint Lists Dilimiter Issue

There are many blogs and articles talking about the Calculated Columns in SharePoint or Excel , but few of them highlight the issue with the dilimiters of the formulas.

http://office.microsoft.com/en-au/windows-sharepoint-services-help/examples-of-common-formulas-HA010105479.aspx?CTT=3

While formulating the formula, you need to know what is the character used by your SharePoint to dilimiter the paramerters, usually it's the comma (,), but sometimes it's the simicoln (;).

So, if you are sure about the formula and it's not working, then try to change the dilimiter and it will work.
Example:
=TEXT([Event Date],"MMM")        
>>> can be changed to >>>>
=TEXT([Event Date];"MMM")

Happy programming!
Saed

Tuesday, April 5, 2011

Executing SQL commands on SharePoint List

Hi Folks,

Some times you need to run some SQL commnads on a SharePoint List trying to copy some content from one column into another ... etc.

The solution for this is Microsoft Access (2007/2010), simply create a new Access database.
Type the list URL:
Then choose the List you want:

Then create a new Query:

Then Run the query.

And it will update the List inside the SharePoint portal.

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