Code Snippets in visual studio 2005 - ASP.NET

10 04 2008

Hello friends I want to share something on “Code Snippets” with you.

Code Snippet

Q: What is a “Code Snippet”?

A: A Code Snippet is a reusable block of code. Unlike a static copy-and-paste approach, code snippets allow for dynamic instances of code

by allowing placeholders into mini templates of code. Code snippets are a new feature of Visual Studio 2005.

Q: How do I use a Code Snippet?

A: In the Visual Studio 2005 code editor, you can invoke snippets with [ctrl][k][x] keyboard shortcut. IntelliSense will then display a

context menu displaying the available code snippets to choose. However, most code snippets are available without using the keyboard

shorcut. If you know the shortcut name of the code snippet, simply type the name and press tab to invoke.

Now we will see how we can use snippets here.
I will write a small snippet for disconneceted data access with dataset.

1) Open nottepad.
2) Type the code in it and save it as sample.snippet.

<?xml version="1.0" encoding="utf-8" ?>
<CodeSnippets xmlns=”http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet”>
<CodeSnippet Format=”1.0.0″>
<Header>
<Title>Disconnected Data architecture</Title>
<Shortcut>Dataset</Shortcut>
<Description>Code snippet for Disconnected data access</Description>
<Author>Bharath Radhekrishna</Author>
<SnippetTypes>
<SnippetType>Expansion</SnippetType>
<SnippetType>SurroundsWith</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Code Language=”csharp”><![CDATA[dataset
SqlConnection cn = new SqlConnection();
string selquery = $selquery$;
SqlDataAdapter da = new SqlDataAdapter(selquery, cn);
DataSet ds = new DataSet();
da.Fill(ds);
]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>


3) After saving this. Go to visual studio 2005. Click Tools > CodeSnippets manager or ctrl+K, ctrl+B.
and add this snippet to library.

4) While you are coding right click the mouse. You will see an option of “insert snippet”. when you click it will tell us
to insert the snippet we like. For example if take the above snippet it will be displayed with the name as:
“Disconnected Data architecture”.If we click it will display the code as:

SqlConnection cn = new SqlConnection();
string selquery = ;
SqlDataAdapter da = new SqlDataAdapter(selquery, cn);
DataSet ds = new DataSet();
da.Fill(ds);

So everytime it is not necessary for us to write whole sentence or connections.It will reduce our burden of writing commonly used
code everytime.

For more info on Code snippets you can refer to:

http://gotcodesnippets.com/faq.aspx
http://www.google.com/search?q=how+to+insert+a+code+snippet+in+visual+web+developer+2005&sourceid=navclient-ff&ie=UTF-8&rlz=
1B3GGGL_enIN246IN246.

Screens

Codesnippet while right clicking mouse





Retrieving port number with sql query

31 03 2008


Declare @PortNumber VarChar(7)

Exec Xp_RegRead

@RootKey=’HKEY_LOCAL_MACHINE’,

@Key=’Software\Microsoft\Microsoft Sql Server\SQLEXPRESS\MSSQLServer\SuperSocketNetLib\Tcp’,

@Value_Name=’TcpPort’,

@Value=@PortNumber OutPut

Print ‘Port Number:’

Print @PortNumber

When you execute this you will get a problem to enable XP_CMDshell.You can enable by this.Read the article at:
Enabling xp_cmdshell in SQL Server 2005





Retrieving IP address of a system with SQL Query

31 03 2008

Retrieving IP address of a system with SQL Query

CREATE PROCEDURE getIPAdress

(

@Hostname VARCHAR(255)

)

AS

SET NOCOUNT ON

CREATE TABLE #Results

(

Results VARCHAR(4000)

)

DECLARE @Commandstring VARCHAR(300)

SET @Commandstring = ‘ping ‘ + @Hostname

INSERT INTO #Results

EXEC master..xp_cmdshell @Commandstring

