Click here to Skip to main content
15,887,923 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
SQL
i have created this function with a parameter offset and i want to access the database using stored procedure but the code and stored procedure i have written below is not working...what is the right way to do it??

# public bool isoffsetexist(int offset)
#     {
#         conn.Open();
#  
#         SqlCommand cmd = new SqlCommand();
#         cmd.Connection = conn;
#         cmd.CommandType = CommandType.StoredProcedure;
#         cmd.CommandText = "sp_offset1";
#         SqlDataReader dr = cmd.ExecuteReader();
#  
#         int i = 0;
#         while (dr.Read())
#         {
#             i++;
#         }
#         if (i == 1)
#         {
#             conn.Close();
#             return true;
#         }
#         else
#         {
#             conn.Close();
#             return false;
#         }
#     }


my stored procedure "sp_offset1" is

# CREATE PROCEDURE [dbo].[sp_offset1]
# @offset numeric,
# @appname varchar(20),
# @os varchar(20)
#  AS
# select * from tb_offset
#  where @offset=" + offset + "
Posted

1 solution

Your sp should be like below
SQL
CREATE PROCEDURE [dbo].[sp_offset1]
@offset numeric,
@appname varchar(20),
@os varchar(20)
AS
select * from tb_offset
where offset=@offset

Then you should add parameter in your code
C#
SqlCommand cmd = new SqlCommand();
SqlParameter param;
string offset= txtOffset.text;
param = new SqlParameter("@offset", SqlDbType.Numeric);
param.Direction = ParameterDirection.Input;
param.Value = offset;
cmd.Parameters.Add(param);

Please change your code based on your need.

Then your sp contains 3 parameters, so you should pass values to 3 parameters in your code.(@appname, @os)
 
Share this answer
 
v2

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