Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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..
 
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:
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 6-Mar-13 4:33am
Edited 6-Mar-13 6:39am
v4
Comments
ThePhantomUpvoter at 6-Mar-13 9:35am
   
Yes.... and?
ThePhantomUpvoter at 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 at 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..
ThePhantomUpvoter at 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 at 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!!
ThePhantomUpvoter at 6-Mar-13 10:07am
   
Well what you should do now is to go back and read my comment. I already told you that would happen.
Ankit_Sharma1987 at 6-Mar-13 10:16am
   
ohh yes, i was inserting, these fields from textbox,By metioning parameters..
but...i want all column by linkbutton, which parameters is not assigned, , is'nt it? sir
ThePhantomUpvoter at 6-Mar-13 10:50am
   
You have to fix your errors first. You have a stored procedure that expects 5 parameters and you are only passing one. Do you see a problem with that?
ryanb31 at 6-Mar-13 9:53am
   
I don't understand what you are trying to do. Are you trying to do something in link_Click()?
Ankit_Sharma1987 at 6-Mar-13 10:07am
   
yes, Sir, i Want to..fetch columns, by itemname, I have metioned, in my proceedure., please check
ryanb31 at 6-Mar-13 10:16am
   
I still don't follow. It isn't clear.
Ankit_Sharma1987 at 6-Mar-13 10:20am
   
Firstlly I have, genereated Dynamic Linkbuttons
Secondly, i Fetched, itemname, from menu table,on that linkbutton, ok sir
then, Now i want, values of selected row, on Session and, fetch all values on another page..
thats all i want
ryanb31 at 6-Mar-13 10:23am
   
I think there is a language barrier issue because most of what you said does not make any sense. However, if you want data from SQL, do cm1.ExecuteReader() instead of ExecuteNonQuery. Then you can get values from the db.
Ankit_Sharma1987 at 6-Mar-13 10:29am
   
ok, i am trying, sir...
ThePhantomUpvoter at 6-Mar-13 10:41am
   
Please do not change your original question to something completely different. That throws everything else off. In fact, you was already told that error would occur and how to fix it.
Ankit_Sharma1987 at 6-Mar-13 10:51am
   
Sir, i have one question? may i Ask?
Ankit_Sharma1987 at 6-Mar-13 10:55am
   
If, we have to, Fetch any thing, We use SqlDataAdapter, DataSet, and then write Select Query, But if we use stored proceedure, to fetch, then, what we should use? please make me clear...
ThePhantomUpvoter at 6-Mar-13 11:06am
   
You use a data reader. Have you done any research on this? Read any books? Taken any classes? Maybe you might want to think about it.
ThePhantomUpvoter at 6-Mar-13 11:07am
   
Sure... if only you had just one question
Ankit_Sharma1987 at 6-Mar-13 11:27am
   
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");
}
Now i have used this code to fetch column values..
Ankit_Sharma1987 at 6-Mar-13 11:28am
   
then also, it is passing, error:
Procedure or function 'str_get_subitemDetails' expects parameter '@item_id', which was not supplied.
ThePhantomUpvoter at 6-Mar-13 11:40am
   
I would tell you yet again that you need to pass all of the parameters that your stored procedure is expecting but you just don't seem to listen.
Ankit_Sharma1987 at 6-Mar-13 12:00pm
   
and Sorry sir, i hve been workin in Asp.net plateform, since, 6 months..., sorry...
actually, Your help to make , me explain, but slowly slowly, i am, getting the, point, so thanks for that!!
richcb at 6-Mar-13 11:49am
   
Ok, let me try and explain in a different way what ThePhantomUpVoter has said five times. When using a stored procedure, you have to pass it however many parameters it is expecting. Per your code above, the stored procedure is expecting 5 parameters when you call it. Therefore, when you want to use the stored procedure, the 5 items it is expecting in order to work properly must be supplied to it.
Ankit_Sharma1987 at 6-Mar-13 11:56am
   
yes mean i have to pass item_id , which i have to supply.., but, item_id, which that error is asking...is Autogenerated field, whch i have generated in this proceedure, see, this is the , code:
 
ALTER PROCEDURE [dbo].[str_collection]
@itemname nvarchar(50),
@item_img nvarchar(MAX),
@subitem_name nvarchar(50),
@item_rate decimal(18,2),
@item_descript nvarchar(100)
AS
BEGIN
SET NOCOUNT ON
declare @id nvarchar(50)
SET @id=LEN(@subitem_name)
declare @entrydate as date
declare @item_id nvarchar(50)
SET @entrydate= GETDATE()
SET @item_id=@itemname+@id
INSERT INTO menu(itemname,item_id,item_img,entrydate)values(@itemname,@item_id,@item_img,@entrydate)
INSERT INTO submenu(item_id,item_descript,item_rate,subitem_name)values(@item_id,@item_descript,@item_rate,@subitem_name)
END
Ankit_Sharma1987 at 6-Mar-13 11:58am
   
Actually i want,item_id,item_descript,item_rate,subitem_name these column, which i have inserted in submenu table, by stored proceedure.
richcb at 6-Mar-13 12:05pm
   
You need to do a search on how to use a stored procedure, that will give you an example.
richcb at 6-Mar-13 12:03pm
   
No, you have to supply 5 parameters, not just the item_id.
Ankit_Sharma1987 at 6-Mar-13 12:07pm
   
ok, i have to supply, 5 parameter , ok
this is my storedproceedure code, ok
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
above i have declared, item_id, and all parameters, then what i have change in my code?
richcb at 6-Mar-13 12:11pm
   
What error message are you getting now?
If you are passing the stored procedure all the parameters, it should either work or you should get another error?
Ankit_Sharma1987 at 6-Mar-13 12:15pm
   
yes in sql , when i am, executing, procedure, then, it is giving no errors...
Command Execute Successfully
But, debugging, my c# Code it is, giving error...:
Procedure or function 'str_get_subitemDetails' expects parameter '@item_id', which was not supplied.
ThePhantomUpvoter at 6-Mar-13 14:12pm
   
Okay lets go through this again slowly. You have a stored procedure that requires you to pass it 5 parameters. With the code that you have now, you are passing your stored procedure exactly 0 parameters of which item_id is one of them. If you go and add item_id to your parameter collection, the next error you would get is that you are not passing itemname. You have to pass all of the parameters and values to your stored procedure from your C# code or you will get an error. So go into your code and supply the parameters and values for your stored procedure and the errors will go away. Only then can you go on to do whatever else it is that you are wanting to do next.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Please, check how to add parameters to SqlCommand: SqlParameterCollection.AddWithValue[^].
Hint: You need to pass 5 parameters Wink | ;)
  Permalink  
Comments
Espen Harlinn at 10-Mar-13 18:30pm
   
5'ed!
Maciej Los at 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)

  Print Answers RSS
0 Maciej Los 310
1 OriginalGriff 285
2 Afzaal Ahmad Zeeshan 200
3 Sergey Alexandrovich Kryukov 195
4 BillWoodruff 180
0 OriginalGriff 6,499
1 Sergey Alexandrovich Kryukov 6,048
2 DamithSL 5,193
3 Manas Bhardwaj 4,657
4 Maciej Los 4,120


Advertise | Privacy | Mobile
Web02 | 2.8.1411022.1 | Last Updated 10 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100