"If at first you don't succeed; call it version 1.0" :-Unknown

Pages

Thursday, February 3, 2011

Getting value from previous page OR Cross page PostBack

hi,
Getting value from previous page, it's simple use query-string is it? did u every noticed the url then. its look like a sexy model with grass on ass.I hate that.On my current project i faced this problem. when i c that i was shocked that's killing my project. my browser can't display it's fully. For some data i can't. 
                                       Then i made a study over postback it's ended in a ocean. Dot NET is providing 100 ways to solve this problem then i don't know y everyone running after this fucking query-string. among those i scrabbled 2 simple way's.


Method 1


Suppose i have 2 pages [ Page1.aspx, Page2.aspx ] 
On Page1.aspx create 2 text-box and a button like this


   First Name :
   <asp:TextBox ID="txt_Fname" runat="server"></asp:TextBox><br />
   Second Name :
   <asp:TextBox ID="txt_Lname" runat="server"></asp:TextBox><br />
   <asp:Button ID="btn_Save" runat="server" PostBackUrl="~/page2.aspx"
    Text="Submit" OnClick="btn_Save_Click" />

on button add PostBackUrl="~/page2.aspx"  

on Page2.aspx.cs add this code on page load Event

    protected void Page_Load(object sender, EventArgs e)
    {
        if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
        {
            TextBox txtPFname = (TextBox)PreviousPage.FindControl("txt_Fname");
            TextBox txtPLname = (TextBox)PreviousPage.FindControl("txt_Lname");
            lblMsg.Text="Welcome :'"+txtPFname.Text+"'"+txtPLname.Text+"";

        }
    }


Method 2

Suppose i have 2 pages [ Default1.aspx, Default2.aspx ]

On Default1.aspx add 2 textbox and 1 button like Method 1

        First Name :
        <asp:TextBox ID="txt_Fname" runat="server"></asp:TextBox><br />
        Second Name :
        <asp:TextBox ID="txt_Lname" runat="server"></asp:TextBox><br />
        <asp:Button ID="btn_Save" runat="server" PostBackUrl="~/Cross Post Back/page2.aspx"
        Text="Submit" OnClick="btn_Save_Click" />

On default1.aspx.cs add this code

 public TextBox txtpFname
    {
        get
        {
            return txt_Fname;
        }
    }
    public TextBox txtpLname
    {
        get
        {
            return txt_Lname;
        }
    }


On default2.aspx add this line on top 

<%@ PreviousPageType VirtualPath="Default1.aspx" %>


and on default2.aspx.cs, on pageload event add these lines 




    {
        if (PreviousPage != null && PreviousPage.IsCrossPagePostBack)
        {
            lblMsg.Text = "Hai," + PreviousPage.txtpFname.Text + " "+PreviousPage.txtpLname.Text+"";
        }
    }


Hope these help u some way :)

Have a nice day... 'N happy Coding :)

Monday, January 31, 2011

Recover the Database in SQL SERVER

 

Steps to Recover

1.create the Database with same Name,MDF Name,LDF Name.
2.Stop the Sql Server and then Replace the only new MDF file by old database (Corrupted database) MDF file and delete the LDF File of newly created database.
3.Now Start the Sql Server again.
4.you can notice that database status became 'Suspect' as expected.
5.Then run the given script to know the current status of your newly created datatbase.
(Better you note it down the current status)
SELECT *
FROM sysdatabases
WHERE name = 'yourDB'
6.Normally sql server would not allow you update anything in the system database.SO run the given script to enable the update to system database
sp_CONFIGURE 'allow updates', 1
RECONFIGURE WITH OVERRIDE
7.After run the above script, update the status of your newly database as shown below. once you updated the status, database status become 'Emergency/Suspect'.
UPDATE sysdatabases
SET status = 32768
WHERE name = 'yourDB'
8.Restart SQL Server (This is must, if it is not done SQL Server will through an error)

9.Execute this DBCC command to create the LDF file.make sure the Name of LDF file which you are giveing is same as deleted LDF file of Newly Created database.
DBCC TRACEON (3604)
DBCC REBUILD_LOG(bmpos,'D:\yourDB_Log.ldf')

DBCC accepts two parameters

1. parameter is database name and
2. parameter is physical path (where the MDF file is located) of the log file. (*Make sure the path is physical, if you specify the logical file name it will throw an error.)
10.Run the given stored procedure to reset the status of your database.
sp_CONFIGURE 'allow updates',0
RECONFIGURE WITH OVERRIDE
11.Do not forget to disable theallow update to system datatbase.
sp_CONFIGURE 'allow updates',0
RECONFIGURE WITH OVERRIDE
12.At last, update the status which you have noted in the 5th step.
UPDATE sysdatabases
SET status = 1073741840
WHERE name = 'yourDB'
Note : During steps 8, 9 , 10 you may encounter any errors if database is in use.
in this case you Set the database to single user.
sp_DBOPTION 'yourDB', 'single user','true'
Once the steps 8,9,10 are completed and database is already single user mode, then run this script to set it multiple users mode.
sp_DBOPTION 'yourDB', 'single user','false'
That's all, now database ready to use with multiple users mode. hopes help and save your time.


Have a nice day... 'N happy Coding :)

Saturday, January 22, 2011

Dynamically Adding Controls in ASP.NET

In my previous post i show Dynamically adding controls on grid with validation. I know this an old topic I have read the forums many readers asking about add controls dynamically in asp.net giving problems and also it not working properly with events. So I have decided write the snippet for you.


Implementation

Let’s create a simple page with a PlaceHolder to add control from code behind.

Html Code


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Dynamically Adding Controls in ASP.NET</title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:PlaceHolder ID="plMain" runat="server"></asp:PlaceHolder>
        </div>
    </form>
</body>
</html>



C# Code




protected override void OnPreInit(EventArgs e)
    {
        Button btn = new Button();
        btn.ID = "btnSave";
        btn.Text = "Click Me";
        btn.Click += new EventHandler(btn_Click);
        this.plMain.Controls.Add(btn);
        base.OnPreInit(e);
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        // here only processing.
    }

    void btn_Click(object sender, EventArgs e)
    {
        Response.Write("<p>The Button has clicked</p>");

    }

Now the reason why some programmers getting error because they try to create controls(Button) on pageload event. After the button is added and when I click button make a post back, but unfortunately button was disappeared. The reason is when we are click button it make post back, so each post back there page load from top to bottom.

There is easy solution for this issue. You have to select a correct event to place the code to add control into page dynamically. The best and correct event is 
Page_Init.
Init Event:http://msdn.microsoft.com/en-us/library/ms178472.aspx

Have a nice day... 'N happy Coding :)