Mar 5 2006

Using Method inside a DataGrid or GridView TemplateField

Category: ASP.NET 1.x | ASP.NET 2.0 - GeneralBil@l @ 09:55

Sometimes there is a need to do some action on a field returned from the database and displayed inside a TemplateField inside the DataGrid or GridView columns.

For example, you might have an email address inside your database and you want to set the HREF of the a tag as a link with the "mailto:" appended at the beginning of the field.

I usually use a small helper method, that takes as input the field coming from the databasee and formats for me the display I want. In the example below, I am displaying the full name of the customer or client, as a link to his/her email address.

GridView Example:

<asp:TemplateField HeaderText="Name">
    <ItemTemplate>
        <a href='<%# FormatUrl(Eval("email1").ToString())%>'><%# Eval("fname") %>,&nbsp;<%# Eval("lname") %></a>
    </ItemTemplate>
</asp:TemplateField>

As you can see, I am calling a method called FormatUrl with an input the email field we're getting from the database.

DataGrid Example:

<asp:TemplateColumn HeaderText="Name">
    <ItemTemplate>
        <a href='<%# FormatUrl(Eval("email1").ToString())%>'><%# Eval("fname") %>,&nbsp;<%# Eval("lname") %></a>
    </ItemTemplate>
</asp:TemplateColumn>

Now the FormatUrl method is as simple as the following:

public string FormatUrl(string email)
{
    return "mailto:" + email;
}

                

The idea is so simple, we are just calling a heler method, giving it the needed fields to return a formatted string as we want.

Hope this helps,

Regards

Tags: ,

Mar 4 2006

AddMessageModule in DotNetNuke

Category: DotNetNukeBil@l @ 21:38

I was developing a Custom Registration Module in DotNetNuke. I followed the same technique used in the default registration module that comes with DotNetNuke, however, I had to do my own cutomizations and using my own tables and objects.

I noticed the heavy use of AddModuleMessage used by this module. This method does nothing but show a message at the top of the module.

I digged a bit in the DotNetNuke code and knew that when the page loads, the DNN creates the default page, and adds a PlaceHolder called MessagePlaceHolder(if not mistaken), to the top of the ContentPane Section of the page above any module added. So when an error happens, DNN finds this PlaceHolder, make it visible and display the error inside it.

My problem was that, the registration form found at www.devconnect.net was a long form, and sometimes the page is showing the last items in the form, and the user pressed the Register button, if there is an error above, the user will stay in his/her same position without noticing what happened. All what happened was that a problem like a field was not filled is required or a system error happened, like username using is used in th database with another user. The latter message is displayed by DNN in the PlaceHolder and the method placing the error there is the AddModuleMessage. What I wanted is that, when an error happens, I need the user to be able to see the error, how? I created the method below to make the page automatically scroll to the PlaceHolder that is showing the error message after a call to the AddModuleMessage method is done, check the code and the comments below:

// Scroll to top of module
public void Scroll2TopModule()
{
    // Set page position on top when the AddModuleMessage is functional
    // First I found the place holder containing the error message displayed by AddModuleMessage
        PlaceHolder msgHolder = (PlaceHolder)this.Parent.FindControl("MessagePlaceHolder");

    // If this place holder is visible, it is only visible when the AddModuleMesssage is called
    if (msgHolder.Visible)
        {
            // I down casted the current PortalModuleBase’s page into the CDefault, which I 
            // think it is the default page of the portal
            // then, I used the ScrollToControl method found in the default.aspx page, 
            // this method takes as input a control to scroll to
            // I gave it the parent of the Message Holder, why? The Message holder is usually added 
            // to the Content Page, so I told the method to scroll to the head of the Content Pane
                ((CDefault)this.Page).ScrollToControl(msgHolder.Parent);
        }
}

I hope this method helps you. It helped me a lot.

Regards,

Tags:

Mar 4 2006

Difference between NULL and DBNull

