Html.TextBoxFor in MVC 2 doesn't allow the setting of the value property using the object html attributes.
syntax -
<%= Html.TextBoxFor(model => model.city, new { maxlength = "40" })%>
If you need to set a default value on a textbox field, you need to use the old
syntax -
<%= Html.TextBox("city", "New Delhi", new { maxlength = "40" })%>
Tuesday, August 28, 2012
MVC: set Default value in Html.TextboxFor Helper In Asp.Net MVC 2
Thursday, March 1, 2012
Generic function call
public class clsGeneric
{
public void show<T>(T value)
{
Type t = typeof(T);
Console.WriteLine(t.Name + ":" + value);
}
}
class Program
{
static void Main(string[] args)
{
clsGeneric ng;
ng = new clsGeneric();
int a = 10;
ng.show(a);
string b = "SS";
ng.show(b);
Console.WriteLine("Press any key to close........");
Console.ReadKey();
}
}
OUTPUT
----------------------------
Int32:10
String:SS
Press any key to close........
use global variable with same name of Local variable
public class Demo1
{
int a = 10;
public void ff()
{
int a = 11;
Console.WriteLine("a={0}", this.a);
}
class Program
{
static void Main(string[] args)
{
Demo1 ng = new Demo1();
ng.ff();
Console.WriteLine("Press any key to close........");
Console.ReadKey();
}
}
Output:
-----------------------------------
a=10
Press any key to close........
-----------------------------------
Tuesday, February 14, 2012
Bind generic dictionary collection to combobox
Dictionary<string, string> DicSearch;
DicSearch = new Dictionary<string, string>();
DicSearch.Add("1", "Mohit");
DicSearch.Add("2", "Agrawal");
cboSearch.DataSource = new BindingSource(DicSearch, null);
cboSearch.DisplayMember = "value";
cboSearch.ValueMember = "key";
DicSearch = null;
Tuesday, February 7, 2012
SqlCommand Query based on parameters
SqlCommand command = new SqlCommand();
command.CommandText = "SELECT * FROM Emplyoee emp WHERE emp.Name=@EmpName";
command.Parameters.Add(new SqlParameter(" @EmpName", 50));
// Execute the SQL Server command...
SqlDataReader reader = command.ExecuteReader();
DataTable tblemplyee = new DataTable();
tblemplyee.Load(reader);
foreach (DataRow rowProduct in tblemplyee.Rows)
{
// Use the data...
}
NOTE: This is a good way to block SQL injection
"" vs String.Empty
String.Empty is faster than "" because it doesn't create a new string object instance. Creating new instances brings
penalties on both execution speed and memory usage.
Example:
Example:
// Slowest
str1 == "";
// Slow
str1.Equals(" ") == false
//Fast
str1 == String.Empty
// Fastest
str1.Equals(String.Empty) == false
Monday, January 23, 2012
Working with Nullable Types
******************* Type 1 ***********************
int iUnits;
int iStock ;
if (iStock== null)
{
iUnits= 0;
}
else
{
iUnits= (int)iStock;
}
******************* Type 2 ***********************
// using the coalesce operator, ??,
int iUnits= iStock ?? 0;
The coalesce operator works like this: if the first value (left hand side) is null, then C# evaluates the second expression (right hand side).
int iUnits;
int iStock ;
if (iStock== null)
{
iUnits= 0;
}
else
{
iUnits= (int)iStock;
}
******************* Type 2 ***********************
// using the coalesce operator, ??,
int iUnits= iStock ?? 0;
The coalesce operator works like this: if the first value (left hand side) is null, then C# evaluates the second expression (right hand side).
Declare Null datetime variable
****************** Type 1 *********************
System.Nullable<DateTime> dt;
dt = null;
****************** Type 2 *********************
DateTime? dt1;
dt1 = null;
System.Nullable<DateTime> dt;
dt = null;
****************** Type 2 *********************
DateTime? dt1;
dt1 = null;
How to Use foreach in Multiple classes
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Indexer
{
public class NameDetail
{
public string FirstName { get; set; }
public string MiddleName{ get; set; }
public string LastName { get; set; }
public string FullName
{
get
{
String strFullname="";
if (string.IsNullOrEmpty(FirstName) == false )
{
strFullname = FirstName;
}
if (string.IsNullOrEmpty(MiddleName) == false)
{
strFullname += MiddleName;
}
if (string.IsNullOrEmpty(LastName) == false)
{
strFullname += LastName;
}
return strFullname.Trim();
}
}
}
public class Name: IDisposable, IEnumerable, IEnumerator
{
List arrPos = new List();
public int count { get ;set ;}
private int Position = -1; // This is for Ienum interface
public NameDetail this[int pos]
{
get
{
try
{
return arrPos[pos];
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
set
{
if (arrPos.Count <= pos)
{
arrPos.Insert(pos, value);
count = arrPos.Count;
}
else
{
throw new Exception("No Index postiion");
}
}
}
#region IDisposable
public void Dispose()
{
arrPos = null;
}
#endregion IDisposable
//**********************************************************
//********************* Use For Each in object *************
//*********************************************************
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public bool MoveNext()
{
if (Position < arrPos.Count - 1)
{
++Position;
return true;
}
return false;
}
public void Reset()
{
Position =-1;
}
public object Current
{
get
{
return arrPos[Position];
}
}
#endregion
}
class Program
{
static void Main(string[] args)
{
Name a = new Name();
NameDetail ND ;
ND = new NameDetail();
ND.FirstName = "Mohit";
ND.LastName = "Agrawal";
a[0] = ND;
ND = new NameDetail();
ND.FirstName = "aaa";
a[1] = ND;
foreach (NameDetail n in a)
{
Console.WriteLine(n.FirstName );
Console.WriteLine(n.MiddleName);
Console.WriteLine(n.LastName );
Console.WriteLine(n.FullName);
}
Console.ReadKey();
a.Dispose();
a = null;
ND = null;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace Indexer
{
public class NameDetail
{
public string FirstName { get; set; }
public string MiddleName{ get; set; }
public string LastName { get; set; }
public string FullName
{
get
{
String strFullname="";
if (string.IsNullOrEmpty(FirstName) == false )
{
strFullname = FirstName;
}
if (string.IsNullOrEmpty(MiddleName) == false)
{
strFullname += MiddleName;
}
if (string.IsNullOrEmpty(LastName) == false)
{
strFullname += LastName;
}
return strFullname.Trim();
}
}
}
public class Name: IDisposable, IEnumerable, IEnumerator
{
List
public int count { get ;set ;}
private int Position = -1; // This is for Ienum interface
public NameDetail this[int pos]
{
get
{
try
{
return arrPos[pos];
}
catch(Exception ex)
{
throw new Exception(ex.Message);
}
}
set
{
if (arrPos.Count <= pos)
{
arrPos.Insert(pos, value);
count = arrPos.Count;
}
else
{
throw new Exception("No Index postiion");
}
}
}
#region IDisposable
public void Dispose()
{
arrPos = null;
}
#endregion IDisposable
//**********************************************************
//********************* Use For Each in object *************
//*********************************************************
#region IEnumerable Members
public IEnumerator GetEnumerator()
{
return (IEnumerator)this;
}
public bool MoveNext()
{
if (Position < arrPos.Count - 1)
{
++Position;
return true;
}
return false;
}
public void Reset()
{
Position =-1;
}
public object Current
{
get
{
return arrPos[Position];
}
}
#endregion
}
class Program
{
static void Main(string[] args)
{
Name a = new Name();
NameDetail ND ;
ND = new NameDetail();
ND.FirstName = "Mohit";
ND.LastName = "Agrawal";
a[0] = ND;
ND = new NameDetail();
ND.FirstName = "aaa";
a[1] = ND;
foreach (NameDetail n in a)
{
Console.WriteLine(n.FirstName );
Console.WriteLine(n.MiddleName);
Console.WriteLine(n.LastName );
Console.WriteLine(n.FullName);
}
Console.ReadKey();
a.Dispose();
a = null;
ND = null;
}
}
}
Subscribe to:
Posts (Atom)
Hash Table in C#
The Hashtable is a non-generic collection and used to store the key/value pairs based on the hash code of the key. Key will be used to acce...
-
Html.TextBoxFor in MVC 2 doesn't allow the setting of the value property using the object html attributes. syntax - <%= Html.Text...
-
Dictionary<string, string> DicSearch; DicSearch = new Dictionary<string, string>(); DicSearch.Add("1", "Mohi...
-
e.preventDefault() is the key method, to disable the default operations to proceed further. We first stop the event from further bubbli...
-
****************** Type 1 ********************* System.Nullable<DateTime> dt; dt = null; ****************** Type 2 **************...