SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["SchoolsConnectionString"].ConnectionString);
DataSet ds = new DataSet();
SqlDataAdapter adp = new SqlDataAdapter("select * from students", con);
Dataset ds = new DataSet();
adp.Fill(ds);
string message;
ds.Tables[0].DefaultView.RowFilter = "Name='Subhash'";
if (ds.Tables[0].DefaultView.Count != 0)
{
message = ds.Tables[0].DefaultView[0].Row["LastName"].ToString();
}
else
{
message = "";
}
Tuesday, January 18, 2011
Log4Net(for Making logger file)
//This is the link where you can read about log4Net
//open source provide by Microsoft
http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx
http://www.beefycode.com/post/Log4Net-Tutorial-pt-1-Getting-Started.aspx
http://shiman.wordpress.com/2008/07/09/how-to-log-in-c-net-with-log4net-a-tutorial/Here is
great example for sending logfile into mail......
use the logger to log a few statements with various severity levels. log4net defines 5 such levels:
Log4Net supports five levels of logs with five methods.
Debug: fine-grained statements concerning program state, typically used for debugging;
Info: informational statements concerning program state, representing program events or behavior tracking;
Warn: statements that describe potentially harmful events or states in the program;
Error: statements that describe non-fatal errors in the application; this level is used quite often for logging handled exceptions;
Fatal: statements representing the most severe of error conditions, assumedly resulting in program termination.
we have to add Log4Net Dll in Add reference section.
after that Inside the AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
and add some code in web.config file..
section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false"/>
log4net debug="true">
appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
file value="Log4net\\log4net.txt"/>
appendToFile value="true"/>
rollingStyle value="Size"/>
maxSizeRollBackups value="10"/>
maximumFileSize value="50KB"/>
staticLogFileName value="true"/>
lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
layout type="log4net.Layout.PatternLayout">
conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n"/>
/layout>
/appender>
root>
level value="ALL"/>
appender-ref ref="RollingLogFileAppender"/>
/root>
/log4net>
add key="log4net.Internal.Debug" value="true" />
/appSettings>
//////////and Inside the .cs page
using log4net;//Add class in page
//Define Globally
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("_Default");
//use like this
protected void Page_Load(object sender, EventArgs e)
{
try
{
int a = 0;
int b = 1;
int c = b / a;
}
catch (Exception ex)
{
log.Error(ex.Message, ex);//here is using
}
}
//open source provide by Microsoft
http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx
http://www.beefycode.com/post/Log4Net-Tutorial-pt-1-Getting-Started.aspx
http://shiman.wordpress.com/2008/07/09/how-to-log-in-c-net-with-log4net-a-tutorial/Here is
great example for sending logfile into mail......
use the logger to log a few statements with various severity levels. log4net defines 5 such levels:
Log4Net supports five levels of logs with five methods.
Debug: fine-grained statements concerning program state, typically used for debugging;
Info: informational statements concerning program state, representing program events or behavior tracking;
Warn: statements that describe potentially harmful events or states in the program;
Error: statements that describe non-fatal errors in the application; this level is used quite often for logging handled exceptions;
Fatal: statements representing the most severe of error conditions, assumedly resulting in program termination.
we have to add Log4Net Dll in Add reference section.
after that Inside the AssemblyInfo.cs
[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Web.config", Watch = true)]
and add some code in web.config file..
section name="log4net" type="log4net.Config.Log4NetConfigurationSectionHandler,Log4net" requirePermission="false"/>
log4net debug="true">
appender name="RollingLogFileAppender" type="log4net.Appender.RollingFileAppender">
file value="Log4net\\log4net.txt"/>
appendToFile value="true"/>
rollingStyle value="Size"/>
maxSizeRollBackups value="10"/>
maximumFileSize value="50KB"/>
staticLogFileName value="true"/>
lockingModel type="log4net.Appender.FileAppender+MinimalLock"/>
layout type="log4net.Layout.PatternLayout">
conversionPattern value="%-5p %d %5rms %-22.22c{1} %-18.18M - %m%n"/>
/layout>
/appender>
root>
level value="ALL"/>
appender-ref ref="RollingLogFileAppender"/>
/root>
/log4net>
add key="log4net.Internal.Debug" value="true" />
/appSettings>
//////////and Inside the .cs page
using log4net;//Add class in page
//Define Globally
private static readonly log4net.ILog log = log4net.LogManager.GetLogger("_Default");
//use like this
protected void Page_Load(object sender, EventArgs e)
{
try
{
int a = 0;
int b = 1;
int c = b / a;
}
catch (Exception ex)
{
log.Error(ex.Message, ex);//here is using
}
}
Labels:
Log4Net(for Making logger file)
Friday, January 14, 2011
Compute with DataTable
DataTable Dtable=new DataTable();
DataColumn DColumn=new DataColumn("Salary",typeof(float));
DTable.Column.Add(DColumn);
DataRow DRow=DTable.NewRow();
DRow[0]=25000;
DTable.Rows.Add(DRow);
DRow=DTable.NewRow();
DRow[0]=30000;
DTable.Rows.Add(DRow);
//For Doolar Formate
string u= string.Format("{0:C}", ds.Tables[0].Compute("sum(Salary)", ""));
string u = string.Format("{0:0,000,000}", ds.Tables[0].Compute("sum(Salary)", ""));
//for Indian Rupee formate
string u = string.Format("{0:#,#.}", ds.Tables[0].Compute("sum(Salary)", ""));
Console.WriteLine(TotalSalary);
DataColumn DColumn=new DataColumn("Salary",typeof(float));
DTable.Column.Add(DColumn);
DataRow DRow=DTable.NewRow();
DRow[0]=25000;
DTable.Rows.Add(DRow);
DRow=DTable.NewRow();
DRow[0]=30000;
DTable.Rows.Add(DRow);
//For Doolar Formate
string u= string.Format("{0:C}", ds.Tables[0].Compute("sum(Salary)", ""));
string u = string.Format("{0:0,000,000}", ds.Tables[0].Compute("sum(Salary)", ""));
//for Indian Rupee formate
string u = string.Format("{0:#,#.}", ds.Tables[0].Compute("sum(Salary)", ""));
Console.WriteLine(TotalSalary);
Labels:
Compute with DataTable
Thursday, January 13, 2011
C# IndexOf String Examples
IndexOf methods:
First, here we note that there are four IndexOf instance methods. The first two methods find the first indexes. They scan through the characters from the left to right. The second two find the last indexes, and they go from right to left.
IndexOf:This method finds the first index of the char argument. It returns -1 if the char was not found.
IndexOfAny:This method finds the first index of any of the char arguments. It returns -1 if none are found.
LastIndexOf:This finds the last index of the char argument. It returns -1 if the char was not found.
LastIndexOfAny:This finds the first index of any of the char arguments. It returns -1 if none are found.
Example:
string Path: C:\Documents and Settings\Jitendra\My Documents\Visual Studio 2008\Projects\WebApplication5\WebApplication5\
string k = stem.Web.HttpContext.Current.Request.PhysicalApplicationPath;
int o ;
k = k.Remove(k.LastIndexOf("\\"));
o = k.LastIndexOf("\\");
string appName1 = k.Substring(o+1);
Example:- string strFileName;
string strFile = strFileName.Substring(strFileName.LastIndexOf("\\")+1);
First, here we note that there are four IndexOf instance methods. The first two methods find the first indexes. They scan through the characters from the left to right. The second two find the last indexes, and they go from right to left.
IndexOf:This method finds the first index of the char argument. It returns -1 if the char was not found.
IndexOfAny:This method finds the first index of any of the char arguments. It returns -1 if none are found.
LastIndexOf:This finds the last index of the char argument. It returns -1 if the char was not found.
LastIndexOfAny:This finds the first index of any of the char arguments. It returns -1 if none are found.
Example:
string Path: C:\Documents and Settings\Jitendra\My Documents\Visual Studio 2008\Projects\WebApplication5\WebApplication5\
string k = stem.Web.HttpContext.Current.Request.PhysicalApplicationPath;
int o ;
k = k.Remove(k.LastIndexOf("\\"));
o = k.LastIndexOf("\\");
string appName1 = k.Substring(o+1);
Example:- string strFileName;
string strFile = strFileName.Substring(strFileName.LastIndexOf("\\")+1);
Labels:
C# IndexOf String Examples
Wednesday, January 12, 2011
Special Character check through javascript
//The IndexOf method can be used to check if one character is inside of another. For example, suppose you want to check an email address to see if it contains the @ character. If it doesn't you can tell the user that it's an invalid email address.
script type="text/javascript" language ="javascript" >
function checkSpecialCharacters() {
var str = "'!#$%^&;*()";
var txtUserId = document.getElementById('txtUserId').value;
var txtPassword = document.getElementById('txtPassword').value;
var txt = txtUserId + txtPassword;
for (var i = 0; i < str.length; i++) { var char1 = str.substr(i, 1) if (txt.indexOf(char1) > -1) {
alert('special character(s) are not allowed in loginid/password');
//alert(obj.getMessage(196));
return false;
}
}
}
/script>
script type="text/javascript" language ="javascript" >
function checkSpecialCharacters() {
var str = "'!#$%^&;*()";
var txtUserId = document.getElementById('txtUserId').value;
var txtPassword = document.getElementById('txtPassword').value;
var txt = txtUserId + txtPassword;
for (var i = 0; i < str.length; i++) { var char1 = str.substr(i, 1) if (txt.indexOf(char1) > -1) {
alert('special character(s) are not allowed in loginid/password');
//alert(obj.getMessage(196));
return false;
}
}
}
/script>
Tuesday, January 11, 2011
DefaultView
//DefaultView:filter a limited number of columns from a table:
//Distinct Data in DataTable with the Field of roleid,roleName,abbreviation........
DataSet infoDS=new DataSet();
DataTable dTable = infoDS.Tables[0].DefaultView.ToTable(true, "roleid", "roleName", "abbreviation");
for (int i = 0; i < dTable.Rows.Count; i++) { if (dTable.Rows.Count > 0)
{
string _RoleId = dTable.Rows[i]["roleid"].ToString();
string _RoleName = dTable.Rows[i]["roleName"].ToString();
string _Abbreviation = dTable.Rows[i]["abbreviation"].ToString();
}
}
//Distinct Data in DataTable with the Field of roleid,roleName,abbreviation........
DataSet infoDS=new DataSet();
DataTable dTable = infoDS.Tables[0].DefaultView.ToTable(true, "roleid", "roleName", "abbreviation");
for (int i = 0; i < dTable.Rows.Count; i++) { if (dTable.Rows.Count > 0)
{
string _RoleId = dTable.Rows[i]["roleid"].ToString();
string _RoleName = dTable.Rows[i]["roleName"].ToString();
string _Abbreviation = dTable.Rows[i]["abbreviation"].ToString();
}
}
Compute with DataTable
DataTable Dtable=new DataTable();
DataColumn DColumn=new DataColumn("Salary",typeof(float));
DTable.Column.Add(DColumn);
DataRow DRow=DTable.NewRow();
DRow[0]=25000;
DTable.Rows.Add(DRow);
DRow=DTable.NewRow();
DRow[0]=30000;
DTable.Rows.Add(DRow);
float TotalSalary=(float)DTable.Compute("Sum(Salary)","");
Console.WriteLine(TotalSalary);
DataColumn DColumn=new DataColumn("Salary",typeof(float));
DTable.Column.Add(DColumn);
DataRow DRow=DTable.NewRow();
DRow[0]=25000;
DTable.Rows.Add(DRow);
DRow=DTable.NewRow();
DRow[0]=30000;
DTable.Rows.Add(DRow);
float TotalSalary=(float)DTable.Compute("Sum(Salary)","");
Console.WriteLine(TotalSalary);
Labels:
Compute with DataTable
Subscribe to:
Posts (Atom)