SQL Delete From Table With Constraints.
--Make the System ignore the constraint
ALTER TABLE PK_ConstraintsTableName NOCHECK CONSTRAINT ALL
--Now Delete
DELETE
FROM TableName
WHERE WhereClause
--Put the contraints back
ALTER TABLE PK_ConstraintsTableName CHECK CONSTRAINT ALL
May 28th, 2015
SQL - Using Case Statement To Update A Table:
CREATE TABLE #t1 (ID INT, name VARCHAR(10), val1 INT, val2 INT)
INSERT #t1 (ID , name , val1 , val2 )
SELECT 1, 'aaa01', 10, 100
UNION ALL
SELECT 2, 'bbb01', 20, 200
UNION ALL
SELECT 3, 'aaa02', 30, 300
UNION ALL
SELECT 4, 'bbb02', 40, 400
S
SQL JOINS Examples
Here is an example of what different join produce in SQL.
SQL Complex Grouping using group substring
SQL Complex Grouping using group sub-string.
C# csv parser CSVParser
The Comma Separated Value (CSV) file contains data that (as the name suggests) are separated by commas. In C# they can easily be split into an array by using Data.Split(','). The problem occurs when data contains a comma.
2C# String Wrapper Function
For a project I needed to create a string wrapper function.
The wrapper function had to follow these rules.
1: Each line has a prefix.
2: Each line may not exceed 80 charachters, including the prefix.
C# Copy From Text File To DataTable
private DataTable ReadAsDataTable(string file, bool hasHeader, char seperator)
{
DataTable dt = new DataTable();
StreamReader reader = new StreamReader(file);
string line = string.Empty;
string[] data = null;
int counter = 0;
while (reader.Peek() != -1)
{
line = reader.ReadLine();
data = l
C# RegEx Validator
//A simple RegEx validotor.
C# How To Add Or Subtract Days Months Years From Date
private DateTime GetDate(DateTime inputDate, string differenceType, bool add, int difference)
{
DateTime result = inputDate;
if (!add)
{
//Substraction
difference = -1 * difference;
}
switch (differenceType.ToUpper())
{
case "DAYS":
result = new DateTime(inputDate.Year, inputDate.Month, i
c# - How to create images thumbnail control using picturebox
private void AddImageToThumbnail(string[] images)
{
int top = 0;
int ht = 150;
int counter = 0;
PictureBox[] pictures = new PictureBox[images.Length];
if (images.Length > 0)
{
//The panel pnlThumbnail will hold thumbnail pictures.
C# Get All Files In A Directory
public string[] GetAllFilesInDirectory(string path, string pattern = "*.*", SearchOption options = SearchOption.TopDirectoryOnly)
{
if (path.Trim().Length == 0)
return null; //Error handler can go here
if ((pattern.Trim().Length == 0) || (pattern.Substring(pattern.Length - 1) == "."))
return nu
C# Find Most Recent File In Directory
public string GetLastFileInDirectory(string directory, string pattern = "*.*")
{
if (directory.Trim().Length == 0)
return string.Empty; //Error handler can go here
if ((pattern.Trim().Length == 0) || (pattern.Substring(pattern.Length - 1) == "."))
return string.Empty; //Error handler can go her
C# XML To SQL Stored Procedure
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.IO;
using System.Xml.Serialization;
//using System.Data;
using System.Data.SqlClient;
namespace zSharp2010.XMLToDB
{
public class Employee
{
public int ID { get; set; }
C# Serialization/DeSerialization
Let me first define the Person Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace zSharp2010.Serializer
{
[Serializable()]
public class Person
{
public int ID { get; set; }
public string Name { get; set; }
public DateTime DOB { get; set;
SQL - Check For Not Null Field
SELECT *
FROM TableName
WHERE NOT (FieldName IS NULL)
C# - VB6 Equivalent of Err.Number
In VB6 you may catch err as:
Private Sub DivideByZero()
On Error GoTo Err_Handler
Dim num As Integer
Dim zero As Integer
num = 100 / zero
Exit_Eop:
Exit Sub
Err_Handler:
MsgBox ("Error Code : " & Err.number & vbCrLf & "Error Message : " & Err.Description)
End Sub
In C# the equivalent woul
C# Using App.config file.
Reading from App.config file:
using System.Configuration;
To read from app.config file you may use the following code:
private string ReadFromAppConfig(string key)
{
return ConfigurationManager.AppSettings[key];
}
To write to app.config file you may use the following code:
private void Write
SQL C# Procedure expects parameter which was not supplied
Don't forget to put:
cmd.CommandType = CommandType.StoredProcedure;
Error: COMException Was unhandled. Retireving the COM class factory for component with ‘xxx’ failed due to the following error: 80040154
I received the following error while using a sample application from a third party client.
The process cannot access the file because it is being used by another process.
The process cannot access the file 'c:\temp\Query.txt' because it is being used by another process.
Try the following code, however be very careful since you could end up reading the contents of the same file.
C# ? Operative
booleanExpression ? trueValue : falseValue;
Example: string itemText = count > 1 ? "items" : "item";
C# - VB Equivalent Of IIF
booleanExpression ? trueValue : falseValue;
Example: string itemText = count > 1 ? "items" : "item";
Error while trying to run project: Unable to start debugging. The Microsoft Visual Studio Remove Debugging Monitor has been closed on the remote machine
Using VS2008, I occasionally get
“Error while trying to run project: Unable to start debugging.
The Microsoft Visual Studio Remove Debugging Monitor has been closed on the remote machine”.
To solve this problem:
(i) Close the Visual Studio.
Convert Octal To Decimal In C#
public int OctToDecimal(string data)
{
int result = 0;
char[] numbers = data.ToCharArray();
int ASCIIZero = 48;
int ASCIIEight = 56;
try
{
if (!IsNumeric(data))
error = "Invalid Value - This is not a numeric value";
else
{
for (int counter = numbers.Length; counter > 0; counter--)
{
by
Convert Decimal To Octal In C#
public string DecimalToOct(string data)
{
string result = string.Empty;
int rem = 0;
try
{
if (!IsNumeric(data))
error = "Invalid Value - This is not a numeric value";
else
{
int num = int.Parse(data);
while (num > 0)
{
rem = num % 8;
num = num / 8;
result = rem.ToString() + result;
SQL - Copy a row from same table
DECLARE @tblName VarChar(50) @tblName VarChar(50)
DECLARE @columns VarChar(5000) @columns VarChar(5000)
DECLARE @query VarChar(8000) @query VarChar(8000)
DECLARE @id int @id int
--Initialize the table and id.
Convert Decimal To Binary In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace zSharp2008.zMath
{
class cMath
{
string error = string.Empty;
public string Error
{
get
{
return error;
}
}
public string DecimalToBinary(string data)
{
string result = string.Empty;
in
Convert Binary To Decimal In C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace zSharp2008.zMath
{
class cMath
{
string error = string.Empty;
public string Error
{
get
{
return error;
}
}
public int BinaryToDecimal(string data)
{
int result = 0;
char[] numbers = da
Using Custom Class in C# Dictionary
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace zSharp2010.Dictionary
{
class cDictionary
{
//Create the dictionary.
Check For Palindrome
public bool IsPalindrome(string stringToCheck)
{
char[] rev = stringToCheck.Reverse().ToArray();
return (stringToCheck.Equals(new string(rev), StringComparison.OrdinalIgnoreCase));
}
SQL Merge two rows based on two tables.
Lets say we have two tables
Property Table:
And user table:
We need results in the format:
DECLARE @tblProperties table (PropID int, Property varchar(20), Value varchar(20))
DECLARE @tblUsers table (UserID int, Name varchar(20), PropID Int)
INSERT @tblProperties (PropID, Property, Value)
SEL
Insert multiple rows in a table.
--Creat a table containing some words.
Remove unwanted words from a table.
--USE cursor to remove invalid words from a table.
--Creat a table containing bad words
DECLARE @tblWordsToAvoidList table (sInvalidWords varchar(255))
INSERT @tblWordsToAvoidList (sInvalidWords)
SELECT ( 'aaa')
UNION ALL
SELECT ( 'eee')
--Creat a table containing some words.
The type or namespace name '??????' does not exist in the namespace
If you are getting this error check the following:
1: Right click on References
2: Add the reference to the assembly that is missing (this might be another project that you have).
OCR Using MS-Office
Simplest code to OCR an image using Microsoft Office's Imaging functionality (requires MS-Office 2007 or later, imaging components must be installed and MODI must be added to references).
7Creating An Access DataBase Programatically In C#
using ADOX; //Requires Microsoft ADO Ext. 2.8 for DDL and Security
using ADODB;
public bool CreateNewAccessDatabase(string fileName)
{
bool result = false;
ADOX.Catalog cat = new ADOX.Catalog();
ADOX.Table table = new ADOX.Table();
//Create the table and it's fields.
ADD A New Column with default values filled
Use [DataBase]
ALTER TABLE Schema.TableName
ADD FieldName Bit NOT NULL DEFAULT 0 WITH VALUES
C# IsDate Function
public bool IsDate(object value)
{
bool result = false;
string strDate = value.ToString();
try
{
DateTime dt = DateTime.Parse(strDate);
if(dt != DateTime.MinValue && dt != DateTime.MaxValue)
result = true;
}
catch (Exception ex)
{
result = false;
}
return result;
}
C# IsNumeric Function
public bool IsNumeric(string value)
{
bool result = false;
try
{
Convert.ToInt64(value);
result = true;
}
catch (Exception ex)
{
result = false;
}
return result;
}
VB Like InputBox in C#
To implement VB like Input Box in C# you may use the following code:
________________________________________________________________________
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.
Find the first and last date of a given week, month, quarter or year. (A C# Algorithm)
For a project I needed to figure the dates of first and last day of the week, month, quarter
and year for a given date.
Encode
A simple encoding method. Uses ASCII encoding.
VB.NET - StringBuilder
Strings are immutable - meaning the space allocated in memory is fixed. If two strings are concatenated, .net internally creates another string. The string builder is muatble, meaning the space allocated in memory is dynamic.
If two stringbuilders are concatenated, no new object is created.
VB.NET - Constructs
Constructs: Is a method that runs when a new instance at class is created. It is called Sub New. Sub New may have arguments. Constructs may be overloaded.
Events: Different from VB6.
Class ClassName
Event EventName ( .. )
RaiseEvent EventName ( ..
View comments