Skip to content

Category: Coding

Simple Linq to SQL Insert and Update

private void InsertAndUpdate()
{
  NorthwindDataContext db = new NorthwindDataContext();

  //Insert a record

  Customer newCus = new Customer();
  newCus.CustomerID = "YYYZZ";
  newCus.CompanyName = "Company_Z";

  db.Customers.InsertOnSubmit(newCus);
  db.SubmitChanges();      

  //Update a record
  Customer record = (from p in db.Customers
         where p.CustomerID == "12345" 
         select p).SingleOrDefault();

  Console.WriteLine(default(Customer));
  if (record != default(Customer))
  {
    record.CompanyName = "Company_A";
  }

  db.SubmitChanges();
}

 

Leave a Comment

C# List to DataTable

Don’t recall where I found this but converts List to DataTable.

public static System.Data.DataTable ToDataTable<T>(this IList<T> data)
        {
            System.ComponentModel.PropertyDescriptorCollection props = System.ComponentModel.TypeDescriptor.GetProperties(typeof(T));
            System.Data.DataTable table = new System.Data.DataTable();
            for (int i = 0; i < props.Count; i++)
            {
                System.ComponentModel.PropertyDescriptor prop = props[i];
                table.Columns.Add(prop.Name);
                //table.Columns.Add(prop.Name, prop.PropertyType);
            }
            object[] values = new object[props.Count];
            foreach (T item in data)
            {
                for (int i = 0; i < values.Length; i++)
                {
                    values[i] = props[i].GetValue(item);
                }
                table.Rows.Add(values);
            }
            return table;
        }

 

Leave a Comment

Using C# to check if string contains a string in string array

Pieced this together from stackoverflow… http://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array/2912541#2912541

string stringToCheck = "text1";
string[] stringArray = { "text1", "testtest", "test1test2", "test2text1" };

This checks if stringToCheck contains any one of substrings from stringArray.

if(stringArray.Any(stringToCheck.Contains))

If you want to ensure that it contains all the substrings, change Any to All:

if(stringArray.All(s => stringToCheck.Contains(s)))

 

Leave a Comment

Regex for U.S. and Canadian Zip Codes

Below is the regex code for U.S. and Canadian zip codes. I’m don’t remember where I found these but hopefully someone finds them useful.

string _usZipRegEx = @"^\d{5}(?:[-\s]\d{4})?$";
string _caZipRegEx = @"^([ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ])\ {0,1}(\d[ABCEGHJKLMNPRSTVWXYZ]\d)$";
Leave a Comment

BugAid is an extension for Visual Studio

This looks like a promising plugin for Visual Studio. It’s already helped me search for text in a large! object. I also used its comparison features. So far so good. Just need to make the decision whether its worth $49.

You can visit the website here for more information and for the 60 day trial: http://www.bugaidsoftware.com/

BugAid is an extension for Visual Studio that can help you whenever you debug C# code. Using our unique features you can debug faster than ever before and have more time for writing quality code!

 

Leave a Comment

Redirect to HTTPS – Using URL Rewrite

I needed to redirect a HTTP website to HTTPS. I thought it was part of IIS, and I think it was at some point. Maybe in IIS 7.0.

But here is the code to redirect to HTTPS. I grabbed it from here http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/.

You will also need to install the URL Rewrite module located here: http://www.iis.net/download/urlrewrite.

<rule name="Redirect to HTTPS" stopProcessing="true">  
  <match url="(.*)" />  
  <conditions>  
    <add input="{HTTPS}" pattern="^OFF$" />  
  </conditions>  
  <action type="Redirect" url="https://{HTTP_HOST}/{R:1}" redirectType="Permanent" />  
</rule>
Leave a Comment