/* ------------------------ My Meta Content Here SEO ------------------------ */

Pages

Main Menu

Tuesday, July 17, 2012

Sql Server 2005: Change schema for all tables

Recently I have needed to change the schema for all the tables in a Sql Server 2005 database.

Sql Server 2005 provides a T-SQl statement capable to change the schema for a given object called ALTER SCHEMA 

You may use the following syntax to change the schema for all the tables in a database. Just change the "new_schema" string with the desired one:

exec sp_MSforeachtable "ALTER SCHEMA new_schema TRANSFER ? PRINT '? modified' "

Enjoy the Script!
Read More »

Wednesday, June 20, 2012

Page Life Cycle in ASP.NET

Read More »

Friday, April 6, 2012

Disable and Restrict Copy Cut and Paste from on a textbox


You will need two javascript functions for this:

Read More »

Monday, April 2, 2012

Write Read AppSettings Connection String From Web.Config Asp.Net

Most of the time in Asp.Net web or windows forms application we need to connect to Sql Server database, and for connecting we need to Write Or Read connection string from Web.Config Or App.Config file In Asp.Net.

This post explains How to Write Or Read this Information From configuration file for Web Applications or Winforms.

We can either use connectionStrings section or AppSettings and can Modify Programmatically At Run Time as well.

Sql Server information is usually written inside

   1:  <configuration>
   2:  <connectionStrings>
   3:   
   4:  <add name="MyCon" 
   5:  connectionString="Data Source=AMITJAIN\SQL;
   6:  Initial Catalog=Northwind;User ID=amit;Password=password"
   7:  providerName="System.Data.SqlClient" />
   8:   
   9:  connectionStrings>
  10:  configuration>

Where Data Source is db Server Address, Initial Catalog is DataBase Name
To use Windows Authentication instead of Sql Server Authentication write
   1:  <connectionStrings>
   2:   
   3:  <add name="MyCon" 
   4:  connectionString="Data Source=AMITJAIN\SQL;
   5:  Initial Catalog=Northwind;Integrated Security=True"
   6:  providerName="System.Data.SqlClient" />
   7:   
   8:  connectionStrings>

For database attached in App_Data Folder we can write
   1:  <connectionStrings>
   2:   
   3:  <add name="MyCon" 
   4:  connectionString="Data Source=.\SQLEXPRESS;
   5:  AttachDbFilename=|DataDirectory|\NORTHWND.MDF;
   6:  Integrated Security=True;User Instance=True"
   7:  providerName="System.Data.SqlClient" />
   8:   
   9:  connectionStrings>

USING APPSETTINGS

We can also write data in Key Value pairs.
   1:  <configuration>
   2:  <appSettings>
   3:  <add key="MyCon" 
   4:       value="Data Source=AMITJAIN\SQL;
   5:              Initial Catalog=Northwind;
   6:              User ID=amit;Password=password"/>
   7:  appSettings>
   8:  configuration>

READ CONNECTION STRING FROM WEB.CONFIG FILE IN ASP.NET

To read Settings we can write code as mentioned below.

C# CODE

1using System.Configuration;
2string strConn = ConfigurationManager.ConnectionStrings["MyCon"].ConnectionString;

1using System.Configuration;
2string strConn = ConfigurationManager.AppSettings["MyCon"].ToString();

VB.NET CODE

1Imports System.Configuration
2Dim strConn As String = ConfigurationManager.ConnectionStrings("MyCon").ConnectionString
3 Dim strConn As String = ConfigurationManager.AppSettings("MyCon").ToString()

Read More »

Tuesday, March 27, 2012

Public Vs Shared in VB.NET OOPS Concept

public vs shared

public vs. shared -------------------------------------------------------------------------------- Public and Shared aren't comparable like protected and private and public and friend are. Those are access modifiers - they modify who has the right to access them. Public simply means that any code can access the member (function/property/field). Shared which is more like a flag (something either is shared or isn't), indicates that the member (function/property/field) doesn't behave/belong to a specific instance of the class. So your own definitions are pretty accurate, I simply want to make it clear that public and shared aren't comparable. Something can be public shared, private shared, friend shared, protected shared or simply public, private, friend or protected (then there's protected friend, but we'll ignore that for now). private shared means that only the class itself can access the field, a frequent use of a private shared field is for use with singletons: public class MyClass private shared MyClass instance = nothing public shared function GetInstance() as MyClass if instance is nothing then instance = new MyClass() end if return instance() end function private sub new() end sub ... end class from the above code you can see that MyClass can never be created directly since the constructor is private (outside code can't call it). Outside code also can't access the instance field because it too is private. Outside code can however access GetInstance because it's public. GetInstance checks to see if the private field "instance" is nothing (it can access a private field because it's all the same class), if it is, it creates the instance (again, it can access the private constructor) and returns the instance (there is a possible race condition, but that's besides the point). What's neat about the above example is that GetInstance is marked shared. IF it wasn't, no one would ever be able to call it because the constructor is private and thus an instance can't be created. Without an instance, a non-shared member can't be accessed.
Solution

Public and Shared aren't comparable like protected and private and public and friend are. Those are access modifiers - they modify who has the right to access them. Public simply means that any code can access the member (function/property/field). Shared which is more like a flag (something either is shared or isn't), indicates that the member (function/property/field) doesn't behave/belong to a specific instance of the class. So your own definitions are pretty accurate, I simply want to make it clear that public and shared aren't comparable. Something can be public shared, private shared, friend shared, protected shared or simply public, private, friend or protected (then there's protected friend, but we'll ignore that for now). private shared means that only the class itself can access the field, a frequent use of a private shared field is for use with singletons:[CODE] public class MyClass private shared MyClass instance = nothing public shared function GetInstance() as MyClass if instance is nothing then instance = new MyClass() end if return instance() end function private sub new() end sub ... end class[/CODE] from the above code you can see that MyClass can never be created directly since the constructor is private (outside code can't call it). Outside code also can't access the instance field because it too is private. Outside code can however access GetInstance because it's public. GetInstance checks to see if the private field "instance" is nothing (it can access a private field because it's all the same class), if it is, it creates the instance (again, it can access the private constructor) and returns the instance (there is a possible race condition, but that's besides the point). What's neat about the above example is that GetInstance is marked shared. IF it wasn't, no one would ever be able to call it because the constructor is private and thus an instance can't be created. Without an instance, a non-shared member can't be accessed. You might find Paul Vick's great VB.Net book useful:

http://books.google.com/books?id=ejzRTF2TL6UC&prev=http://print.google.com/print%3Fq%3Dpaul%2Bvick&pg=1&sig=ZxfWU68bu9eUj6Q910vyvVa9Gwg&hl=en
Read More »

My Blog List