0

I have got below code in c#.

SqlConnection conn = new SqlConnection("Data Source=MANOJ-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=False;User Id=sa;Password=Manoj;");
conn.Open();

if (conn != null)
{
    //create command
    SqlCommand cmd = new SqlCommand("dbo.GETTridionLinkData", conn);
    cmd.Parameters.AddWithValue("@PageID", "637518");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandTimeout = 500;
    StringBuilder sbXML = new StringBuilder();
    //Adding Root node
    sbXML.Append("<TridionLinks>");

    //Reading all the values of Stored procedure return
    using (XmlReader reader = cmd.ExecuteXmlReader())
    {
        while (reader.Read())
        {
            sbXML.Append(reader.ReadOuterXml().Replace("//", "/"));
        }
    }

    //Closing the root node tag
    sbXML.Append("</TridionLinks>");
    XmlDocument xDoc = new XmlDocument();

    //Loading string xml in XML Document
    xDoc.LoadXml(sbXML.ToString());
}

In above code you can see that, I have set the cmd.CommandTimeout = 500;, now I want to give user an error message if the timeout is more than this or you can say database is down.

Please suggest!!

1 Answer 1

1

Please refer to How to catch SQLServer timeout exceptions

The question has already been answered..

To improve coding, you can use

    try{

         using (sqlconnection Conn = new SqlConnection("Data Source=MANOJ-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=False;User Id=sa;Password=Manoj;"){
            ...
        }
    }catch(sqlException ex){
       if (ex.Number == -2) {
        //return your message to the control or display the error
       }
    }

well just an example..

Sign up to request clarification or add additional context in comments.

1 Comment

thanks, I saw your posted link, cmd.ExecuteNonQuery(); // This line will timeout. is used there, However I am using cmd.CommandTimeout = 500; as well as I am using cmd.ExecuteXmlReader() instead of cmd.ExecuteNonQuery(); so same exceptions handling will be done here also or something else needs to be written

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.