Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to check whether value already exists or not in database when inserting new record in sql server 2008
    primarykey
    data
    text
    <p>I have created a stored procedure for selecting value from the table if that value already exists then it will update that value but if not exists then it will insert new value. </p> <p>I used stored procedure for that as follows :</p> <pre><code>Create PROCEDURE [dbo].[sp_Insert] ( @Tid int = NULL, @Quid int = NULL, @Selected int = NULL, @Score int = NULL ) AS DECLARE @AlreadyExisted BIT SET @AlreadyExisted = 0 IF (EXISTS (SELECT Quid FROM Sel_Ans) BEGIN SET @AlreadyExisted = 1 UPDATE Sel_Ans SET Selected=@Selected WHERE Quid = @Quid END ELSE BEGIN INSERT INTO Sel_Ans (Quid, Selected) VALUES (@Quid, @Selected) END RETURN </code></pre> <p>And in C# (Asp.net) I used this code but its giving error "Error connecting to the database" as its going in try block. Suggest me any changes to my code to work correctly.</p> <pre><code>//session["Counter"] is my counter that will give next record in next click event if (Convert.ToInt32(Session["Counter"]) &lt;= 7) { int t = Convert.ToInt32(Session["Counter"]); SqlCommand MyCmd = new SqlCommand("sp_Insert", sqlconn); MyCmd.CommandType = CommandType.StoredProcedure; SqlParameter objParam1 = null; SqlParameter objParam2 = null; SqlParameter objParam3 = null; SqlParameter objParam4 = null; objParam1 = MyCmd.Parameters.Add("@Tid", SqlDbType.Int); objParam1.Value = '1'; objParam2 = MyCmd.Parameters.Add("@Quid", SqlDbType.Int); objParam2.Value = a[t - 2]; objParam3 = MyCmd.Parameters.Add("@Selected", SqlDbType.Int); objParam3.Value = rb; objParam4 = MyCmd.Parameters.Add("@Score", SqlDbType.Int); objParam4.Value = '1'; try { if (sqlconn.State == ConnectionState.Closed) { sqlconn.Open(); MyCmd.ExecuteNonQuery(); } } catch (Exception ex) { Response.Write("Error Connecting to Database!"); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload