Pages

Advertisement

Saturday, January 19, 2008

Changing Asp.net form's attribute using Response.filter

There are many ways to change the forms attribute and this is one of them ..

Here is the simple code for that ..

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Text.RegularExpressions;
 
public class cdsnetFormActionModifier : Stream
{
    private Stream _sink;
    private long _position;
    string _url;
    public cdsnetFormActionModifier(Stream sink, string url)
    {
        _sink = sink;
        _url = "$1" + url + "$3";
    }
 
    public override bool CanRead
    {
        get { return true; }
    }
 
    public override bool CanSeek
    {
        get { return true; }
    }
 
    public override bool CanWrite
    {
        get { return true; }
    }
 
    public override long Length
    {
        get { return 0; }
    }
 
    public override long Position
    {
        get { return _position; }
        set { _position = value; }
    }
 
    public override long Seek(long offset, System.IO.SeekOrigin direction)
    {
        return _sink.Seek(offset, direction);
    }
 
    public override void SetLength(long length)
    {
        _sink.SetLength(length);
    }
 
    public override void Close()
    {
        _sink.Close();
    }
 
    public override void Flush()
    {
        _sink.Flush();
    }
 
    public override int Read(byte[] buffer, int offset, int count)
    {
        return _sink.Read(buffer, offset, count);
    }
 
    public override void Write(byte[] buffer, int offset, int count)
    {
        string s = System.Text.UTF8Encoding.UTF8.GetString(buffer, offset, count);
        Regex reg = new Regex("(<form.*action=\")([^\"]*)(\"[^>]*>)", RegexOptions.IgnoreCase);
        Match m = reg.Match(s);
        if (m.Success)
        {
            string form = reg.Replace(m.Value, _url);
            int iform = m.Index;
            int lform = m.Length;
            s = s.Substring(0, iform) + form + s.Substring(iform + lform);
        }
        byte[] yaz = System.Text.UTF8Encoding.UTF8.GetBytes(s);
        _sink.Write(yaz, 0, yaz.Length);
    }
}

 


And configure your load event of any page you want like below


  protected void Page_Load(object sender, EventArgs e)
{
this.Response.Filter = new cdsnetFormActionModifier(this.Response.Filter, "your_url");
}

Generating Stored Procedures dynamically

Create Stored Procedure

Create stored procedure to auto generate another stored procedure contain insert command for any tables.

CREATE PROCEDURE Create_procedure_To_insert

parameters


You must add two parameters:

1- Table Name: to generate insert command on it.

2- Developer Name: to set developer name in comments.

//
//

CREATE PROCEDURE create_procedure

@table varchar(200),

@DeveloperName varchar(200),

@Createtable varchar(20)

//

Declaration Variables


You need to declaration many variables to use in stored procedure see below code to know it.

declare @testTable varchar(8000)

declare @testTable2 varchar(8000)

declare @testTable3 varchar(8000)

declare @opration varchar(8000)

declare @final varchar(8000)

declare @OP varchar(100)

 

1- @testTable: used this variable to set all columns from table.

2- @testTable2:used this variable to set all datatype for columns from table.

3- @testTable3:used this variable to set all parameters for columns from table.

4- @opration :used this variable to set insert command.

5-@final :used this variable to set auto generate stored procedure.

6-@OP :used this variable to set name for the new stored procedure.

Initialization Variables


You need to set empty values in the below variables.

set @testTable=''

set @testTable2=''

set @final=''

set @testTable3=''

set @opration=''

declare @Datetime varchar(50)

set @Datetime=getdate()

Importents Code


You need to create three select statment :

1- The First Select statment to get all columns from information_schema.columns when table equal table name in parameters ,you must get 'isidentity'= zero becuase if 'isidentity'=one you can not insert data on identity columns

2-The second Select statment to get all datatype from information_schema.columns when table equal table name in parameters .you must get 'isidentity'= zero becuase if 'isidentity'=one you can not insert data on identity columns.

3-The Third Select statment to get columns name and set @columns name in parameters

after that you must generate the new stored procedure ,so you can create structure for procedure in string datatype and set the string in execute function to execution stored procedure "Exec(@final)"

select @testTable=@testTable+ ',

'+column_name from information_schema.columns where table_name=@table and (COLUMNPROPERTY(OBJECT_ID(@table), column_name, 'isidentity') = 0) AND (column_default IS NULL)

select @testTable2=@testTable2+ ',

@'+column_name+' '+data_type+'(' + cast(character_maximum_length as varchar(10)) +')' + case is_nullable when 'no' then ' ' when 'yes' then '=null' end from information_schema.columns where table_name=@table and (COLUMNPROPERTY(OBJECT_ID(@table), column_name, 'isidentity') = 0)and character_maximum_length<>null AND (column_default IS NULL)and data_type<>'text'

select @testTable2=@testTable2+ ',

@'+column_name+' '+data_type from information_schema.columns where table_name=@table and (COLUMNPROPERTY(OBJECT_ID(@table), column_name, 'isidentity') = 0)and (character_maximum_length=null or data_type='text' ) AND (column_default IS NULL)

select @testTable3=@testTable3+ ',

@'+column_name from information_schema.columns where table_name=@table and (COLUMNPROPERTY(OBJECT_ID(@table), column_name, 'isidentity') = 0) AND (column_default IS NULL)

set @testTable=SUBSTRING(@testTable,2,len(@testTable))

set @testTable2=SUBSTRING(@testTable2,4,len(@testTable2))

set @testTable3=SUBSTRING(@testTable3,2,len(@testTable3))

set @opration=' insert into [' +@table+']

(

'+@testTable+'

)

values

(

'+ @testTable3 +'

)'

set @OP='InsertNew'+@table

set @final='/*

----------------------------------------------------------------------------------------

Store Procedure Name : SP__'+@OP +'

----------------------------------------------------------------------------------------

1- Creation Date :'+convert (varchar,getdate(),103) +'

2- Last Update :'+convert (varchar,getdate(),103)+'

3- Parametars No:6

4- Creation By :'+@DeveloperName+'

5- Last Update By :'+@DeveloperName+'

6- Return Value : Dataset

---------------------------------------------------------------------------------------

*/

Create PROCEDURE SP__'+@OP+'

(
'+ @testTable2 + '
)

AS

set nocount on

' + @opration + '

Select * from [' +@table +']'

exec (@final)