Click here to Skip to main content
14,872,533 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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
Updated 12-Jun-12 23:53pm
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.
PROGRAMING TECH TUTORIAL 3-Dec-19 1:04am
   
https://studio.youtube.com/video/Aqoq_5teKtY/edit?utm_campaign=upgrade&utm_medium=redirect&utm_source=%2Fmy_videos

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

Try this:

C#
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
   
Comments
Harsha Dev 13-Jun-12 9:12am
   
thanks...
PROGRAMING TECH TUTORIAL 3-Dec-19 1:05am
   
https://studio.youtube.com/video/Aqoq_5teKtY/edit?utm_campaign=upgrade&utm_medium=redirect&utm_source=%2Fmy_videos
C#
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.
   
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!
PROGRAMING TECH TUTORIAL 3-Dec-19 1:05am
   
https://studio.youtube.com/video/Aqoq_5teKtY/edit?utm_campaign=upgrade&utm_medium=redirect&utm_source=%2Fmy_videos
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);
   
v2
Comments
Harsha Dev 13-Jun-12 9:12am
   
thanks...
PROGRAMING TECH TUTORIAL 3-Dec-19 1:05am
   
https://studio.youtube.com/video/Aqoq_5teKtY/edit?utm_campaign=upgrade&utm_medium=redirect&utm_source=%2Fmy_videos
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:
VB
da.SelectCommand.Parameters.AddWithValue("@flag", 2);
   
Comments
Harsha Dev 13-Jun-12 9:12am
   
thanks...
PROGRAMING TECH TUTORIAL 3-Dec-19 1:05am
   
https://studio.youtube.com/video/Aqoq_5teKtY/edit?utm_campaign=upgrade&utm_medium=redirect&utm_source=%2Fmy_videos
Stored Procedure in GridView
In sql Server
SQL
--=======--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--============


SQL
--=======--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
XML
<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();       
    }
}
   
v2
Comments
PROGRAMING TECH TUTORIAL 3-Dec-19 1:05am
   
https://studio.youtube.com/video/Aqoq_5teKtY/edit?utm_campaign=upgrade&utm_medium=redirect&utm_source=%2Fmy_videos
C#
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();
   
v2
SQL
da.SelectCommand.Parameters.Add(new SqlParameter("@flag", SqlDbType.Int));
        da.SelectCommand.Parameters["@flag"].Value = 2;

Thanks
   

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