Friday, February 25, 2011

How to populate ComboBox from an array in asp.net





ComboBoxArray.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>  
  
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  <script runat="server">  
    protected void Button1_Click(object sender, EventArgs e)  
    {  
        string[] Colors = {"BurlyWood","Chartreuse","Salmon","Ivory","Magenta"};  
        ComboBox1.DataSource = Colors;  
        ComboBox1.DataBind();  
    }  
</script>  
<head id="Head1" runat="server">  
    <title>Ajax ComboBox - How to populate ComboBox from an array in asp.net</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            ASP.NET Ajax ComboBox - How to populate  
            <br /> ComboBox from an array in asp.net  
        </h2>  
        <hr width="475" align="left" color="Pink" />  
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
        </asp:ToolkitScriptManager>  
        <asp:ComboBox   
            ID="ComboBox1"   
            runat="server"   
            DropDownStyle="DropDown"   
            AutoCompleteMode="None"  
            CaseSensitive="false"  
            RenderMode="Block"  
            AutoPostBack="false"  
            Font-Names="Comic Sans MS"  
            Font-Size="Large"  
            ForeColor="SeaGreen"  
            >  
        </asp:ComboBox>  
        <br /><br /><br />  
        <br /><br /><br />  
        <asp:Button   
            ID="Button1"  
            runat="server"  
            OnClick="Button1_Click"  
            Text="Populate ComboBox From Array"  
            Height="45"  
            Font-Bold="true"  
            ForeColor="DodgerBlue"  
            />  
    </div>  
    </form>  
</body>  
</html>

Using ComboBox AutoCompleteMode Append in asp.net






ComboBoxAutoCompleteModeAppend.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>  
  
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<script runat="server">  
    protected void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        Label1.Text = "You select: " + ComboBox1.SelectedItem.Text;  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>Ajax ComboBox - Using ComboBox AutoCompleteMode Append in asp.net</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            ASP.NET Ajax ComboBox - Using ComboBox  
            <br /> AutoCompleteMode Append in asp.net  
        </h2>  
        <hr width="500" align="left" color="Pink" />  
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
        </asp:ToolkitScriptManager>  
        <asp:Label   
            ID="Label1"  
            runat="server"  
            Font-Size="Large"  
            ForeColor="Green"  
            >  
        </asp:Label>  
        <br /><br />  
        <asp:ComboBox   
            ID="ComboBox1"   
            runat="server"   
            DropDownStyle="DropDown"   
            AutoCompleteMode="Append"  
            CaseSensitive="false"  
            RenderMode="Block"  
            AutoPostBack="true"  
            Font-Names="Comic Sans MS"  
            Font-Size="Large"  
            OnSelectedIndexChanged="ComboBox1_SelectedIndexChanged"  
            >  
            <asp:ListItem Text="DarkRed"></asp:ListItem>  
            <asp:ListItem Text="SaddleBrown"></asp:ListItem>  
            <asp:ListItem Text="DarkSalmon"></asp:ListItem>  
            <asp:ListItem Text="DeepPink"></asp:ListItem>  
            <asp:ListItem Text="IndianRed"></asp:ListItem>  
            <asp:ListItem Text="SkyBlue"></asp:ListItem>  
        </asp:ComboBox>  
    </div>  
    </form>  
</body>  
</html>


Using ComboBox AutoCompleteMode Suggest in asp.net


ComboBoxAutoCompleteModeSuggest.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>  
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
<script runat="server">  
    protected void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        Label1.Text = "You choose: " + ComboBox1.SelectedItem.Text;  
    }  
</script>  
<head id="Head1" runat="server">  
    <title>Ajax ComboBox - Using ComboBox AutoCompleteMode Suggest in asp.net</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            ASP.NET Ajax ComboBox - Using ComboBox  
            <br /> AutoCompleteMode Suggest in asp.net  
        </h2>  
        <hr width="475" align="left" color="LightBlue" />  
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
        </asp:ToolkitScriptManager>  
        <asp:Label   
            ID="Label1"  
            runat="server"  
            Font-Size="Large"  
            ForeColor="SaddleBrown"  
            Font-Italic="true"  
            >  
        </asp:Label>  
        <br /><br />  
        <asp:ComboBox   
            ID="ComboBox1"   
            runat="server"   
            DropDownStyle="DropDown"   
            AutoCompleteMode="Suggest"  
            CaseSensitive="false"  
            RenderMode="Block"  
            AutoPostBack="true"  
            Font-Names="Comic Sans MS"  
            Font-Size="X-Large"  
            OnSelectedIndexChanged="ComboBox1_SelectedIndexChanged"  
            >  
            <asp:ListItem Text="Coral"></asp:ListItem>  
            <asp:ListItem Text="Cornsilk"></asp:ListItem>  
            <asp:ListItem Text="SandyBrown"></asp:ListItem>  
            <asp:ListItem Text="DeepPink"></asp:ListItem>  
            <asp:ListItem Text="Gray"></asp:ListItem>  
            <asp:ListItem Text="Crimson"></asp:ListItem>  
        </asp:ComboBox>  
    </div>  
    </form>  
