none
problems with sql exception message RRS feed

  • Question

  • bool band = false;
                try
                {
                    conn.Open();
                }
                catch (SqlException)
                {
                    MessageBox.Show("no hay conexion");
                    band = true;
                }
                finally
                {
                    conn.Close();
                }
                if (band != true)
                {
                    SqlDataAdapter da = new SqlDataAdapter(consul, conn);
                    da.Fill(ds, "table");
                    //metodo para meter la consulta de cantidad en la datatable
                    ds.Tables["table"].Columns.Add("cantidad");conn.Open();
                    SqlCommand cmd = new SqlCommand(consul1, conn);
                    if (cmd.ExecuteScalar() != null)
                    {
                        ds.Tables["table"].Rows[0].SetField("cantidad", cmd.ExecuteScalar().ToString());
                    }
                    //metodo para meter la consulta de surtido en la datatable
                    ds.Tables["table"].Columns.Add("surtido");
                    cmd = new SqlCommand(consul2, conn);
                    if (cmd.ExecuteScalar() != null)
                    {
                        ds.Tables["cliemb"].Rows[0].SetField("surtido", cmd.ExecuteScalar().ToString());
                    }
    
                    conn.Close();
                }
                else
                {
                    tex.Select(0, tex.Text.Length);
                }

    this is my code ,and i have a problem because i recive a message that make my program crash, the problem is that i used this program in a mobile device so when i lose the connection cause the wifi signal is low my program enter to the catch method send me the message box but after that says that there is an error and the aplication close itself, i want to handele that error and dont close the aplication....help helppp

    Thursday, October 17, 2013 5:43 PM

Answers

  • Put *all* your code in the "try", not just the conn.Open(). If you're sometimes losing your connection due to a poor wifi signal, you'll have to have some kind of re-try loop that waits for your connection to come back again. In any case, it *all* needs to be within a "try", so that you can catch the error and deal with it there.

    ~~Bonnie Berent DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Piteko B Monday, October 21, 2013 3:02 PM
    Friday, October 18, 2013 1:26 PM

All replies

  • Hello Piteko,

    Welcome to this forum.

    From your description, it seems that it throws an error in the catch again.

    My suggestion for this is that we should to debug the program to see the error message.

    With the error message, we then could find a solution it.

    So could you please share the error message?

    Regards.


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, October 18, 2013 6:47 AM
    Moderator
  • Put *all* your code in the "try", not just the conn.Open(). If you're sometimes losing your connection due to a poor wifi signal, you'll have to have some kind of re-try loop that waits for your connection to come back again. In any case, it *all* needs to be within a "try", so that you can catch the error and deal with it there.

    ~~Bonnie Berent DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    • Marked as answer by Piteko B Monday, October 21, 2013 3:02 PM
    Friday, October 18, 2013 1:26 PM