Sunday, December 18, 2011

C# Ping an IP Address

1:  Ping ps = new Ping();  
2:  IPAddress address = IPAddress.Parse("173.201.96.128");  
3:  PingReply reply = ps.Send(address);  
4:  if (reply.Status == IPStatus.Success)  
5:  {  
6:    MessageBox.Show("Online");  
7:  }  
8:  else  
9:  {  
10:    MessageBox.Show(reply.Status.ToString());  
11:  }  

Tuesday, December 6, 2011

C# ListViews

Creating the view, adding grid lines and columns..
listView1.View = View.Details;
listView1.GridLines = true;

listView1.Columns.Add("ID");
listView1.Columns.Add("First Name", 90);
listView1.Columns.Add("Last Name", 80);
listView1.Columns.Add("DOB");
 After adding columns you can add data..
ListViewItem item = new ListViewItem();
item.Text = "0";
item.SubItems.Add("John");
item.SubItems.Add("Doe");
item.SubItems.Add("12/14/81");

listView1.Items.Add(item);
 Now you can set a full row select, a popular feature..
listView1.FullRowSelect = true;

C# HTTP Post

1:  using System.Net;  
2:  using System.IO;  
3:  ...  
4:  ...  
5:  public string HTTPPost(string uri, string parameters)  
6:  {  
7:    WebRequest webRequest = WebRequest.Create(uri);  
8:    webRequest.ContentType = "application/x-www-form-urlencoded";  
9:    webRequest.Method = "POST";  
10:    byte[] bytes = Encoding.ASCII.GetBytes(parameters);  
11:    Stream os = null;  
12:    try  
13:    {  
14:      webRequest.ContentLength = bytes.Length;  
15:      os = webRequest.GetRequestStream();  
16:      os.Write(bytes, 0, bytes.Length);  
17:    }  
18:    catch (WebException ex)  
19:    {  
20:     MessageBox.Show(ex.Message, "Request error");  
21:    }  
22:    finally  
23:    {  
24:      if (os != null)  
25:      {  
26:        os.Close();  
27:      }  
28:    }  
29:    try  
30:    {  
31:      WebResponse webResponse = webRequest.GetResponse();  
32:      if (webResponse == null) return null;  
33:      StreamReader sr = new StreamReader(webResponse.GetResponseStream());  
34:      return sr.ReadToEnd().Trim();  
35:    }  
36:    catch (WebException ex)  
37:    {  
38:      MessageBox.Show(ex.Message, "Response error");  
39:    }  
40:    return null;  
41:  }  

C# DataGridView Tutorial

Add a new DataGridView object to your form then..

To add new columns to hold our data..
dataGridView1.Columns.Add("id","ID");
dataGridView1.Columns.Add("fname","First Name");
dataGridView1.Columns.Add("lname","Last Name");
dataGridView1.Columns.Add("dob","DOB");
 You can add data simply by..
int i = dataGridView1.Rows.Add();
dataGridView1.Rows[i].Cells[0].Value = "0";
dataGridView1.Rows[i].Cells[1].Value = "John";
dataGridView1.Rows[i].Cells[2].Value = "Doe";
dataGridView1.Rows[i].Cells[3].Value = "12/14/82";

Loop through the data by..
try
{
    foreach (DataGridViewRow dr in dataGridView1.Rows)
    {
        MessageBox.Show(dr.Cells[0].Value.ToString());
    }
}
catch { };

Reading it from a file, comma separated..
if (!File.Exists("customers.txt"))
{
    MessageBox.Show("customers file not found");
    return;
}

StreamReader rdr = new StreamReader(@"customers.txt");
string[] lines = rdr.ReadToEnd().Split('\n');
foreach (string line in lines)
{
    string[] info = line.Split(',');

    int i = dataGridView1.Rows.Add();
    dataGridView1.Rows[i].Cells[0].Value = info[0];
    dataGridView1.Rows[i].Cells[1].Value = info[1];
    dataGridView1.Rows[i].Cells[2].Value = info[2];
    dataGridView1.Rows[i].Cells[3].Value = info[3];
}