Friday, November 23, 2007

Error: Input string was not in a correct format.

How to avoid Error "Input string was not in a correct format"

To avoid error always make check whether you are passing a valid value to function.

Example:

Code Contain Error
protected void Page_Load(object sender, EventArgs e)
{
string str1 = "3.5";
string str2 = "";

//Here we know that str2 is blank, but when values are assigned dynamic it is
//Best practise to make check before passing value to function.
TestFn(float.Parse(str1),float.Parse(str2));
}

private void TestFn(float value1, float value2)
{
//Do Something...
}


Code with Check to Avoid Error
protected void Page_Load(object sender, EventArgs e)
{
string str1 = "3.5";
string str2 = "";

float fStr1;
if (str1 != string.Empty)
fStr1 = float.Parse(str1);
else
fStr1 = 0;

float fStr2;
if (str2 != string.Empty)
fStr2 = float.Parse(str2);
else
fStr2 = 0;

//Values are parse and check for validity before passing to function
TestFn(fStr1,fStr2);
}

private void TestFn(float value1, float value2)
{
//Do Something...
}

Note: Code is just for example and has practically of no meaning.

Sunday, November 18, 2007

Design Patterns in ASP.NET 2.0

Good Link for Design Patterns in ASP.NET 2.0

Tuesday, November 06, 2007

Site Navigation control in asp.net

There are three Site Navigation control in asp.net 2.0

  • SiteMapPath Control
  • Menu Control
  • Treeview Control
Before using Site Navigation control let go through overview of web.sitemap, which is used as datasource to assign this control.

What is Web.SiteMap and How to Create Web.SiteMap file?
Web.SiteMap file is an XML File, which contains details of navigation that is followed by navigation control.

For Creating Web.SiteMap file
- Right click the project in solution explorer and add new item.
- Select Site Map from the add new item dialog.
- It will create "web.sitemap" file in your root directory


A Sample format of Site Map
<siteMapNode url="" title="" description="">
<siteMapNode url="" title="" description="" />
<siteMapNode url="" title="" description="" />
</siteMapNode>


Here, siteMapNode contains following properties.
Url - describes URL to be redirect on node click.
Title - describes Text to be displayed on node.
Description - describes Text to be displayed as Tooltip.

For creating sub node, create a siteMapNode inside parent siteMapNode.
A sample web.sitemap file displaying contries and its associated cities of sales region.
<siteMapNode url="default.aspx" title="Countries" description="Sales Contries">

<siteMapNode url="india.aspx" title="India" description="Sales for India">
<siteMapNode url="ahmedabad.aspx" title="Ahmedabad" description="Sales for Ahmedabad" />
<siteMapNode url="mumbai.aspx" title="Mumbai" description="Sales for Mumbai" />
<siteMapNode url="delhi.aspx" title="Delhi" description="Sales for Delhi" />
<siteMapNode url="chennai.aspx" title="Chennai" description="Sales for Chennai" />
<siteMapNode url="bangalore.aspx" title="Bangalore" description="Sales for Bangalore" />
</siteMapNode>

<siteMapNode url="usa.aspx" title="USA" description="Sales for USA" >
<siteMapNode url="edison.aspx" title="Edison - NJ" description="Sales for Edison - NJ" />
<siteMapNode url="stcharles.aspx" title="St. Charles - IL" description="Sales for St. Charles - IL" />
<siteMapNode url="la.aspx" title="Los Angeles - CA" description="Sales for Los Angeles - CA" />
</siteMapNode>
</siteMapNode>


Understanding SiteMapPath Control
SiteMapPath control automatically uses web.sitemap file located in the root of your application.

Drag and drop, SiteMapPath control and it will automatically display naviagation path paved by web.sitemap file.

Example Figure1:


Example Figure2:




Understanding TreeView Control
TreeView control display data in hierarchical order.

Drag an drop the TreeView control on to webform, note TreeView control doesn't use web.sitemap as default datasource, you need to assign it explicitly.



Select "New data source" and select sitemap and press ok.



That's it you are done.



Displaying checkbox with treeview control, you can assign "ShowCheckBox" property of TreeView control to either
  • All - Checkbox to all node.
  • Root - Checkbox to root node.
  • Parent - Checkbox to parent node.
  • Leaf - Checkbox to leaf node.
  • None - No Checkbox



Displaying Selected node code in Treeview control
protected void btnSelected_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
foreach (TreeNode node in TreeView1.CheckedNodes)
{
sb.Append(node.Text + "<br>");
}
Response.Write("You have selected following node: <br>" + sb.ToString());
}




Understanding Menu Control
Like TreeView Control you need to explicitly assign data source to menu control.


Select "New data source" and select sitemap and press ok.



That's it you are done.

Bydefault menu control display root node, which generally contains single node, so you may choose option "StaticDisplayLevels" Property to 2 to display node from 2nd level...

Example, here i am displaying node from 2nd level so my StaticDisplayLevels will be 2.

Friday, November 02, 2007

Expand your .Net Network

Expand your .Net Network

Its sometime difficult to solve the unknown problem, which might be few hits for someone who is expert in a given area. It is better to join together. .Net forums are good way to solve the problem, but still it takes time to resolve the error, also it is doesn't screen under ever expert eyes who might can solve your problem. So lets join each of us on Messenger with our expertise area, so that one can ping each other easily based on their problem to expert.

DotNetGuts Group is maintaining database, where in you can find user and their messenger info. based on their technology area working in, so that one can share and acquire knowledge

Find out Info Complete list

GTalk-Id: dotnetguts@gmail.com
Yahoo-Id: vivek_on_chat
MSN-Id: vivek.patel

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape