Monday, 28 May 2012

PATINDEX And CHARINDEX


PATINDEX
Returns the starting position of the first occurrence of a pattern in a specified expression, or zeros if the pattern is not found, on all valid text and character data types.


Syntax:
PATINDEX ( '%pattern%', expression )
Eg: PATINDEX('%[0-9]%'@string)



PATINDEX for complex search
The following search will find all bikes which off-road yet comfortable. 
Select  ProductNumber ,[Description] from ProductDescription pd
where patindex( ‘%off-road%comfortable%’, pd.[Description]) > 0
ProductNumber
Description

BK-M18S-12
Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride.

BK-M18S-21
Suitable for any type of riding, on or off-road. Fits any budget. Smooth-shifting with a comfortable ride.

CHARINDEX
Searches an expression for another expression and returns its starting position if found.
SYNTAX:
CHARINDEX ( expressionToFind ,expressionToSearch [ , start_location ] )
Eg:CHARINDEX(' ',@String)
start_location
        It is optional.
If start_location is not specified, is a negative number, or is 0, the search starts at the beginning of expressionToSearch.

Thursday, 24 May 2012

Find Page Control in Custom Gridview

 SqlDataSource d = this.Parent.FindControl("SqlDataSource1") as SqlDataSource;

Tuesday, 22 May 2012

Get DataTable From Gridview


bool firstTime = true;
    System.Data.DataTable dt;
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
            if (this.firstTime)
            {
                System.Data.DataView dv =
                     (e.Row.DataItem as System.Data.DataRowView).DataView;
                this.dt = dv.ToTable();               
                this.firstTime = false;
            }
    }

Add ListItemCollection Property To Custom Gridview

   private ListItemCollection m_Items=null;

   [DesignerSerializationVisibility(DesignerSerializationVisibility.Content),  
    NotifyParentProperty(true), 
    PersistenceMode(PersistenceMode.InnerProperty)
     ] 
        public virtual ListItemCollection Items
        {
            get
            {
                if (this.m_Items == null)
                {
                    this.m_Items = new ListItemCollection ();
                   
                }
                 return this.m_Items;
            }
        }

Saturday, 12 May 2012

Embedded StyleSheet File with Custom Control


How to embedded StyleSheet file with custom control? Question has been opened many time on ASP.NET forum.
So in this article, I will try to demonstrate how to achieve this task.
In General
In general, if you want to embedded any thing with custom control, you need first to make its "Build Action" property to be "Embedded Resource", and then you need to add to the custom control assemblies using the <Assembly> attribute typically above class name, and finally you need to register in the page so it can be rendered.
I will illustrate all these things in details through the article.

Step One: make it as Embedded Resource
 After you added the StyleSheet file to your Custom Control Class Library project, first thing you need to do is to make build action property for the stylesheet file as embedded resource, otherwise your page will not be able to see the stylesheet file.
To do this, from the VS do a right click on the stylesheet file, and click on Properties,  and then change the "Build Action" property to be "Embedded Resource".

Step Two: Add it to the Custom Controls Assemblies
 Second thing you need to do is to register the Css file to the custom control assemblies by adding the below code typically above class name

[assembly: WebResource("YourProjectName.StyleSheetFileName.css""text/css")]
namespace YourNameSpace
{
   public class MyCustomControl : CompositeControl
    {
       //Custom Control Rest Code

 Step Three: Register Css File on the Page
Finally, we have to tell the Page that our custom control has Css file needed to be registered when the final HTML is generated for that page. So we need to write a bit of code in the OnInit method.

protected override void OnInit(EventArgs e)
{
    base.OnInit(e);
   
    string css = "<link href=\"" + Page.ClientScript.GetWebResourceUrl(this.GetType(),
    "YourProjectName.CssFileName.css") + "\" type=\"text/css\" rel=\"stylesheet\" />";
   
               
    Page.ClientScript.RegisterClientScriptBlock(this.GetType, "cssFile", css, false);
}

And that's it, you can now use the CSS StyleSheet file within your Custom Control.

Wednesday, 2 May 2012

See Text Of Stored Proceure -sp_helptext

 sp_HelpText is used  to see the text of the stored procedure


 sp_helptext StoredProcedurename

 Eg.
 sp_helptext sp_helptext

watermark for Password Text box css and javascript


<style type="text/css">
        .WaterMarkedTextBox
        {
            height: 16px;
            width: 168px;
            padding: 2px 2 2 2px;
            border: 1px solid #BEBEBE;
            background-color: #F0F8FF;
            color: gray;
            font-size: 8pt;
            text-align: center;
        }
        .WaterMarkedTextBoxPSW
        {
            background-position: center;
            height: 16px;
            width: 168px;
            padding: 2px 2 2 2px;
            border: 1px solid #BEBEBE;
            background-color: #F0F8FF;
            color: white;
            vertical-align: middle;
            text-align: right;
            background-image: url(Images/psw_wMark.png);
            background-repeat: no-repeat;
        }
        .NormalTextBox
        {
            height: 16px;
            width: 168px;
        }
    </style>



 <script language="javascript" type="text/javascript">
         function Focus(objname, waterMarkText) {
             obj = document.getElementById(objname);
             if (obj.value == waterMarkText) {
                 obj.value = "";
                 obj.className = "NormalTextBox";
                 if (obj.value == "User ID" || obj.value == "" || obj.value == null) {
                     obj.style.color = "black";
                 }
             }
         }
         function Blur(objname, waterMarkText) {
             obj = document.getElementById(objname);
             if (obj.value == "") {
                 obj.value = waterMarkText;
                 if (objname != "txtPwd") {
                     obj.className = "WaterMarkedTextBox";
                 }
                 else {
                     obj.className = "WaterMarkedTextBoxPSW";
                 }
             }
             else {
                 obj.className = "NormalTextBox";
             }

             if (obj.value == "User ID" || obj.value == "" || obj.value == null) {
                 obj.style.color = "gray";
             }
         }
    </script>


<body>
    <form id="form2" runat="server">
    <div>
        <h3>
            Watermark Textbox using JavaScript and CSS</h3>
    </div>
    <table>
        <tr>
            <td>
                User Id
            </td>
            <td>
                <asp:TextBox ID="txtUserId" runat="server"
              onfocus="Focus(this.id,'User ID')"
                    onblur="Blur(this.id,'User ID')"
                  Width="126px" CssClass="WaterMarkedTextBox">User ID</asp:TextBox>
            </td>
        </tr>
        <tr>
            <td>
                Password
            </td>
            <td>
                <asp:TextBox ID="txtPwd" TextMode="Password"
              runat="server" onfocus="Focus(this.id,'')"
                    onblur="Blur(this.id,'')" Width="126px"
                     CssClass="WaterMarkedTextBoxPSW" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Submit" />
            </td>
        </tr>
    </table>
    </form>
</body>

 Image