RAISERROR nel procedimento (SQL Server 2005) non ricade nel processo di aggiornamento a lato client (C #)

StackOverflow https://stackoverflow.com/questions/1714668

Domanda

Sto usando stored procedure con RAISERROR. L'errore sollevata dal SP non è catturato dal try catture in C #. Sto utilizzando SqlDataSource per la connessione SQL e l'evento SqlDataSource_Updating per aggiornare i dati.

Codice SQL:

DECLARE @count int
@count=0
   if(@count = 0)
   begin
   RAISERROR('Updation failed. record already exists', 16, 1) 
  end

c # Codice:

protected void sqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
  try
  {
    // Assigns the update command parameter
    e.Command.Parameters.Add(new SqlParameter("@fag", Flg));
    e.Command.Parameters.Add(new SqlParameter("@Date", DateTime.Now));

  }
  //catch (SqlException ex)
  //{


  //}
  catch (Exception ex)
  {
    // If so, checks the Exception message
    if (ex.Message == "Updation failed. record Not exists")
    {
        // Display the details of the exception to the user
        lblMessage.Text = " record already exists";
    }

    // Writes the error in log file
    WriteErrorLog(ex.Message , "sqlDataSource_Updating");
  }
}

SQLDataSource:

                SelectCommand="SELECT [ID], [Name], [Flg] FROM [Master] ORDER BY Name "  

                InsertCommand="exec [Master_Insert] @ID, @Name, @Flg, @createdDate, @createdBy, @updatedDate, @updatedBy"

                UpdateCommand="exec [Master_Update] @ID, @Name, @Flg, @updatedDate, @updatedBy"

                 DeleteCommand="DELETE FROM Master WHERE ID = @ID" 
                            oninserted="sqlDataSource_Inserted" 
                            oninserting="sqlDataSource_Inserting" 
                            onupdated="sqlDataSource_Updated" 
                            onupdating="sqlDataSource_Updating"> 
                </asp:sqldatasource>

Saluti Geetha

È stato utile?

Soluzione 3

Grazie per tutte le vostre risposte.

Utilizzando un e.Exception in sqlDataSource_Updated mio problema ottenuto risolto.

protected void sqlDataSource_Updated(object sender, SqlDataSourceStatusEventArgs e)

{

       if (e.Exception != null)
            {

                // If so, checks the Exception message

                if (e.Exception.Message == "record already exists")

                {

                }
         }
}

Altri suggerimenti

Il Aggiornamento evento viene utilizzato per personalizzare l'SqlCommand in esecuzione, ma non esegue il comando. Il blocco try / catch deve avvolgere il luogo effettivo in cui viene eseguito il comando, non il callback personalizzazione.

Una cosa che ho notato subito è che il messaggio di errore che la stored procedure è la raccolta di "Updation non riuscita. Il record esiste già " non corrisponde la stringa di testo ", Updation non riuscita. registrazione non esiste ", che si sta verificando nel blocco try / catch.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top