Thursday, December 09, 2010

Silverlight: MessageBox with Title, OK, and Cancel Buttons

A message box is a dialog box that is used to display an alert or a message. A message box can have a title and multiple options such as Yes/No or  OK/Cancel. A message box can also have some input control to take input from a user.

The MessageBox class in WPF represents a modal message box dialog, which is defined in the System.Windows namespace. The Show static method of the MessageBox is the only method that is used to display a message box. The Show method returns a MessageBoxResult enumeration that has values OK, Cancel, Yes, and No. We can use MessageBoxResult to find out what button was clicked on a MessageBox and take an appropriate action.

The following code snippet creates a MessageBox with a message, a title, and two OK and Cancel buttons.

 MessageBoxResult result = MessageBox.Show("Are you sure you want to delete this Deposit?",
                                    "Delete", MessageBoxButton.OKCancel);
      if (result == MessageBoxResult.OK)
      {
         // do if your result is OK
      } 
      else
      {
         // do if result is cancel
      }

Hope this help Smile

Sunday, December 05, 2010

T-SQL: Change tables owner to dbo with sp_changeobjectowner

Sometimes there is a need to change all tables in the database to be owned by dbo for maintenance or to fix up accidental errors. All tables owned by dbo schema is usually best practices in the database application development with MSSQL.

The following small SQL code snippet goes through all user tables in the database and changes their owner to dbo. It usessp_changeobjectowner system stored procedure

DECLARE tabcurs CURSOR
FOR
    SELECT 'dips.' + [name]
      FROM sysobjects
     WHERE xtype = 'u'
OPEN tabcurs
DECLARE @tname NVARCHAR(517)
FETCH NEXT FROM tabcurs INTO @tname
WHILE @@fetch_status = 0
BEGIN
    EXEC sp_changeobjectowner @tname, 'dbo'
    FETCH NEXT FROM tabcurs INTO @tname
END
CLOSE tabcurs
DEALLOCATE tabcurs

Hope this is useful..

Javascript Tip: Close Pop-up - Refresh Parent

Sometimes we need to force parent refresh on closing pop-up window which was opened from this parent window. There are many ways to make it work, however the most elegant way will be using location.href

function Refresh() 
{
    window.opener.location.href = window.opener.location.href;
    window.close();
}

Smile

ASP.NET Tip: Render Control into HTML String

How to get string representation of ASP.NET control or in other words render it into string instead of let it be rendered on the page. Here is the sample code to Render control

using System.Text;
using System.IO;
using System.Web.UI;
public string RenderControl(Control ctrl) 
{
    StringBuilder sb = new StringBuilder();
    StringWriter tw = new StringWriter(sb);
    HtmlTextWriter hw = new HtmlTextWriter(tw);
    ctrl.RenderControl(hw);
    return sb.ToString();
}

Saturday, December 04, 2010

How to Create Dynamically a Property Class from T-SQL

Some times we need to write huge classes with properties, its time consuming and boring . Here is a work around how you can do this from T-SQL.

use master
go
Declare @tableName varchar(50)
SET @tableName = 'bangaram'
SELECT case when sc.isnullable<>0 AND st.name NOT LIKE 'varchar' AND st.name NOT LIKE 'text' then  
'private System.Nullable<'+REPLACE(REPLACE(REPLACE(REPLACE(st.name,'varchar','string'),'bit','bool'),'datetime','DateTime'),'text','string')+ '> _'  +sc.name +';
 public System.Nullable<'+REPLACE(REPLACE(REPLACE(REPLACE(st.name,'varchar','string'),'bit','bool'),'datetime','DateTime'),'text','string')+ '> '+sc.name
else 
'private '+REPLACE(REPLACE(REPLACE(REPLACE(st.name,'varchar','string'),'bit','bool'),'datetime','DateTime'),'text','string')+ ' _'  +sc.name +';
 public '+REPLACE(REPLACE(REPLACE(REPLACE(st.name,'varchar','string'),'bit','bool'),'datetime','DateTime'),'text','string')+ ' '+sc.name  
  end ,
        '{
            get { return this._'+sc.name+'; }
            set { if ((this._'+sc.name+' != value)) { this._'+sc.name+' = value; } }
        }'
FROM Syscolumns sc INNER JOIN Systypes st on st.xtype=sc.xtype
WHERE sc.id in(SELECT id FROm sysobjects WHERE name=@tableName)
ORDER BY sc.id

Replace @tablename with your table name in your database table. One more thing I have give @tablename size as varchar(50) increase if your tablename is bigger. That’s all you need to do apart from little bit of formatting..Good luck guys. Have fun..Smile