I have always misunderstood the difference between NULL and DBNULL until recently while I was working on ASP.NET web application, and was talking to a Webservice and I found an exception coming from one of the web methods inside the Webservice which is using the SqlCommand.ExecuteScalar() method.

What happens when the ExecuteScalar() method is used? Usually, the ExecuteScalar() method returns the First Column of the First Row returned by the database stored procedure or SQL query.

Suppose for example you are searching for the CustomerPhone of the customer named "Bilal Haidar", and that customer didn't have any record in the database, so a query such as:

public string Get_Customer_ID(string UserName)
{
   // Prepare database connection
   SqlConnection conn = new SqlConnection("...");
   // Prepare command to execute
   SqlCommand cmd = new SqlCommand("SELECT CustomerPhone FROM Custoers WHERE CustomerName = 'Bilal Haidar'", conn);

   // Execute the command and store result in an object
   object objResult = cmd.ExecuteScalar();






if (objResult == null) return "Customer not found in our database";
if (objResult == System.DBNull.Value) return "Customer found but his/her phone number is null"; return (string) objResult; }

So now here how it goes. The query will search for the Customer Phone. We are returning the result and storing it in the objResult object.

If the result object is null, then there is no record in the database with the username specified.

If the result value is not null, we need to check if the Cutomer Phone field has a value or not, how do you do so? Using the DBNull.Value would check for you if the field returned is null or not.

So, comparing the ExecuteScalar() to null means, we are checking if there is a record returned, and checking if value of the first column of the first row is null or not, means we need to check against DBNul..Value

Hope you have now a better understanding of the difference between NULL and DBNull

Regards

Tags: , , , ,

Mar 4 2006

ASP.NET and DNN Website deploy

When you developer an ASP.NET web application or a DotNetNuke application, you will be having all types of file extensions:

  • .csproj
  • .cs
  • .vb
  • .su
  • .sln
  • etc ...

So, imagine you are developing a huge web application or even a DotNetNuke website and now it is time to deploy your application. Are you going to go through each single folder in your web application, remove all un needed files and leave the aspx, ascx, css, etc .. files for deployment? Maybe that can be done for small applications, but not for DoetNetNuke applications nor for huge ASP.NET web applications.

The solution found here is called DNNDeploy which you can read more about it here:

ASP.NET Deployment App

This tool goes through all your files in the application, allows you to choose what file extensions to keep and generates a web site ready for deployment.

I am sure this helps us a lot.

Regards

Tags: , ,

Mar 4 2006

Debug a DotNetNuke Modules

Category: DotNetNukeBil@l @ 20:26

While developing Custom Modules in DotNetNuke, there comes a time where  you need to debug your modules, or specific user controls.

You can download the dnndebug.aspx page from the following link:

DNNDebug.aspx

All you need to do is place this aspx page in the root of your DNN installation, and give it the full path of the control you want to debug as follow:

~/DesktopModules/MyModule/UserControl1.ascx

It will parse the user control, and display for you the errors generated by this usercontrol.

Hope this helps you identify your errors and make your DNN development easier.

Regards

Tags:

Mar 4 2006

I am back . . .

Category: General | GeneralBil@l @ 20:09

Hello:

You might be asking where I have been all this time. Well, I have been away a bit from posting, first for getting some sickness which I am over it by now and second, I was working on a community website for Microsoft in the area of Middle East.

The website is www.devconnect.net, it is a Microsoft Development Community site. I invite you all and welcome you all to the webisite, to register their, to have the chance to write your own articles, code samples, share with us your experience, knowledge, help on the forums, and get helped.

The website was my first experience in DotNetNuke. I learned a lot in the lsat few weeks in the field of DotNetNuke, developed my own custom modules and enjoyed working with it a lot.

In the coming few days, I will be posting many interesting code samples and ideas that I have gathered in the last few weeks.

Regards to all,

Tags: ,