Friday, January 17, 2014

“This SqlTransaction has completed; it is no longer usable.”

Look for possible areas where the transaction is being committed twice (or rolled back twice, or rolled back and committed, etc.). Does the .Net code commit the transaction after the SP has already committed it? Does the .Net code roll it back on encountering an error, then attempt to roll it back again in a catch (or finally) clause?
It's possible an error condition was never being hit on the old server, and thus the faulty "double rollback" code was never hit. Maybe now you have a situation where there is some configuration error on the new server, and now the faulty code is getting hit via exception handling.

The following example creates a SqlConnection and a SqlTransaction. It also demonstrates how to use the BeginTransaction, Commit, and Rollback methods. The transaction is rolled back on any error. Try/Catch error handling is used to handle any errors when attempting to commit or roll back the transaction.
private static void ExecuteSqlTransaction(string connectionString)
    using (SqlConnection connection = new SqlConnection(connectionString))

        SqlCommand command = connection.CreateCommand();
        SqlTransaction transaction;

        // Start a local transaction.
        transaction = connection.BeginTransaction("SampleTransaction");

        // Must assign both transaction object and connection 
        // to Command object for a pending local transaction
        command.Connection = connection;
        command.Transaction = transaction;

            command.CommandText =
            "Insert into Region (RegionID, RegionDescription) VALUES (100, 'Description')";
            command.CommandText =
            "Insert into Region (RegionID, RegionDescription) VALUES (101, 'Description')";

            // Attempt to commit the transaction.
            Console.WriteLine("Both records are written to database.");
        catch (Exception ex)
            Console.WriteLine("Commit Exception Type: {0}", ex.GetType());
            Console.WriteLine("  Message: {0}", ex.Message);

            // Attempt to roll back the transaction. 
            catch (Exception ex2)
                // This catch block will handle any errors that may have occurred 
                // on the server that would cause the rollback to fail, such as 
                // a closed connection.
                Console.WriteLine("Rollback Exception Type: {0}", ex2.GetType());
                Console.WriteLine("  Message: {0}", ex2.Message);

Wednesday, January 8, 2014

How to limit the text in a label using c#

Lets say we are having label as below.
<asp:Label CssClass="ShortDesc" Text='<%# Eval("NewsDescription")%">' runat="server""></asp:Label">
By Using CSS
.ShortDesc{ height:50px; Overflow:hidden; }
OR restrict to 100 or N characters
<asp:Label CssClass="ShortDesc" Text='<%# Eval("NewsDescription").ToString().SubString(0,Math.Min(100,Eval("NewsDescription").ToString().Length)) %">' runat="server""></asp:Label">
OR return a Short Desc from your DB SELECT substring(NewsDescription,1,100)+'...' AS ShortNewsDescription, NewsDescription From Jobs
And use that in your repeater <asp:Label CssClass="ShortDesc" Text='<%# Eval("ShortNewsDescription")%">' runat="server""></asp:Label">