Monday, 22 November 2010

C# Error: Input string was not in a correct format

You will receive a C# error �Input string was not in a correct format� , when you try to convert a string value into integer. To prevent this error either check the value of string is integer or not or keep conversion block inside try catch block.

other method is to use TryParse  to convert string to integer.

How to produce Code:

using System;
using System.Collections.Generic;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
        string str = "one";
        int i = Convert.ToInt32(str);    
        Console.WriteLine("Numeric Value is: " + i);
        Console.ReadLine();
        }
    }
}

C# Error: Input string was not in a correct format

How to prevent Error:

Method 1:

int i = 0;
    try
    {
        i = Convert.ToInt32(str);
    }
    catch (Exception ex)
    {
    }

 

Method 2:

Int32.TryParse(str,out i);

How to add Values with Space in Infragistics MultiSelectDropdownList

This sample code will help you to format infragistics dropdown values using pre and post spaces. suppose that you are getting name and id column in Ds dataset and you want to add these values in cmbList infragistics dropdown.

Loop through each and every values and replace black values with  . also you need to decode the given string and add to combo list.

ASP.NET Code:

Register section:

<%@ Register Assembly="MultiSelectDropdownList" Namespace="MultiSelectDropdownList"
    TagPrefix="cmb" %>

Tag Definition:

<cmb:MultiSelectDropdownList ID="cmbList" runat="server" >                            
                           </cmb:MultiSelectDropdownList>

C# Code:

public void FormattedCombo(DataSet Ds)
   {
       int i = 0;
       for (i = 0; i < Ds.Tables[0].Rows.Count; i++)
       {
           System.IO.StringWriter writer = new System.IO.StringWriter();
           string str = Ds.Tables[0].Rows[i]["Name"].ToString().Replace(" ", "&nbsp;");
           Server.HtmlDecode(str, writer);
           cmbList.Items.Insert(i + 1, new ListItem(writer.ToString(), Ds.Tables[0].Rows[i]["id"].ToString()));
           writer.Close();
       }
   }

Thursday, 11 November 2010

How to use IndexOf to Ignore Case in C#

This program will help you find given string location inside another string location with ignoring the case.

C# Code to use IndexOf to Ignore Case

using System;
using System.Collections.Generic;
using System.Text;
using System.Globalization;

namespace MyApp
{
    class Program
    {
        static void Main(string[] args)
        {
        string string1 = "C# Tutorial by CSharpTalk.com.";
        string string2 = "BY";

        CompareInfo myCompare = CultureInfo.InvariantCulture.CompareInfo;
        int Pos = myCompare.IndexOf(string1, string2, CompareOptions.IgnoreCase);
        Console.WriteLine("String Found at: " + Pos);
        Console.ReadLine();
        }
    }
}

Output:

How to use IndexOf to Ignore Case

How to hide Client side Control on Server Side in C#

Recently i was doing a asp.net programming and faced a interesting issue, I had a client side (html input button) button control and wanted to hide in C# on the basis of certain conditions. After a long research i found a solution.

To hide HTML button from C#, just add the button inside the ASP.NET Panel control and hide the ASP.NET Panel.

ASP.NET Code:
<asp:panel id="pnlSave" Runat="server"><input id="btnSave" onclick="javascript:btn_Save()" type="button" value="Save"/></asp:panel>

C# Code:
 pnlSave.Visible = false;

This is the simplest solution while you can go for:
1. Add a hidden field in ASP Page and after calculation assign true or false to this hidden field.
2. on the basis of this hidden field value hide or show the HTML button.