Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all

i have a Stored Procedure That For Save Record In Vote Table
(i want if vote code Dublicate or Customer Code is not exists Stored procedure Send A number To C# And In C# I Use From Returned Stored Procedure Value )

SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go




ALTER PROCEDURE [dbo].[Votes_Insert]
    (
    @cus_id                     [bigint],
    @vote_id                        [bigint],
    @vote_comment                       [nvarchar](500)
    )
AS

if exists (select * from Votes where vote_id=@vote_id)
return '3'

if not exists (select * from Customers where cus_id=@cus_id)
return '2'


INSERT INTO Votes
    (
    cus_id,
    vote_id,
    vote_comment
    )
VALUES
    (
    @cus_id,
    @vote_id,
    @vote_comment
    )
return '1'


i use this code in csharp to use it :

C#
private void btn_Save_Click(object sender, EventArgs e)
        {
            try
            {


                if (txt_cus_code.Text == "" || txt_vote_code.Text == "" || txt_vote_body .Text == "")
                {
                    btn_Result.Text = "Please Enter All Information";
                    btn_Result.ForeColor = Color.Red;
                    btn_Result.PulseSpeed = 100;
                    btn_Result.Pulse(14);
                    btn_Result.Text = "";
                }

                else
                {

                    SqlConnection MySqlConnection = new SqlConnection(ConnectionMain.strconnection);
                    SqlCommand MySqlCommand = MySqlConnection.CreateCommand();
                    MySqlCommand.CommandText = "Votes_Insert";
                    MySqlCommand.CommandType = CommandType.StoredProcedure;

                    MySqlCommand.Parameters.Add("@cus_id", SqlDbType.BigInt, 8).Value = Convert.ToInt64(txt_cus_code .Text);
                    MySqlCommand.Parameters.Add("@vote_id", SqlDbType.BigInt , 8).Value = Convert.ToInt64(txt_vote_code.Text);
                    MySqlCommand.Parameters.Add("@vote_comment", SqlDbType.NVarChar, 500).Value = txt_vote_body .Text;
                   
                    if (MySqlConnection.State != ConnectionState.Open)
                    {
                        MySqlConnection.Open();
                    }

                    int result = MySqlCommand.ExecuteNonQuery();

                    if (MySqlConnection.State != ConnectionState.Closed)
                    {
                        MySqlConnection.Close();
                    }
                    if (result == 1)
                    {
                        btn_Result.Text = "inforation saved successfull ...";
                        btn_Result.ForeColor = Color.Green;
                        btn_Result.PulseSpeed = 100;
                        btn_Result.Pulse(14);
                        btn_Result.Text = "";
                    }
                    else if (result ==3)
                    {
                        btn_Result.Text = "vote code is dublicate";
                        btn_Result.ForeColor = Color.Red;
                        btn_Result.PulseSpeed = 100;
                        btn_Result.Pulse(14);
                        btn_Result.Text = "";

                    }
                    else if (result == 2)
                    {
                        btn_Result.Text = "customer not exists";
                        btn_Result.ForeColor = Color.Red;
                        btn_Result.PulseSpeed = 100;
                        btn_Result.Pulse(14);
                        btn_Result.Text = "";

                    }
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(error.ToString());
            }

            vote_Load (this, e);
      }


This Codes Save Record , But When I Enter Duplicate Vote Id Dont Show Any Message

What I Do ?

Please Help Me

Thanks A Lot
Posted
Updated 28-Mar-12 6:50am
v2
Comments
[no name] 28-Mar-12 13:33pm    
Have to verified the IF EXISTS parts work outside of the method? Have you debugged the stored procedure?

First there is the discrepancy in type between the return value of the SP and the one expected type in the switch statement; the last expects an int and the SP return a char (return '3').

Furthermore the ExecuteNonQuery() method returns by definition the number of rows effected. Therefore the save works correctly.

To solve it you must change the return value of the SP eq. return 3 in stead of return '3' and add a parameter to retrieve the return value of the SP.


// Return value as parameter
SqlParameter returnValue = new SqlParameter("returnVal", SqlDbType.Int);
returnValue.Direction = ParameterDirection.ReturnValue;
MySqlCommand.Parameters.Add(returnValue);
.
.
.
// Execute the stored procedure
MySqlCommand.ExecuteNonQuery();
.
.
.
int result =  Convert.ToInt32(returnValue.Value);


Regards
Piet
 
Share this answer
 
v2
Not a direct answer but some tips

Rather than this
C#
if (MySqlConnection.State != ConnectionState.Closed)
{
  MySqlConnection.Close();
}


You should be making use of using block

using(SqlConnection conn = new SqlConnection(...))
{

}


This will ensure the connection object closed and disposed of when the block is exited, even if an exception occurs.

It is more performant to use

txt_cus_code.Length == 0

rather then this
txt_cus_code.Text == ""
 
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