Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
my store procedure is

SQL
CREATE PROCEDURE [dbo].[Usp_Insert_Data_In_Student_Detial_Table]
(
	@ROLLNUMBER int,
	@NAME varchar(50)
)

AS
		IF EXISTS (SELECT '#' FROM Student_Detail WHERE NAME = @NAME)
		BEGIN
				SELECT 'SORRY'
		END

		ELSE

		
		BEGIN
				
				INSERT INTO Student_Detail
					
						(
							ROLLNUMBER,
							NAME
						)
				VALUES
				
						(
							@ROLLNUMBER,
							@NAME
						)
				
		END


my save button code is

C#
private void Sumbit_Button_Click(object sender, EventArgs e)
        {
            using (SqlConnection con = new SqlConnection(cs))
            {
                using (SqlCommand cmd = new SqlCommand("Usp_Insert_Data_In_Student_Detial_Table", con))
                {
                    cmd.Parameters.AddWithValue("@ROLLNUMBER", rollNumberTextBox.Text);
                    cmd.Parameters.AddWithValue("@NAME", nameTextBox.Text);
                    cmd.CommandType = CommandType.StoredProcedure;
                    con.Open();
                    int a = cmd.ExecuteNonQuery();
                    if(a>0)
                    {
                        MessageBox.Show("DATA SAVE SUCESSSFULLY");

                    }

                    
                }
            }

        }


my problem is how to show message box when record already exits

What I have tried:

i have search many posts for resolve this problem but cant find it. show please help me
Posted
Updated 27-Apr-23 5:17am
v2
Comments
GKP1992 26-Apr-23 4:02am    
You should refer to C# manual about how an if-else block works and what it can be used for. Try to see if it can be applied to your problem.
kalpeshdelta 26-Apr-23 9:08am    
where add the two line code

Firstly "Detail" is spelled incrorrectly: "Usp_Insert_Data_In_Student_Detial_Table" is wrong - but that's minor.

Check the return value: Instead of "SORRY", I'd return a the number of rows inserted: 0 for "It exists" and 1 for "Inserted"
SQL
IF EXISTS ...
   BEGIN
   SELECT 0
   END
ELSE
   BEGIN
   ...
   SELECT 1
   END
Then your C# code just needs an else clause.
 
Share this answer
 
Comments
kalpeshdelta 26-Apr-23 1:38am    
but what is code i write to show message box "record already exits when button click event
OriginalGriff 26-Apr-23 2:09am    
You are kidding, right?
You wrote the C# code and you can't modify it to add two lines of code, one of which is a single word, and the other is nearly identical to teh code you have already?

Is this a trolling session?
GKP1992 26-Apr-23 4:03am    
He could just be that dense.
You can try to change your SP with IF NOT EXIST:

CREATE PROCEDURE [dbo].[Usp_Insert_Data_In_Student_Detial_Table]
(
	@ROLLNUMBER int,
	@NAME varchar(50)
)

AS
		IF NOT EXISTS (SELECT '#' FROM Student_Detail WHERE NAME = @NAME)
		BEGIN
				
				
				INSERT INTO Student_Detail
					
						(
							ROLLNUMBER,
							NAME
						)
				VALUES
				
						(
							@ROLLNUMBER,
							@NAME
						)

             select 1 as DataExist
				
		END
        ELSE
		BEGIN
              select 0 as DataExist
		END


If result DataExistis 0, show message box That data already exist

OR You can separate the query between checking duplicate and save operation something like this:
string sQuery = string.Empty;
            try
            {
				int iTotal = 0;
				int iResultSave = 0;
                sQuery = @"SELECT count(*) TOTAL FROM Student_Detail WHERE NAME = @NAME";


                using (SqlConnection con = new SqlConnection(CommonConst.Conn_String_MS_SQL_SERVER))
                {
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand(sQuery, con))
                    {
						cmd.Parameters.AddWithValue("@NAME", nameTextBox.Text);
						using (SqlDataReader dr = cmd.ExecuteReader())
                        {
                            if (dr != null)
                            {
                                while (dr.Read())
                                {
									iTotal = Convert.ToInt32(dr["TOTAL "]);
                                }
                            }
                        } // reader closed and disposed up here
                    } // command disposed here
                } //connection closed and disposed here
				
				
				if(iTotal <= 0){
				
                sQuery = @"INSERT INTO Student_Detail
					
						(
							ROLLNUMBER,
							NAME
						)
				VALUES
				
						(
							@ROLLNUMBER,
							@NAME
						)";
				 using (SqlConnection con = new SqlConnection(CommonConst.Conn_String_MS_SQL_SERVER))
                {
                    con.Open();
                    using (SqlCommand cmd = new SqlCommand(sQuery, con))
                    {                   
						cmd.Parameters.AddWithValue("@ROLLNUMBER", rollNumberTextBox.Text);
						cmd.Parameters.AddWithValue("@NAME", nameTextBox.Text);
                        iResultSave = cmd.ExecuteNonQuery();
                    } // command disposed here
                } //connection closed and disposed here

				}else{                        
						MessageBox.Show("Data Already Exist");	
					}
				
				} catch(Exception ex){
					//insert log sned message
				}
 
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