Click here to Skip to main content
12,072,432 members (60,373 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# ASP.NET
sc.Open();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter("masteriud",sc);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        SqlParameter param = new SqlParameter("@flag", 2);
        cmd.Parameters.AddWithValue("@flag", 2);
        da.SelectCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int));
        da.SelectCommand.Parameters["@Id"].Value = (txtempid.Text).Trim();
        da.SelectCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar));
        da.SelectCommand.Parameters["@Name"].Value = (txtempname.Text).Trim();
        da.SelectCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar));
        da.SelectCommand.Parameters["@Address"].Value = (txtempaddr.Text).Trim();
        DataSet ds = new DataSet();
        da.Fill(ds, "Memployee");
        GridView1.DataSource = ds.Tables["Memployee"];
        GridView1.DataBind();
        da.Dispose();
        sc.Close();
Error i am getting like this -

Procedure or Function 'masteriud' expects parameter '@flag', which was not supplied.

but i have the parameter but still it is showing error

Please Help!!!
Thanks
Posted 13-Jun-12 0:43am
Edited 13-Jun-12 0:53am
v2
Comments
RDBurmon 13-Jun-12 9:26am
   
Thanks Everyone who replied to this thread , So Harsha , I think you have got enough responses and you should be able to mark it as your answer and close the thread. Please do so.
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 4

Hi,
Look into your code carefully: you will find that you have not at all used command object "cmd"

Try this:

sc.Open();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter("masteriud",sc);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
        //SqlParameter param = new SqlParameter("@flag", 2);
        //cmd.Parameters.AddWithValue("@flag", 2);

        da.SelectCommand.Parameters.Add(new SqlParameter("@flag", SqlDbType.Int));
        da.SelectCommand.Parameters["@flag"].Value = 2;
 

        da.SelectCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int));
        da.SelectCommand.Parameters["@Id"].Value = (txtempid.Text).Trim();
        da.SelectCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar));
        da.SelectCommand.Parameters["@Name"].Value = (txtempname.Text).Trim();
        da.SelectCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar));
        da.SelectCommand.Parameters["@Address"].Value = (txtempaddr.Text).Trim();
        DataSet ds = new DataSet();
        da.Fill(ds, "Memployee");
        GridView1.DataSource = ds.Tables["Memployee"];
        GridView1.DataBind();
        da.Dispose();
        sc.Close();

All the best..
--AK
  Permalink  
Comments
Harsha Dev 13-Jun-12 9:12am
   
thanks...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

SqlParameter param = new SqlParameter("@flag", 2);
        cmd.Parameters.AddWithValue("@flag", 2);
You have created a SQLCommand and assigned parameter @flag to it. You never use this command created. Leaving this parameter, you created other directly for Adapter and used it. Either use SQLCommand defining all the needed parameters for it OR add @flag parameter to adapter too.
  Permalink  
v2
Comments
Harsha Dev 13-Jun-12 9:12am
   
thanks...
Sandeep Mewara 13-Jun-12 9:59am
   
Welcome.
VJ Reddy 13-Jun-12 10:55am
   
Good answer. 5!
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

The issue is cmd object is not assigned to da.SelectCommand.

Simply,Remove the lines
SqlCommand cmd = new SqlCommand();
 cmd.Parameters.AddWithValue("@flag", 2);

Add the line
da.SelectCommand.Parameters.Add(param);
  Permalink  
v2
Comments
Harsha Dev 13-Jun-12 9:12am
   
thanks...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

First thing I would do if I were you is clean it up a bit. You create an object in the variable param, but never use it.

You also use "cmd.Parameters.Add" for the @flag, and "da.SelectCommand.Parameters.Add" for the rest. I suspect it's because you're adding the flag parameter to the cmd object after creating the data adapter.

Try moving the cmd.Parameters.Add to before you create the DataAdapter, or change it to be:
da.SelectCommand.Parameters.AddWithValue("@flag", 2);
  Permalink  
Comments
Harsha Dev 13-Jun-12 9:12am
   
thanks...
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 5

Stored Procedure in GridView
In sql Server
--=======--Create Table--============

create table tb1(
c_id int not null primary key,
c_name varchar(50) not null unique,
c_address varchar(50))
 
create table tb2(
c_id int foreign key references tb1(c_id),
c_name varchar(50) references tb1(c_name) ,
c_mobile int,
c_gender varchar(2))
 
create table tb3(
c_name varchar(50) references tb1(c_name) ,
c_father varchar(200),
c_mother varchar(200))
 
--=======--End Table--============

--=======--stored procedure--============
create procedure data
as
select distinct tb1.c_id,tb1.c_name,tb1.c_address,tb2.c_mobile,tb2.c_gender,tb3.c_father,tb3.c_mother from tb1 left join tb2 on tb1.c_id=tb2.c_id left join tb3 on tb1.c_name=tb3.c_name
go
 
execute data
--=======--END stored procedure--============

In demo.Aspx Page
<asp:GridView ID="GridView1" runat="server">
    </asp:GridView>
In demo.cs Page

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
 
public partial class demo: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=khushbu-hp;Initial Catalog=abc;Integrated Security=True");
        SqlDataAdapter da = new SqlDataAdapter("data", con);
        da.SelectCommand.CommandType= CommandType.StoredProcedure;
        DataSet ds=new DataSet();
        da.Fill(ds, "data");
        GridView1.DataSource = ds.Tables["data"];
        GridView1.DataBind();       
    }
}
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 7

sc.Open();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter("masteriud",sc);
        da.SelectCommand.CommandType = CommandType.StoredProcedure;
     
        da.SelectCommand.Parameters["@flag"].Value = 2;
        da.SelectCommand.Parameters.Add(new SqlParameter("@Id", SqlDbType.Int));
        da.SelectCommand.Parameters["@Id"].Value = (txtempid.Text).Trim();
        da.SelectCommand.Parameters.Add(new SqlParameter("@Name", SqlDbType.VarChar));
        da.SelectCommand.Parameters["@Name"].Value = (txtempname.Text).Trim();
        da.SelectCommand.Parameters.Add(new SqlParameter("@Address", SqlDbType.VarChar));
        da.SelectCommand.Parameters["@Address"].Value = (txtempaddr.Text).Trim();
        DataSet ds = new DataSet();
        da.Fill(ds, "Memployee");
        GridView1.DataSource = ds.Tables["Memployee"];
        GridView1.DataBind();
        da.Dispose();
        sc.Close();
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 8

da.SelectCommand.Parameters.Add(new SqlParameter("@flag", SqlDbType.Int));
        da.SelectCommand.Parameters["@flag"].Value = 2;
Thanks
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web01 | 2.8.160208.1 | Last Updated 28 Jan 2013
Copyright © CodeProject, 1999-2016
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