</body>  
</html>

Thursday, February 24, 2011

To set change ComboBox AutoPostBack programmatically in asp.net


ComboBoxAutoPostBack.aspx

<%@ Page Language="C#" AutoEventWireup="true" %>    
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>  
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<script runat="server">  
    protected void Button1_Click(object sender, EventArgs e)  
    {  
        ComboBox1.AutoPostBack = false;  
        Label1.Text = "ComboBox AutoPostBack Now: False";  
    }  
    protected void Button2_Click(object sender, EventArgs e)  
    {  
        ComboBox1.AutoPostBack = true;  
        Label1.Text = "ComboBox AutoPostBack Now: True";  
    }  
    protected void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)  
    {  
        ComboBox1.ForeColor = System.Drawing.Color.FromName(ComboBox1.SelectedItem.Text);  
    }  
</script>  
  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>Ajax ComboBox - How to set change ComboBox AutoPostBack programmatically in asp.net</title>  
</head>  
<body>  
    <form id="form1" runat="server">  
    <div>  
        <h2 style="color:DarkBlue; font-style:italic;">  
            ASP.NET Ajax ComboBox - How to set change ComboBox  
            <br /> AutoPostBack programmatically in asp.net  
        </h2>  
        <hr width="525" align="left" color="Pink" />  
        <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">  
        </asp:ToolkitScriptManager>  
        <asp:Label   
            ID="Label1"  
            runat="server"  
            Font-Size="Large"  
            ForeColor="Green"  
            >  
        </asp:Label>  
        <br /><br />  
        <asp:ComboBox   
            ID="ComboBox1"   
            runat="server"   
            DropDownStyle="DropDown"   
            AutoCompleteMode="None"  
            CaseSensitive="false"  
            RenderMode="Block"  
            AutoPostBack="true"  
            OnSelectedIndexChanged="ComboBox1_SelectedIndexChanged"  
            Font-Names="Comic Sans MS"  
            Font-Size="Large"  
            ForeColor="SeaGreen"  
            >  
            <asp:ListItem Text="Red"></asp:ListItem>  
            <asp:ListItem Text="Green"></asp:ListItem>  
            <asp:ListItem Text="Blue"></asp:ListItem>  
            <asp:ListItem Text="DeepPink"></asp:ListItem>  
            <asp:ListItem Text="SkyBlue"></asp:ListItem>  
        </asp:ComboBox>  
        <br /><br /><br />  
        <br /><br /><br />  
        <asp:Button   
            ID="Button1"  
            runat="server"  
            OnClick="Button1_Click"  
            Text="Set AutoPostBack False"  
            Height="45"  
            Font-Bold="true"  
            ForeColor="DodgerBlue"  
            />  
        <asp:Button   
            ID="Button2"  
            runat="server"  
            OnClick="Button2_Click"  
            Text="Set AutoPostBack True"  
            Height="45"  
            Font-Bold="true"  
            ForeColor="DodgerBlue"  
            />  
    </div>  
    </form>  
</body>  
</html>  

Conversion from Currency into English Words


Function Dollars(ByVal amount As Decimal, _Optional ByVal EmptyStringIfZero As Boolean = True) As String
Dim result As String = String.Empty
Select Case amount
Case 0
       result = If(EmptyStringIfZero, String.Empty, "Zero ")
Case 1 To 19
       result = Choose(amount, "One ", "Two ", "Three ", "Four ", _
"Five ", "Six ", "Seven ", "Eight ", "Nine ", "Ten ", "Eleven ", _
"Twelve ", "Thirteen ", "Fourteen ", "Fifteen ", "Sixteen ", _
"Seventeen ", "Eighteen ", "Nineteen ")
Case 20 To 99
       result = Choose(amount \ 10 - 1, "Twenty ", "Thirty ", _
"Fourty ", "Fifty ", "Sixty ", "Seventy ", "Eighty ", _
"Ninety ") & Dollars(amount Mod 10)
Case 100 To 999
         result = Dollars(amount \ 100) & "Hundred" & IIf _
(amount >= 200, "s ", " ") & Dollars(amount Mod 100)
Case 1000 To 999999
         result = Dollars(amount \ 1000) & "Thousand" & IIf _
(amount >= 2000, "s ", " ") & Dollars(amount Mod 1000)
Case 1000000 To 999999999
         result = Dollars(amount \ 1000000) & "Million" & IIf _
