Thursday, May 21, 2009

Improvement to return a single row from stored procedure

Ok, the method I posted previously wasn't good enough. There is no handling for null result. Updated method:

public static ThisTable GetUser(int uid)
{
        DataClasses1DataContext dataContext = new DataClasses1DataContext();

        ThisTable myTable = new ThisTable();
        ISingleResult result = dataContext.GetUser(uid);

        var details = result.ToList();
        if (details.Count > 0)
        {
                myTable = details[0];
        }
        return myTable;
 }

Wednesday, May 13, 2009

Returning a single row from stored procedure

The stored procedure in LINQ to SQL will return ISingleResult type by default. 

If the result returned is not going to be used as a DataSource and a loop / list with a capacity of 1 is not favourable, a solution might be to access a single rowset and map it to an Entity.

  1. In the dbml file, right-click the Data Function created for the stored procedure in the right pane and select "Properties".

  2. Change the Return Type from (Auto-generated Type) to the Table Class; which should be available in the drop down (e.g. ThisTable).

  3. Use the following method to access the first (and only) item in the result returned:
public static ThisTable GetUser(int uid)
{
      ThisTable myTable= new ThisTable();
     DataClasses1DataContext dataContext = new DataClasses1DataContext();
      ISingleResult result = dataContext.GetUser(uid);
      return result.ToList()[0];
}

Friday, May 08, 2009

How can a textbox fill 100% of a table cell?

Ever wanted to have a textbox fill 100% of a table cell?

For unknown reason, <asp:TextBox ID="uxName" runat="server" Width="100%" /> will not work as expected.

It will work only if the actual style is set.
i.e. <asp:TextBox ID="uxName" runat="server" Width="100%" />

Strange, but true.

Monday, May 04, 2009

LINQ to SQL with dynamic sql and temporary tables

Recently I was fiddling with .NET Framework 3.5 and LINQ. LINQ is a very interesting concept. When working with Framework 2.0, I used to create classes manually to store the data retrieved from Database. Now, we have a drag-and-drop, automatic way of generating classes.

Over the weekend, I was using LINQ to SQL to execute an existing stored procedure which returned results by using dynamic sql. However, when I dropped the procedure into the dbml file, it generated a function with the return type as integer.

After a quick round of googling, it turns out that LINQ to SQL does not work as expected with dynamic sql, as well as temporary tables.

Not wanting to do any modification on the shared stored procedure, a workaround is required.

The solution is surprisingly simple.
  1. Replace the affected stored procedure with a dummy one, that returns all the fields the normal one should return (name and type must be same).
  2. Drop this dummy stored procedure into the dbml file. This will generate the correct return type for that procedure.
  3. Modify the dummy stored procedure back to the correct one.

That's all!! A very quick and dirty solution.