Select DISTINCT SUBSTRING(Results,12,CHARINDEX(’:',Results)-12) AS HostIpAdress from #Results

Where Results LIKE ‘Reply From%’

DROP TABLE #Results

GO
============================
To know result

exec getIPAdress ‘ABCXP10′

ABCXP10 is a system name

Blogged with Flock

Tags:





javascript with response.redirect

29 03 2008

Q) I had been trying to use javacript to alert some message and after that I do a response.redirect to another page.

But the alert message seems not appearing and it just redirect to another page.

A) use the below code.here we use location.replace

Location.replace

Syntax:
location.replace(URL)

The replace method replaces the current History entry with the specified URL. After calling the replace method, you cannot navigate back to the previous URL using the browser’s Back button.

Usage

string s = “alert(’Your Profile Is Successfully Updated’);location.replace(’Client_home.aspx’);”;
Page.ClientScript.RegisterStartupScript(this.GetType(), “sri”, s, true);

Blogged with Flock





Windows officelive website doesnt support safari web browser

27 03 2008

When I am trying to open office live it is taking url as:
http://home.officelive.com/Misc/CompatibleShell.aspx?linkId=UnsupportedBrowser

It means office live not supporting safari web browser
https://home.officelive.com

View the screen shot at:
http://www.flickr.com/photos/89692283@N00/2365278477/

Blogged with Flock





Links for Content Management system in ASP.NET

25 03 2008

Open source

http://www.dotnetnuke.com/
http://www.rainbowportal.net/
http://drupal.org/


http://aspalliance.com/simplecms/default.aspxhttp://www.codeplex.com/SampleCMS

http://www.codeplex.com/umbraco/Release/ProjectReleases.aspx?ReleaseId=6344

http://www.davidpirek.com/CMS/


http://graffiticms.com/
http://www.kentico.com/
http://www.axcms.net/en_axcms_home.AxCMS?ActiveID=1848

Blogged with Flock

Tags:





validate textbox with only numbers using Javascript code

10 03 2008

We can validate textbox with only numbers by writing this Javascript code and calling at onkeypress event of text box

<!–
      function isNumberKey(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }
      //–>

   <asp:TextBox ID=”txt_maxage” Onkeypress=”return isNumberKey(event)” runat=”server”></asp:TextBox>

Blogged with Flock

Tags:





Play music when mouse hovers on a link in ASP.NET

10 03 2008

Javascript code
============

Write this in .aspx page.

<script language=”JavaScript”><!–
// Sound on Mouseover javascript supplied by http://www.hypergurl.com

var aySound = new Array();
// PLACE YOUR SOUND FILES BELOW
aySound[0] = “sound002.wav”;
aySound[1] = “sound003.wav”;
aySound[2] = “sound004.wav”;
aySound[3] = “sound005.wav”;
aySound[4] = “sound006.wav”;
aySound[5] = “sound007.wav”;
aySound[6] = “sound008.wav”;
aySound[7] = “sound009.wav”;
aySound[8] = “sound010.wav”;
// Don’t alter anything below this line

IE = (navigator.appVersion.indexOf(”MSIE”)!=-1 && document.all)? 1:0;
NS = (navigator.appName==”Netscape” && navigator.plugins["LiveAudio"])? 1:0;
ver4 = IE||NS? 1:0;
onload=auPreload;

function auPreload() {
if (!ver4) return;
if (NS) auEmb = new Layer(0,window);
else {
Str = “<DIV ID=’auEmb’ STYLE=’position:absolute;’></DIV>”;
document.body.insertAdjacentHTML(”BeforeEnd”,Str);
}
var Str = ”;
for (i=0;i<aySound.length;i++)
Str += “<EMBED SRC=’”+aySound[i]+”‘ AUTOSTART=’FALSE’  LOOP=’TRUE’ HIDDEN=’TRUE’>”
if (IE) auEmb.innerHTML = Str;
else {
auEmb.document.open();
auEmb.document.write(Str);
auEmb.document.close();
}
auCon = IE? document.all.soundfiles:auEmb;
auCon.control = auCtrl;
}
function auCtrl(whSound,play) {
if (IE) this.src = play? aySound[whSound]:”;
else eval(”this.document.embeds[whSound].” + (play? “play()”:”stop()”))
}
function playSound(whSound) { if (window.auCon) auCon.control(whSound,true); }
function stopSound(whSound) { if (window.auCon) auCon.control(whSound,false); }
//–></script>
<!– –>


