Click here to Skip to main content
15,896,111 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have store procedure in Sql server, which has many fields and some of them are initialized in parameters list. So do i need to pass those parameters a value from asp.net or bypass them ? see my code below to get my meaning.

Store Procedure:

SQL
ALTER PROCEDURE [dbo].[ADD_SMS_InQueue]
(
@ToMobNo varchar(15),
@Message nvarchar(2000),

@encoding_Type int=0,
@IsAlertSMS bit=0,

@priority int=2,
@SendByUser int=1,

@MessageType tinyint=0


)
AS
BEGIN TRY
    BEGIN TRANSACTION
        INSERT INTO MessageQueue ([QueueDateTime] ,Client_ID,ToMobileNo,
            [Message],LongMessageType_ID,EncodingType_ID,AlertMessage,LocalSMS,Priority_ID,Attempts,TotalAttempts,Status_ID,SendByUser,MessageType)
        values (GETDATE(),1,@ToMobNo,@Message,3,@encoding_Type,@IsAlertSMS,1,@priority,0,0,1,@SendByUser,@MessageType)


    COMMIT TRANSACTION
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION
END CATCH


C# code for sending vales:
C#
public class MessageQueueBizz
    {
        public string ToMobileNo { get; set; }
        public string Message { get; set; }

        public MessageQueueBizz(string ToMobileNo, string Message) 
        {
            this.ToMobileNo = ToMobileNo;
            this.Message = Message;
        }
    }
    public class ManageQueueBizz 
    {
        public int Insert(MessageQueueBizz MessageQueueBizz)
        {
            SqlCommand cmd = new SqlCommand("ADD_SMS_InQueue", DataBaseConnectionSMSDB.OpenConnection());
            cmd.CommandType = CommandType.StoredProcedure;
            SqlParameter pToMobileNo = new SqlParameter("@ToMobNo", MessageQueueBizz.ToMobileNo);
            cmd.Parameters.Add(pToMobileNo);
            SqlParameter pMessage = new SqlParameter("@Message", MessageQueueBizz.Message);
            cmd.Parameters.Add(pMessage);
            int result = Convert.ToInt32(cmd.ExecuteNonQuery());
            DataBaseConnectionSMSDB.CloseConnection();
            return result;
        }
Posted
Comments
Kornfeld Eliyahu Peter 20-May-14 6:05am    
Nice code, but what is your question?
Thanks7872 20-May-14 6:10am    
He already stated that see the code to get the meaning....:laugh:
DamithSL 20-May-14 6:17am    
haven't you run and test yet? :-)

Your procedure has 7 parameters, of which 5 have default values. You do not have to provide parameter values foe those - although you can if you want a specific value instead of the default.

The only two parameters you must send are ToMobNo and Message which do not have default values. If you do not supply these, you will get an error.

However, you can probably make your code a lot cleaner:
C#
SqlCommand cmd = new SqlCommand("ADD_SMS_InQueue", DataBaseConnectionSMSDB.OpenConnection());
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@ToMobNo", MessageQueueBizz.ToMobileNo);
cmd.Parameters.AddWithValue("@Message", MessageQueueBizz.Message);
int result = cmd.ExecuteNonQuery();
 
Share this answer
 
Comments
[no name] 20-May-14 6:16am    
nice suggestion
You do not have to add parameters with default values in stored procedures to the parameter list for execution.

But if you do add a parameter to the parameters list then you must assign a value.

If you leave the value as a C# null then it will throw an exception.

If you want to pass a null value to the stored procedure you have to use DBNull.Value
 
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