Friday, 13 December 2013

How to Data Transfer from one form to another form and save to the database:


This is very simple concept and possible through Page Navigation  Control:-suppose You have two form  one is-Default3.aspx and another is Default4.aspx Both form’s  some fields are same.so I want   after filling Default3.aspx form  and when  click to button or link button  it should be navigate to Default4.aspx page with data,than  after fill anather field and click to button for save,it will save to the data base.How it is possible. Please Follow this step :-
 Step-1
Create Table in following code  in sqlserver database:-

CREATE TABLE [dbo].[rabi2] (
    [Id]       INT          IDENTITY (1, 1) NOT NULL,
    [username] VARCHAR (50) NULL,
    [colname]  NCHAR (10)   NULL,
    [branch]   VARCHAR (50) NULL,
    [sem]      VARCHAR (50) NULL,
    [ads]      VARCHAR (50) NULL,
    PRIMARY KEY CLUSTERED ([Id] ASC)
);


Step-2
Design Source page (Default3.aspx) like this:-
Demo:-

Code for design :-
Copy this code and paste inside source code(Default3.aspx):-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 22%;
            border: 1px solid #FF0000;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td align="center" colspan="2">
                    Source Form...</td>
            </tr>
            <tr>
                <td>
                    UserName</td>
                <td>
                    <asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Name</td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Age</td>
                <td>
                    <asp:TextBox ID="txtage" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Address</td>
                <td>
                    <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Summit</asp:LinkButton>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

Step-3:-
Write  this code at Default3.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
             Response.Redirect("Default4.aspx?Value=" +txtusername.Text);
    }
}

Step-4:-
Design Target page like this:-
Demo:-
 
Code for design :-
Copy this code and paste inside your source code(Default4.aspx) :-
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 22%;
            border: 1px solid #FF0000;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td align="center" colspan="2">
                    Target Form.....</td>
            </tr>
            <tr>
                <td>
                    UserName</td>
                <td>
                    <asp:TextBox ID="txtusername" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    College Name</td>
                <td>
                    <asp:TextBox ID="txtcolname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Branch</td>
                <td>
                    <asp:TextBox ID="txtbranch" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    Sem</td>
                <td>
                    <asp:TextBox ID="txtsem" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    AAddress</td>
                <td>
                    <asp:TextBox ID="txtaddress" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td align="center" colspan="2">
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Save" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>

Step-5:-
Write this code  at Default4.aspx.cs page:-
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class Default4 : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=x;Initial Catalog=xyz;Integrated Security=True;Pooling=False");
    protected void Page_Load(object sender, EventArgs e)
    {
        txtusername.Text = Request.QueryString["Value"];
       
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        conn.Open();
        string sql = "insert into rabi2(username,colname,branch,sem,ads) values('" + txtusername.Text + "','" + txtcolname.Text + "','" + txtbranch.Text + "','" + txtsem.Text + "','" + txtaddress.Text + "')";
        SqlCommand cmd = new SqlCommand(sql, conn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        conn.Close();
        String ss = "Your Record saved sucessfully.....";
        Response.Write("<script>alert('" + ss + "')</script>");
       
    }
}

Thanks & Regard
Rabi

No comments:

Post a Comment