Category: C#

Check to see whether the file in a url Exists or Not

You can use HttpWebRequest. Note four things 1) The webRequest will throw exception if the link doesn’t exist 2) You may like to disable auto redirect 3) You may also like to check if it’s a valid url. If not, it will throw UriFormatException. 4) Per Paige suggested , Use “Head” in request.Method so that […]

Read more

ExecuteNonQuery / ExecuteScalar

Namespaces: System.Data; System.Xml; System.Configuration; // Clear out any test records from previous test executions. using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings[“DB_NAME”].ConnectionString)) { connection.Open(); string _mediaId = string.Empty; string _title = “Title”; using (System.Data.SqlClient.SqlCommand cmd = connection.CreateCommand() as System.Data.SqlClient.SqlCommand) { System.Data.SqlClient.SqlCommand cmd1 = new System.Data.SqlClient.SqlCommand(sql, connection); cmd1.CommandType = CommandType.Text; using (System.Data.SqlClient.SqlDataReader reader = cmd1.ExecuteReader()) { while (reader.Read()) […]

Read more

Extract substring from a string until finds a comma c# asp.net

substring = str.Split(‘,’)[0]; or You can use IndexOf() to find out where is the comma, and then extract the substring. If you are sure it will always have the comma you can skip the check.

or

or

Favorite

Read more

Convert foreach loop to Linq code

Converting this code to Linq: string[] selections = “Men,Women,Boys”.Split(‘,’); int _chkboxId = 0; int _chkboxTextId = 1; try { string id = “lstchk_” + _chkboxId; while (!driver.FindElement(By.Id(id)).Equals(null)) { string checkboxId = String.Format(“lstchk{0}”, _chkboxTextId); string checkboxName = driver.FindElement(By.Id(checkboxId)).Text; foreach (string match in selections) { if (checkboxName == match.Trim()) { //matched… do more work here… } } […]

Read more

Refactoring horrible nested if-else statements

I wrote this at some point this week, I was looking back at it tonight and released how truly awful it looks: if(filePath.Contains(“.CSS”)) return true; else return false; else if(filePath.Contains(“.JS”)) return true; else return false; else if(filePath.Contains(“_STR”)) return true; else return false; else if(filePath.Contains(“.VBS”)) return true; else return false; else if(filePath.Contains(“.HTM”)) return true; else return […]

Read more