Archive for the ‘Code Snippets’ Category

Enable/Disable foreign key constraints in SQL Server

– disable all constraints 
EXEC sp_msforeachtable "ALTER TABLE ? NOCHECK CONSTRAINT all"

– enable all constraints 
EXEC sp_msforeachtable @command1="print ‘?’", @command2="ALTER TABLE ? WITH CHECK CHECK CONSTRAINT all"

Original link here.
VN:F [1.1.6_502]Rating: 0.0/5 (0 votes cast)

Leave a Comment

C# Get Copyright information from current application using Reflection

Here is the code snippet:

private string GetCopyright()
{
Assembly asm = Assembly.GetExecutingAssembly();
object[] obj = asm.GetCustomAttributes(false);
foreach (object o in obj)
{
if (o.GetType() ==
typeof(System.Reflection.AssemblyCopyrightAttribute))
{
[...]

Leave a Comment

Show jquery imprompu popup from ASP.NET-AJAX

Simple

ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "message",
"$.prompt(’Are you sure?’)", true);

VN:F [1.1.6_502]Rating: 0.0/5 (0 votes cast)

Leave a Comment

WinForms – Print using printDialog and printDocument

Code snippet here:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
public Form1()
{
[...]

Leave a Comment

Filter Listbox based on Textbox entry

Here is the C# code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
DataSet ds = new DataSet();
public Form1()
[...]

Leave a Comment

Sum of even valued numbers in Fibonacci series

Up to 4 million.

int f1 = 0;
int f2 = 1;
int temp = 0;
int sum = 0;
for (int i = 0; sum < 4000000;i++)
{
temp = f1 + f2;
f1 = f2;
f2 = temp;
if (f2 % 2 == 0)
[...]

Leave a Comment

Sum of multiples of 5 OR 3

 
int sum = 0;
for(int i=0;i<1000;i++)
 
{
if ((i % 3 == 0) || (i % 5 == 0))
{
sum += i;
}
}
 
textBox1.Text = sum.ToString();

VN:F [1.1.6_502]Rating: 0.0/5 (0 votes cast)

Leave a Comment

How to print a WPF form

PrintDialog pd = new PrintDialog();
pd.ShowDialog();
pd.PrintVisual(this, "wpf printing");

VN:F [1.1.6_502]Rating: 0.0/5 (0 votes cast)

Leave a Comment

Load user controls dynamically with ASP.NET

ctrl_homegadget ctrl
= (ctrl_homegadget) LoadControl("ctrl_homegadget.ascx");
ctrl.Border = false;
ctrl.Title = "Hello";
pnlGedgets.Controls.Add(ctrl);

VN:F [1.1.6_502]Rating: 5.0/5 (1 vote cast)

Leave a Comment

Manipulate TFS from VB.NET – a starter

Here is a quick script which demonstrates how to connect to TFS and get projects. Make sure you add reference to necessary dlls - Microsoft.TeamFoundation.Client and Microsoft.TeamFoundation.WorkItemTracking.Client

Imports Microsoft.TeamFoundation.Client
Imports Microsoft.TeamFoundation.WorkItemTracking.Client
Imports System.Net
 
Public Class Form1
Dim nc As NetworkCredential = New NetworkCredential("user", "pass")
Dim tfsurl As New Uri("http://192.168.0.2:8080")
Dim [...]

Leave a Comment