Click here to Skip to main content
15,917,731 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here i need to read data using the Store procedure
i have create a procedure like this

SQL
create procedure Raghu(@ID int,@Name nvarchar(60),@Location nvarchar(60) )
AS
Begin
select*from Emp;//it is table name
End

like similarly i have created Store Procedures insert,delete,update also
but i want know how to use these store procedures in Asp.net coding in Cs file
for Select it should Display in grid view
For insert command it working but it is not working for the Select , Delete and Update

for select command i have return code like this
C#
SqlConnection sqlConnection1 = new SqlConnection("Data Source=A10;Initial Catalog=raghu;Integrated Security=True");
        SqlCommand cm = new SqlCommand();
        SqlDataAdapter da = new SqlDataAdapter();
        DataTable dt = new DataTable();
        try
        {
            cm = new SqlCommand("Raghu", sqlConnection1);
            cm.Parameters.Add("@ID", SqlDbType.Int);
            cm.Parameters.Add("@Name", SqlDbType.NVarChar);
            cm.Parameters.Add("@Location", SqlDbType.NVarChar);
            cm.CommandType = CommandType.StoredProcedure;
            da.SelectCommand = cm;
            da.Fill(dt);
            GridView1.DataSource = dt;
        }
        catch (Exception x)
        {
            Response.Write("x.Message");
        }
        finally
        {
            cm.Dispose();

        } 
    }

can you give any idea where error in this code
it is giving error like this

Procedure or Function 'Raghu' expects parameter '@ID', which was not supplied.
Posted
Updated 27-Jan-12 1:06am
v2
Comments
Anuja Pawar Indore 27-Jan-12 7:07am    
Added pre tag

Hi Raghu,


As per your Stored Procedure, the Input Parameters are : @ID, @Name, @Location.

You've just done
Parameters.Add()


They need some value don't they?

Try
Parameters.AddWithValue()
 
Share this answer
 
v2
Comments
Rajesh Anuhya 27-Jan-12 7:14am    
pre tags added
--RA
hi,

First thing i wanted to know is why are you passing parameters for a select * statement?

Also, you are adding the parameters.But where are you passing the values?

check this link on how to call the stored procedures.

Calling SQL Server stored procedures from Microsoft.NET[^]


Hope this helps.
 
Share this answer
 
Hm-m-m-m.... I see your the error ;)
C#
cm = new SqlCommand("Raghu", sqlConnection1);
cm.Parameters.Add("@ID", SqlDbType.Int);
cm.Parameters.Add("@Name", SqlDbType.NVarChar);
cm.Parameters.Add("@Location", SqlDbType.NVarChar);
cm.CommandType = CommandType.StoredProcedure;

you add parameters, but where are values for this parameters?
something like this:
C#
cm.Parameters[0] = 1;
cm.Parameters[1] = "myName";
cm.Parameters[2] = "myLocation";

also you are using method
C#
public SqlParameter Add(string parameterName, SqlDbType sqlDbType);

try
C#
public SqlParameter Add(string parameterName, object value);

instead. here is an example:
C#
cm.Parameters.Add("@ID",Convert.ToInt32(1))
 
Share this answer
 

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