Click here to Skip to main content
15,884,099 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
So I've created 2 pages, Rental.aspx (the page that will appear when hyperlink is clicked) and BookList.aspx (The page where the linkbutton is displayed).
For the Rental page, I used Gridview with BookTitle and Inputs inside. I thought of making the GridView to display only rows of bookTitle(hyperlink) selected from the BookList page.

I've also created 2 MySQL tables like this:
1) Table: rent
Columns: booktitle, date, name, memberID, phone, signIn
2) Table: booklist
Columns: id, booktitle, availability

Codes

BookList.aspx GridView part:

ASP.NET
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" DataKeyNames="id" OnRowDeleting="GridView1_RowDeleting"  >
    <Columns>
        <asp:TemplateField HeaderText="Id">
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Eval("id") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Book Title">
            <ItemTemplate>
           <asp:LinkButton ID="bookTitle" OnClick="link_Click" Text='<%# Eval("bookTitle") %>' runat="server" ></asp:LinkButton>

            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Availability">

            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("availability") %>'></asp:Label>
            </ItemTemplate>

        </asp:TemplateField>



        <asp:TemplateField>
            <ItemTemplate>
                <asp:LinkButton ID="delete" CommandName="Delete" runat="server">Delete</asp:LinkButton>
            </ItemTemplate>
        </asp:TemplateField>


BookList.aspx.cs

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.Adapters;
using System.Data;
using MySql.Data.MySqlClient;
using System.Windows;

namespace Library_System
{
    public partial class WebForm3 : System.Web.UI.Page
    {
        DataSet myData = new DataSet();
        MySqlConnection conn = new MySqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
           conn.ConnectionString = "server=127.0.0.1;database=test;port=3306;username=root;password=";
            if(!IsPostBack)
            {
                BindData();
       
            }
          
        }
   private void BindData()
        {
            DataTable dt = new DataTable();
            using (conn)
            {
                MySqlDataAdapter adp = new MySqlDataAdapter("select * from test.booklist", conn);
                adp.Fill(dt);

                if (dt.Rows.Count &gt; 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }

        protected void link_Click(object sender, EventArgs e)
        {
            Response.Redirect("Rent.aspx?bookTitle=" + ((LinkButton)sender).Text);

        }


Rent.aspx

ASP.NET
<asp:GridView ID="GridView1" runat="server" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" AutoGenerateColumns="False"  OnRowUpdating="GridView1_RowUpdating">
            <Columns>
                <asp:TemplateField HeaderText="Book Title">
                    <ItemTemplate>
                        <asp:Label ID="book" runat="server" Text='<%# Eval("bookTitle") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date">
                    <ItemTemplate>
                        <asp:TextBox ID="date" runat="server" Text='<%# Eval("date") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Name">
                    <ItemTemplate>
                        <asp:TextBox ID="name" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Member ID">
                    <ItemTemplate>
                        <asp:TextBox ID="memberID" runat="server" Text='<%# Eval("memberID") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Phone">
                    <ItemTemplate>
                        <asp:TextBox ID="phone" runat="server" Text='<%# Eval("phone") %>'></asp:TextBox>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Sign In">
                    <ItemTemplate>
                        <asp:ImageButton ID="signIn" runat="server" AlternateText="On Rent" ImageUrl="~/Images/unclick.png" OnClick="SignIn_Click" />
                    </ItemTemplate>
                </asp:TemplateField>
            
                <asp:TemplateField>

                    <ItemTemplate>
                        <asp:LinkButton ID="submit" runat="server" Text="Submit" style="background-image: url(~/Images/save)" CommandName="Update"></asp:LinkButton>
                    </ItemTemplate>

                </asp:TemplateField>


Rent.aspx.cs

C#
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 MySql.Data.MySqlClient;
using System.Windows;


namespace Library_System
{
    public partial class _new : System.Web.UI.Page
    {
        DataSet myData = new DataSet();
        MySqlConnection conn = new MySqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {
            conn.ConnectionString = "server=127.0.0.1;database=test;port=3306;username=root;password=";
            if (!IsPostBack)
            {
                BindData();
               
            }
        }

        private void BindData()
        {
            string bookTitle = Request.QueryString["bookTitle;"];
            conn.Open();
            DataTable dt = new DataTable();
       
                MySqlCommand cmd = new MySqlCommand("select a.bookTitle from booklist a, rent b where a.bookTitle = b.bookTitle;" + "select * from test.rent where bookTitle= @bookTitle; ", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                MySqlParameter p = new MySqlParameter("@bookTitle", bookTitle);
                cmd.Parameters.Add(p);
                MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
                
                adp.Fill(dt);

                if (dt.Rows.Count > 0)
                {
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            
        }


I think I'm able to click the hyperlink from booklist but when it's redirecting, the error was shown in rent.aspx.cs near the "adp.Fill(dt);"
Posted
Updated 12-Jul-15 17:08pm
v2
Comments
DamithSL 12-Jul-15 23:35pm    
what is the error?

1 solution

change
C#
string bookTitle = Request.QueryString["bookTitle;"];

to
C#
string bookTitle = Request.QueryString["bookTitle"];

another issue is you have two sql select statements in your mysql command and you trying to fill single data table from the result. if you have two select sql statements you will get two data tables as results. try to use DataSet; which can have multiple tables.
change
DataTable dt = new DataTable();
to
DataSet ds = new DataSet();

then change adp.Fill(dt); to adp.Fill(ds);
when you bind assign the table you want, for example GridView1.DataSource = ds.Tables[0];

Another option is use one sql select statement
(select a.bookTitle from booklist a, rent b where a.bookTitle = b.bookTitle;
or
select * from test.rent where bookTitle= @bookTitle)
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900