=============
Code in ASP.NET
=============

<td width=”51%” valign=”top”  class=”nav”><div><a href=”#” class=”htext2″ onmouseover=”playSound(0)” onmouseout=”stopSound(0)”>&ndash; Command Altitude<br />
          </a>
              <br />
          <a href=”#” class=”htext2″ onmouseover=”playSound(2)” onmouseout=”stopSound(2)”>&ndash;Command Heading</a><br />
              <br />
          </div>
            <div><a href=”#” class=”htext2″ onmouseover=”playSound(3)” onmouseout=”stopSound(3)”>&ndash;Command Speed</a>
                <br />
              <br />
              <a href=”#” class=”htext2″ onmouseover=”playSound(4)” onmouseout=”stopSound(4)”>&ndash;Override</a> </div>
          </td>
          <td width=”51%” valign=”top”  class=”nav”><div><a href=”#” class=”htext2″ onmouseover=”playSound(5)” onmouseout=”stopSound(5)”>&ndash;Permanent   Override<br />
          </a>
              <br />
          </div>
              <div><a href=”#” class=”htext2″ onmouseover=”playSound(6)” onmouseout=”stopSound(6)”>&ndash;Temporary Override</a><br />
                  <br />
              </div>
            <div class=”htext2″>&ndash;<a href=”#” class=”htext2″ onmouseover=”playSound(7)” onmouseout=”stopSound(7)”>Cruise</a><br />
            </div>
            <div>
                <br />
                <a href=”#” class=”htext2″ onmouseover=”playSound(8)” onmouseout=”stopSound(8)”>&ndash;Manual Heading</a></div></td>
        </tr>
      </table>
    </td>

Any doubts mail me:  radhek@gmail.com

Blogged with Flock

Tags: ,





Full screen with javascript in ASP.NET

6 03 2008

 
Here I am givinng javascript code by using which we can make full screen or normal mode.
This code wcan be useful in ASP.NET

<SCRIPT LANGUAGE=”JavaScript”>

function fullScreen(theURL)
{
window.opener=null;
this.close();
window.open(theURL, ”, ‘fullscreen=yes, scrollbars=auto’);

}
function fuScreen(theURL)
 {
 window.opener=null;
 this.close();
window.open(theURL, ”, ‘fullscreen=no, toolbar=yes ,menubar=yes ,status=yes ,scrollbars=yes, location=yes, resizable=yes, maximized=0, height=800, width=1280, left=0,top=0′);
}
function window_onload()
{
window.moveTo(0,0);
top.window.resizeTo(screen.availWidth,screen.availHeight);

}

//  End –>
</script>

Using this in ASP.NET
=================

<td width=300 align=”right” valign=”middle”>
                <a href=”javascript:fullScreen(’Override_attack_Tut3.aspx’);”><img src=”images/fullscreen mode.jpg” border=”0″ /></a><td>
            <td width=300 align=”center” valign=”middle”>
                <a href=”javascript:fuScreen(’Override_attack_Tut3.aspx’);”><img src=”images/normal mode.jpg”border=”0″ /></a><td>

Blogged with Flock

Tags:





Creating of Random numbers within the given range.

1 03 2008

To generate a random number in a given range.
We can use the .NET bult-in Random class.

Random randobj = new Random();
       
        test1.Text = randobj.Next(0, 359).ToString();
        test2.Text = randobj.Next(60, 110).ToString();
        test3.Text = randobj.Next(60, 350).ToString();

In this way we can generate random number with in range in asp.net

Any doubts mail me: radhek@gmail.com

Blogged with Flock