Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hello Friends,
I have code, to pick up the, values from database in Session, By Clicking on Dynamically Generated Linkbutton. I want to redirect those values to another page, my code is here..

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 System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class Dynamic_link : System.Web.UI.Page
{
    SqlConnection cnn = new SqlConnection();
    protected void Page_Load(object sender, EventArgs e)
    {
        cnn.ConnectionString=ConfigurationManager.ConnectionStrings["jin"].ConnectionString;
        if(cnn.State==ConnectionState.Closed)
        {
            cnn.Open();
        }  
        SqlCommand cm = new SqlCommand("select itemname from menu", cnn);  
          cm.CommandType = CommandType.Text;  
         SqlDataAdapter da = new SqlDataAdapter(cm);
        DataSet dt = new DataSet();
        da.Fill(dt);
         Repeater Repeater1 = new Repeater();
        Repeater1.DataSource = dt;
        Repeater1.DataBind();
        if (cnn.State == ConnectionState.Open)
        {
            cnn.Close();
        }  
        foreach (RepeaterItem repeatItem in Repeater1.Items)
        {
            // if condition to add HeaderTemplate Dynamically only Once  
            if (repeatItem.ItemIndex == 0)
            {
                RepeaterItem headerItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Header);
                HtmlGenericControl hTag = new HtmlGenericControl("h4");
               hTag.InnerHtml = "Menu Collection";
                repeatItem.Controls.Add(hTag);
            }

            // Add ItemTemplate DataItems Dynamically  
            //SqlDataReader dr = Int32.Parse;
            RepeaterItem repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Item);
            LinkButton lnk = new LinkButton();
            lnk.ID = "lnk_itemname";
            lnk.Text = String.Format("{0}",(dt.Tables[0].Rows[repeatItem.ItemIndex]["itemname"]));
            lnk.Click += new System.EventHandler(link_Click);
            repeatItem.Controls.Add(lnk);
           // Add SeparatorTemplate Dynamically  
            repeaterItem = new RepeaterItem(repeatItem.ItemIndex, ListItemType.Separator);
            LiteralControl ltrlHR = new LiteralControl();
            ltrlHR.Text = "<hr />";
            repeatItem.Controls.Add(ltrlHR);
        }

        // Add Repeater Control as Child Control  
        // of Panel Control  
        Panel1.Controls.Add(Repeater1);  
    }

    protected void link_Click(object sender, EventArgs e)
    {
        cnn.ConnectionString = ConfigurationManager.ConnectionStrings["jin"].ConnectionString;
       SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = new SqlCommand("str_get_subitemDetails", cnn);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
         DataSet ds = new DataSet();
         DataTable dt = new DataTable("submenu");
        da.Fill(dt);

        Session["subitem_name"] = dt.Rows[0]["subitem_name"].ToString();
        Session["item_descript"] = dt.Rows[0]["item_descript"].ToString();
        Session["item_rate"] = dt.Rows[0]["item_rate"].ToString();
        Response.Redirect("getsubitem.aspx");
    }
    
    
}


My StoredProcedure Code is this:
SQL
USE [JIN]
GO
/****** Object:  StoredProcedure [dbo].[str_get_subitemDetails]    Script Date: 03/06/2013 16:51:55 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER procedure [dbo].[str_get_subitemDetails]
@item_id nvarchar(50),
@itemname nvarchar(50),
@subitem_name nvarchar(50),
@item_descript nvarchar(100),
@item_rate decimal(18,2)
as
begin
select submenu.subitem_name,submenu.item_descript,submenu.item_rate from submenu join menu on menu.item_id=submenu.item_id where menu.itemname=@itemname
end


I m Facing this error...
No mapping exists from object type ASP.dynamic_link_aspx to a known managed provider native type.
Please Help me


Facing error:
Procedure or function 'str_get_subitemDetails' expects parameter '@item_id', which was not supplied.

please help:
Posted
Updated 6-Mar-13 5:39am
v4
Comments
[no name] 6-Mar-13 9:35am    
Yes.... and?
[no name] 6-Mar-13 9:42am    
You probably cannot convert a LinkButton to itemname in your parameters. The answer to your next problem, it's because you did not pass enough parameters to your stored procedure.
Ankit_Sharma1987 6-Mar-13 9:48am    
Thnks for reply,
Sir, linkbutton are already generate, and, itemname, is fetching on linkbuttons, all i want to do, is that, i have fetch, the, rest column, by itemname, and take it into session...
please help sir..
[no name] 6-Mar-13 9:58am    
What does any of that have to do with your problem or what I said? You are trying to convert a LinkButton control to "itemname" in your query and you quite possibly cannot do that!
Ankit_Sharma1987 6-Mar-13 10:06am    
Ok, sir I have updaed my code:

protected void link_Click(object sender, EventArgs e)
{
cnn.ConnectionString = ConfigurationManager.ConnectionStrings["jin"].ConnectionString;
SqlCommand cm1 = new SqlCommand("str_get_subitemDetails", cnn);
cm1.CommandType = CommandType.StoredProcedure;
cnn.Open();
cm1.ExecuteNonQuery();
cnn.Close();
Session["subitem_name"] = cm1.CommandType;
Session["item_descript"] = cm1.CommandType;
Session["item_rate"] = cm1.CommandType;
Response.Redirect("getsubitem.aspx");
}
But Now, I m getting Error:
Procedure or function 'str_get_subitemDetails' expects parameter '@item_id', which was not supplied.
Now What should i have to do now!!

1 solution

Please, check how to add parameters to SqlCommand: SqlParameterCollection.AddWithValue[^].
Hint: You need to pass 5 parameters ;)
 
Share this answer
 
Comments
Espen Harlinn 10-Mar-13 18:30pm    
5'ed!
Maciej Los 10-Mar-13 18:32pm    
Thank you, Espen ;)

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