Get data from user and show in another page on Button Click using C# and VB.Net in ASP.Net

sofia
 
on Dec 03, 2021 09:20 PM
Sample_209342.zip
502 Views

Hi dharmedr sir,

I have to get organisation logo, user name and designation from user . It could be one or multiple user logo and designation.

When we enter all these thing and click on button it should be shown on our next welcome page. 

I am new at asp net.

Please help me for doing this sir.

Download FREE API for Word, Excel and PDF in ASP.Net: Download
dharmendr
 
on Dec 03, 2021 11:26 PM

Hi sofia,

Check this example. Now please take its reference and correct your code.

HTML

Home

<div class="container" style="padding: 20px; height: auto; background-color: darkseagreen; border-radius: 10px 6px">
    <div>
        <div class="centerHaeder" style="text-align: center">
            <h1>BOOK STORE</h1>
        </div>
        <hr />
        <div style="text-align: center">
            <p><i>What's your favourite book!</i></p>
        </div>
    </div>
    <div class="col-sm-12">
        <b>
            <asp:Label ID="Label1" runat="server" Text="Book Name"></asp:Label></b><br />
        <asp:TextBox ID="txtBookName" runat="server" Width="100%" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="col-sm-12">
        <br />
        <b>
            <asp:Label ID="Label2" runat="server" Text="Author Name"></asp:Label></b><br />
        <asp:TextBox ID="txtAuthorName" runat="server" Width="100%" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="col-sm-12" style="text-align: center">
        <br />
        <asp:Button ID="btnAddBook" runat="server" Text="Add Book" OnClick="btnAddBook_Click" CssClass="btn-success" />
    </div>
</div>

BookList

<div class="container">
    <h2>List Of Books And Authors</h2>
    <div>
        BookName
    <asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
        AuthorName
    <asp:Label ID="Label2" runat="server" Text=""></asp:Label><br />
        <a href="Home.aspx" runat="server">Go Back</a>
    </div>
    <div class="col-sm-12">
        <asp:GridView ID="grdBookList" runat="server" CssClass="table table-striped table-bordered table-hover"
            AllowPaging="True" AutoGenerateSelectButton="True" Width="100%" CellPadding="4" ForeColor="#333333">
            <AlternatingRowStyle BackColor="White" />
            <EditRowStyle BackColor="#7C6F57" />
            <FooterStyle BackColor="#005D28" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#005D28" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
            <RowStyle BackColor="#E3EAEB" />
            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
            <SortedAscendingCellStyle BackColor="#F8FAFA" />
            <SortedAscendingHeaderStyle BackColor="#246B61" />
            <SortedDescendingCellStyle BackColor="#D4DFE1" />
            <SortedDescendingHeaderStyle BackColor="#15524A" />
        </asp:GridView>
    </div>
</div>

Code

Home

C#

protected void btnAddBook_Click(object sender, EventArgs e)
{
    string bookname = txtBookName.Text;
    string authorname = txtAuthorName.Text;
    Session["BookName"] = bookname;
    Session["AuthorName"] = authorname;
    Response.Redirect("~/BookList.aspx");
}

VB.Net

Protected Sub btnAddBook_Click(ByVal sender As Object, ByVal e As EventArgs)
    Dim bookname As String = txtBookName.Text
    Dim authorname As String = txtAuthorName.Text
    Session("BookName") = bookname
    Session("AuthorName") = authorname
    Response.Redirect("~/BookList.aspx")
End Sub

BookList

C#

protected void Page_Load(object sender, EventArgs e)
{
    Label1.Text = Session["BookName"].ToString();
    Label2.Text = Session["AuthorName"].ToString();
    BindGridView(Session["BookName"].ToString(), Session["AuthorName"].ToString());
}

private void BindGridView(string bookname, string authorname)
{
    System.Data.DataRow Dr = GridData.NewRow();
    Dr["Book Name"] = bookname;
    Dr["Author Name"] = authorname;
    GridData.Rows.Add(Dr);
    grdBookList.DataSource = GridData;
    grdBookList.DataBind();
}