(amount >= 2000000, "s ", " ") & Dollars(amount Mod 1000000)
Case Is >= 1000000000
         result = Dollars(amount \ 1000000000) & "Billion" & _
IIf(amount >= 2000000000, "s", " ") & Dollars(amount Mod 1000000000)
End Select
Return result
End Function
 
Public Function Cents(ByVal amount As Decimal) As String
Dim result As String = amount.ToString
If result.Contains("."c) Then
      result = result.Substring(result.IndexOf("."c) + 1)
Else
      result = "00"
End If
Return " and " & result & "/100"
End Function

Visual Studio Regular Expression Editing tips

VS provides regular expressions in replacing string which can be a lifesaver. Here's a few examples of operations to be performed on many files:

Removing an xml attribute:
What the RE means: Find the string "<Survey " followed by a bunch of characters, followed by the string "cluster=" followed by a bunch of non-space characters. Replace with just the "<Survey " and the characters before "cluster".

regex:  to delete the attribute "cluster" from the "Survey" element:
Find What: \<Survey {.*} cluster=[^ ]*
Replace With: \<Survey \1

Rename an attribute
In this case I want to rename the "Survey" element's 'id' attribute to 'registrarId', but not the other elements' 'id' attribute. The element name and the attribute to change must be on the same line for this to work. A tiny sliver of regular expression in the "Find and Replace" dialog did the trick: The "Find What" says find "<Survey " followed by any characters followed by " id=". The "\1" takes the value of all the characters skipped with the {.*}.

Find What:  \<Survey {.*} id=
Replace with: \<Survey \1 registrarId=

SQL Rounding Up Issues

The first question they ask is why SQL Server truncates the decimals which is silly because it happens in the code-behind.For instance, say that you have a field of decimal datatype with precision 5 and scale 2 – decimal(5, 2).Now if you don’t define precision of the output parameter the value will be rounded up. e.g. 0.99 becomes 1.



CREATE PROCEDURE mySP
(
     @MyDecValue decimal(5, 2) output
)
AS
     SELECT @MyDecValue = MyDecField FROM MyTable WHERE Foo=1

Dim command As SqlCommand = connection.CreateCommand
command.CommandText = "mySp"
command.Parameters.Add("@MyDecValue", SqlDbType.Decimal, 5).Direction = Output

Now if you execute the command the value will be as mentioned rounded up .. meaning, 0.99 becomes 1
You can solve it if you Define the precision and scale of the parameter e.g.
Dim command As SqlCommand = connection.CreateCommand
command.CommandText = "mySp"
Dim decimalsparam As New SqlParameter
decimalsparam.Direction = ParameterDirection.Output
decimalsparam.ParameterName = "@MyDecValue"
decimalsparam.Precision = 5
decimalsparam.Scale = 2
decimalsparam.Size = 5
 
command.Parameters.Add(decimalsparam)
command.ExecuteNonQuery()

Tuesday, February 22, 2011

To create text file and write text programmatically





StreamWriter.aspx

&lt;%@ Page Language="C#" %&gt;  
&lt;%@ Import Namespace="System.IO" %&gt;  
  
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;  
  
&lt;script runat="server"&gt;  
  
    protected void Button1_Click(object sender, System.EventArgs e)  
    {  
        string appPath = Request.PhysicalApplicationPath;  
        string filePath = appPath + "Text.txt";  
        StreamWriter w;  
        w = File.CreateText(filePath);  
        w.WriteLine("This is a test line.");  
        w.WriteLine("This is another line.");  
        w.Flush();  
        w.Close();  
        Label1.Text = "File created and write successfully!&lt;br /&gt;";  
        Label1.Text += filePath;  
    }  
&lt;/script&gt;  
  
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;  
&lt;head id="Head1" runat="server"&gt;  
    &lt;title&gt;How to create text file and write text programmatically in asp.net&lt;/title&gt;  
&lt;/head&gt;   
&lt;body&gt;  
    &lt;form id="form1" runat="server"&gt;  
    &lt;div&gt;  
        &lt;h2 style="color:Red"&gt;asp.net StreamWriter example:&lt;br /&gt;Create File and Write In It&lt;/h2&gt;  
        &lt;asp:Label   
             ID="Label1"   
             runat="server"   
             Font-Size="Large"  
             ForeColor="SeaGreen"  
             Font-Bold="true"  
             Font-Italic="true"  
             &gt;  
        &lt;/asp:Label&gt;  
        &lt;br /&gt;&lt;br /&gt;  
        &lt;asp:Button   
             ID="Button1"   
             runat="server"   
             OnClick="Button1_Click"  
             Font-Bold="true"  
             Text="Create File and Write Text"  
             ForeColor="SeaGreen"  
             /&gt;     
    &lt;/div&gt;  
    &lt;/form&gt;  
&lt;/body&gt;  
&lt;/html&gt;