Thursday, March 12, 2009

In-Memory Dataset operation (Add/Mod/Del) using C#

DataSet dstestoperation = new DataSet();
DataTable dtTestTab = new DataTable();
DataView dvTest;
private FileInfo fitest = new FileInfo(@"c:\MyXML\PicturePath.xml");
private FileInfo fitestschema = new FileInfo(@"c:\MyXML\PicturePath.xsd");

private void DeleteFromTestDataset()
{
DataRow[] dtTestTabrowcollection = (DataRow[])dtTestTab.Select("PictureEmail = '" + txtEmailid.Text + "'");
if (dtTestTabrowcollection.Length > 0)
{
foreach (DataRow myrow in dtTestTabrowcollection)
{
myrow.Delete();
}
}
getcount();
}
private void AddtoTestDataset()
{
DataRow myrow = dtTestTab.NewRow();
myrow["PictureEntryid"] = "A00";
myrow["PictureEmail"] = txtEmailid.Text;
myrow["PictureName"] = "cartoons";
myrow["PictureIcon"] = @"C:\Documents and Settings\My Documents\My Pictures\swaps_"+DateTime.Now.Minute.ToString() + ".jpg";
myrow["PicturePhone"] = "+111-111-1111";
dtTestTab.Rows.Add(myrow);
getcount();
}
private void ModifyTestDataset()
{
DataRow[] dtTestTabrowcollection = (DataRow[])dtTestTab.Select("PictureEmail = '" + txtEmailid.Text + "'");
if (dtTestTabrowcollection.Length > 0)
{
foreach (DataRow myrow in dtTestTabrowcollection)
{
myrow["PictureName"] = "MyPictures";
}
}
getcount();
}
private int testDuplicate()
{
DataRow[] dtTestTabrowcollection = (DataRow[])dtTestTab.Select("Pictureid = '" + txtid.Text + "'");
return (dtTestTabrowcollection.Length - 1);
}
private int TitleCase()
{
System.Globalization.CultureInfo mycultureinfo = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Globalization.TextInfo mytextinfo = mycultureinfo.TextInfo;
txtEmailid.Text = mytextinfo.ToTitleCase(txtEmailid.Text);
}


private void btnSave_Click(object sender, EventArgs e)
{
dtTestTab.AcceptChanges();
dstestoperation.WriteXml(fitest.FullName, XmlWriteMode.IgnoreSchema);
btnSave.Enabled = false;
}

How to convert strings to title (proper) case by using C#

Convert a String to Title Case

The String class does not include a method that converts a string to title case. The ToTitleCase method resides in the TextInfo class, which is a member of the System.Globalization namespace. Unlike the ToUpper and ToLower methods of the String class, the ToTitleCase method is not a static method and requires an instance of the class.

When you use the TextInfo class, you must specify cultural information. In most situations, you can default to the culture that is currently in use. Culture information is a property of the thread on which the code is running. To obtain the culture information, you must gain access to the current thread and retrieve the CurrentCulture property from that thread. Once you accomplish this, you can create the TextInfo object. For example:

CultureInfo cultureInfo = Thread.CurrentThread.CurrentCulture;
TextInfo textInfo = cultureInfo.TextInfo;



The TextInfo class also includes the ToUpper and ToLower methods. Use these methods of TextInfo if you need to specify culture options.

Console.WriteLine(textInfo.ToTitleCase(title));
Console.WriteLine(textInfo.ToLower(title));
Console.WriteLine(textInfo.ToUpper(title));


If you need to create or manipulate strings that have specific culture settings, you can use one of the overloaded constructors of the TextInfo class to create strings with any of the available culture options.

Courtesy: http://support.microsoft.com/kb/312890

Saturday, March 7, 2009

How can we check that our internet connection is working or not.

private void Form1_Load(object sender, EventArgs e)
{
try
{
this.testconnection();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}

private void testconnection()
{
if (this.IsConnectionAvailable())
MessageBox.Show("Internet available");
else
MessageBox.Show("No Internet");
}

public bool IsConnectionAvailable()
{
System.Net.WebRequest objWebReq = System.Net.WebRequest.Create("http://www.microsoft.com/");
System.Net.WebResponse objResp = default(System.Net.WebResponse);
try
{
objResp = objWebReq.GetResponse();
objResp.Close();
objWebReq = null;
IsConnected = true;
return true;
}

catch (Exception ex)
{
objResp = null;
objWebReq = null;
IsConnected = false;
return false;
}
}

Courtesy: http://www.devasp.net/net/articles/display/639.html
-----------------------------------------------------------------------------------
private bool CheckInternetAvailability()
{
try
{
System.Net.IPHostEntry ipaddress = System.Net.Dns.GetHostByName("www.google.com");
return true;
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
return false;
}
}

Courtesy: http://www.csharpfriends.com/Forums/ShowPost.aspx?PostID=13045