public System.Data.DataTable GridData
{
    get
    {
        if (Session["GridData"] == null)
        {
            System.Data.DataTable dt = new System.Data.DataTable("GridData");
            dt.Columns.Add(new System.Data.DataColumn("Book Name"));
            dt.Columns.Add(new System.Data.DataColumn("Author Name"));
            Session["GridData"] = dt;
            return dt;
        }
        else
        {
            return (System.Data.DataTable)Session["GridData"];
        }
    }
    set { Session["GridData"] = value; }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Label1.Text = Session("BookName").ToString()
    Label2.Text = Session("AuthorName").ToString()
    BindGridView(Session("BookName").ToString(), Session("AuthorName").ToString())
End Sub

Private Sub BindGridView(ByVal bookname As String, ByVal authorname As String)
    Dim Dr As System.Data.DataRow = GridData.NewRow()
    Dr("Book Name") = bookname
    Dr("Author Name") = authorname
    GridData.Rows.Add(Dr)
    grdBookList.DataSource = GridData
    grdBookList.DataBind()
End Sub

Public Property GridData As System.Data.DataTable
    Get

        If Session("GridData") Is Nothing Then
            Dim dt As System.Data.DataTable = New System.Data.DataTable("GridData")
            dt.Columns.Add(New System.Data.DataColumn("Book Name"))
            dt.Columns.Add(New System.Data.DataColumn("Author Name"))
            Session("GridData") = dt
            Return dt
        Else
            Return CType(Session("GridData"), System.Data.DataTable)
        End If
    End Get
    Set(ByVal value As System.Data.DataTable)
        Session("GridData") = value
    End Set
End Property

Screenshot

 

dharmendr
 
on Dec 04, 2021 03:55 AM
on Dec 04, 2021 04:26 AM

Hi sofia,

Refer below code.

HTML

Home

<div class="container" style="padding: 20px; height: auto; background-color: darkseagreen; border-radius: 10px 6px">
    <div>
        <div class="centerHaeder" style="text-align: center">
            <h1>WELCOME GUEST</h1>
        </div>
        <hr />
        <div style="text-align: center">
            <p><i>GUEST NAME AND Designation!</i></p>
        </div>
    </div>
    <div class="col-sm-12">
        <b>
            <asp:Label ID="Label1" runat="server" Text="Guest Name"></asp:Label></b><br />
        <asp:TextBox ID="txtName" runat="server" Width="100%" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="col-sm-12">
        <br />
        <b>
            <asp:Label ID="Label2" runat="server" Text="Guest Designation"></asp:Label></b><br />
        <asp:TextBox ID="txtdesignation" runat="server" Width="100%" CssClass="form-control"></asp:TextBox>
    </div>
    <div class="col-sm-12">
        <br />
        <b>
            <asp:FileUpload ID="FileUpload1" runat="server" />
    </div>
    <div class="col-sm-12" style="text-align: center">
        <br />
        <asp:Button ID="btnAddBook" runat="server" Text="Add Book" OnClick="btnAddBook_Click" CssClass="btn-success" />
    </div>
</div>

welcome

<table>
    <tr>
        <td>
            <asp:Image runat="server" ID="imgLogo" /></td>
            <td><asp:Label ID="lblName" runat="server" /></td>
    </tr>
        <tr>
        <td colspan="2">
            <asp:Label ID="lblDesignation" runat="server" /></td>
    </tr>
</table>

Code

Home

protected void btnAddBook_Click(object sender, EventArgs e)
{
    string name = txtName.Text;
    string designation = txtdesignation.Text;
    string fileName = System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName);
    if (!System.IO.Directory.Exists(Server.MapPath("~/Logo")))
    {
        System.IO.Directory.CreateDirectory(Server.MapPath("~/Logo"));
    }
    string filePath = "~/Logo/" + fileName;
    FileUpload1.SaveAs(Server.MapPath(filePath));
    string logo = filePath;
    Session["Name"] = name;
    Session["Designation"] = designation;
    Session["Logo"] = logo;
    Response.Redirect("~/welcome.aspx");
}

welcome

protected void Page_Load(object sender, EventArgs e)
{
    imgLogo.ImageUrl = Session["Logo"].ToString();
    lblName.Text = Session["Name"].ToString();
    lblDesignation.Text = Session["Designation"].ToString();
}