5 Tips & Tricks in ASP.NET
Submitted by planetsourcecode on Monday, June 15, 2009 - 00:01.
Trick #1 Delete a selected row from a DataGrid using c#
Double click the dataGrid and write the code
private void DataGridvie_selected(Object sender,EventArgs e)
{
DataSet da=new DataSet();
int i=datagrid1.selectedIndex;
int empidval=ds.Tables["emp"].Rows[i][0].ToString();
SqlConnection conn=new Sqlconnection("conn string");
conn.Open();
SqlCommand cmd=new SqlCommand("delect emp where empid="+empid+"",conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Row Deleted");
}
Trick #1 How to Validate DataType only with a CompareValidator
Let's say you have a textbox in which you need to make sure the user puts in a valid date. You can actually use a CompareValidator Control for this - - it's so simple.
All you need to do is add the ControlToValidate property (the control/textbox in which a date must be entered), the erroressage you need (ErrorMessage property), the Operator property(change to DataTypeCheck), and the Type property (choose Date).
Trick #3 Difference between two dates in C#
public int GetTotalDays(DateTime startedDateTime)
{
//get the current date
DateTime nowDate = System.DateTime.Now;
System.TimeSpan span = nowDate - startedDateTime;
int days = (int)span.TotalDays;
return days;
}
Trick #4 Difference between Convert.ToInt32(string) and Int32.Parse
Convert.ToInt32(string) and Int32.Parse(string) yield identical results except when the string is actually a null. In this case, Int32.Parse(null) throws an ArgumentNullException; but, Convert.ToInt32(null) returns a zero. So it is better to use Int32.Parse(string)
Trick #5 Display Particular Format for Phone Number
Let's say your database stores phone numbers without the dashes or parentheses, but you would like to display it like that, on the web page.
To do this, store the number as an integer, and then, the following code snippet will do the trick:
Dim s as integer="1234567891"
label1.text=string.Format("{0:(000)000-0000}",s)
wait for much more tips and tricks
About the author:
PlanetSourceCode.in is a place for all developer providing free source codes, articles, complete projects,complete application in PHP, C/C++, Javascript, Visual Basic, Cobol, Pascal, ASP/VBScript, AJAX, SQL, Perl, Python, Ruby, Mobile Development
Comments
Add new comment
- Add new comment
- 13 views