Custom Validation question

I have a Modal Popup dialog which contains a FileUpload control and an Import button. In case of import failure, I want the Modal Popup not to close, but stay open and display an error message. I guess I should use a custom validator for that, but not sure if it is a good idea to place my Import functionality inside of the custom control validator handler.
I would appreciate any suggestions.
Thanks.
tantormediaAsked:
Who is Participating?
 
masterpassConnect With a Mentor Commented:
Some modifications

Page.ClientScript.RegisterStartupScript(this.GetType(), "openMP", "ShowMPE();", true);

function ShowMPE()
{
setTimeout("$find('MyMPE').show();",1000);
}
0
 
masterpassCommented:
Custom validation is one of the best method to do it ... But you don't have to put your logic inside the custom validator for checking that ... Can you just elaborate on what your import function does ... If possible the code too !!!
0
 
tantormediaAuthor Commented:
The function calls ImportExcelToArray() that reads data from Excel file into a two-dimensional array and then passes that data as parameters for a stored procedure that inserts records into a database table or updates existing records.  
protected void ImportFile()
    {
        OdbcConnection odbcConnection = null;

        string[,] sheetEntries = ImportExcelToArray(excelRawColumns, false);

        if (sheetEntries == null)
            return;

        int inserted = 0;
        int updated = 0;
        int rowCounter = 0;
        OdbcDataReader odbcReader = null;
        OdbcCommand odbcCommand = null;
        try
        {
            String odbcConnString = WebConfigurationManager.ConnectionStrings["BookList"].ToString();
            odbcConnection = new OdbcConnection(odbcConnString);

            odbcCommand = new OdbcCommand("{ CALL InsertBookQuest(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) }", odbcConnection);

            odbcCommand.CommandType = CommandType.StoredProcedure;

            for (rowCounter = 0; rowCounter < sheetEntries.GetLength(0); rowCounter++)
            {
                #region Set Parameters
                odbcCommand.Parameters.Clear();

                OdbcParameter odbcParameter = odbcCommand.Parameters.Add("pEAN", OdbcType.VarChar, 13);
                odbcParameter.Value = sheetEntries[rowCounter, 0] == "" ? (object)DBNull.Value : sheetEntries[rowCounter, 0];
                odbcParameter = odbcCommand.Parameters.Add("pTitle", OdbcType.VarChar, 255);
                odbcParameter.Value = sheetEntries[rowCounter, 1] == "" ? (object)DBNull.Value : sheetEntries[rowCounter, 1];
                odbcParameter = odbcCommand.Parameters.Add("pRespParty", OdbcType.VarChar, 255);
                odbcParameter.Value = sheetEntries[rowCounter, 2] == "" ? (object)DBNull.Value : sheetEntries[rowCounter, 2];
                odbcParameter = odbcCommand.Parameters.Add("pPublisher", OdbcType.VarChar, 255);
                odbcParameter.Value = sheetEntries[rowCounter, 3] == "" ? (object)DBNull.Value : sheetEntries[rowCounter, 3];
                odbcParameter = odbcCommand.Parameters.Add("pPubDate", OdbcType.SmallDateTime);
                odbcParameter.Value = sheetEntries[rowCounter, 4] == "" ? (object)DBNull.Value : Convert.ToDateTime(sheetEntries[rowCounter, 4]);
                odbcParameter = odbcCommand.Parameters.Add("pSubject", OdbcType.VarChar, 255);
                odbcParameter.Value = sheetEntries[rowCounter, 5] == "" ? (object)DBNull.Value : sheetEntries[rowCounter, 5];
                odbcParameter = odbcCommand.Parameters.Add("pBinding", OdbcType.VarChar, 45);
                odbcParameter.Value = sheetEntries[rowCounter, 6] == "" ? (object)DBNull.Value : sheetEntries[rowCounter, 6];
                odbcParameter = odbcCommand.Parameters.Add("pPageCount", OdbcType.Int);
                odbcParameter.Value = sheetEntries[rowCounter, 7] == "" ? (object)DBNull.Value : Convert.ToInt32(sheetEntries[rowCounter, 7]);
                odbcParameter = odbcCommand.Parameters.Add("pUSListPrice", OdbcType.Double);
                odbcParameter.Value = sheetEntries[rowCounter, 8] == "" ? (object)DBNull.Value : Convert.ToDouble(sheetEntries[rowCounter, 8]);
                odbcParameter = odbcCommand.Parameters.Add("pDemand", OdbcType.Int);
                odbcParameter.Value = sheetEntries[rowCounter, 9] == "" ? (object)DBNull.Value : Convert.ToInt32(sheetEntries[rowCounter, 9]);
                odbcParameter = odbcCommand.Parameters.Add("pOnOrder", OdbcType.Int);
                odbcParameter.Value = sheetEntries[rowCounter, 10] == "" ? (object)DBNull.Value : Convert.ToInt32(sheetEntries[rowCounter, 10]);
                odbcParameter = odbcCommand.Parameters.Add("pOnBackorder", OdbcType.Int);
                odbcParameter.Value = sheetEntries[rowCounter, 11] == "" ? (object)DBNull.Value : Convert.ToInt32(sheetEntries[rowCounter, 11]);
                odbcParameter = odbcCommand.Parameters.Add("pSpeedStock", OdbcType.Bit);
                if (sheetEntries[rowCounter, 12] == "N" || sheetEntries[rowCounter, 12] == "n")
                    sheetEntries[rowCounter, 12] = "0";
                else if (sheetEntries[rowCounter, 12] == "Y" || sheetEntries[rowCounter, 12] == "y")
                    sheetEntries[rowCounter, 12] = "1";
                odbcParameter.Value = sheetEntries[rowCounter, 12] == "" ? (object)DBNull.Value : Convert.ToInt32(sheetEntries[rowCounter, 12]);
                odbcParameter = odbcCommand.Parameters.Add("pStatus", OdbcType.VarChar, 45);
                odbcParameter.Value = sheetEntries[rowCounter, 13] == "" ? (object)DBNull.Value : sheetEntries[rowCounter, 13];
                odbcParameter = odbcCommand.Parameters.Add("pBTKey", OdbcType.Int);
                odbcParameter.Value = sheetEntries[rowCounter, 14] == "" ? (object)DBNull.Value : Convert.ToInt32(sheetEntries[rowCounter, 14]);
                odbcParameter = odbcCommand.Parameters.Add("pBTFirstPrinting", OdbcType.Int);
                odbcParameter.Value = sheetEntries[rowCounter, 15] == "" ? (object)DBNull.Value : Convert.ToInt32(sheetEntries[rowCounter, 15]);

                #endregion

                if (odbcConnection.State == ConnectionState.Closed)
                    odbcConnection.Open();

                // we use ExecuteReader() and not ExecuteNonQuery() because output parameter doesn't work (probably MySql bug?) and we return IsInsert in a result set instead
                odbcReader = odbcCommand.ExecuteReader();

                if (odbcReader.Read())
                {
                    if (Convert.ToBoolean(odbcReader["IsInsert"]) == true)
                        inserted++;
                    else
                        updated++;
                }
                if (odbcConnection != null && odbcConnection.State == ConnectionState.Open)
                    odbcConnection.Close();

                if (odbcReader != null && !odbcReader.IsClosed)
                {
                    odbcReader.Close();
                    odbcReader = null;
                }
            }
            string msg = inserted + " record(s) inserted and " + updated + " record(s) updated successfully.";
            Response.Redirect(Request.Path + "?success_msg=" + msg);
        }
        catch (OdbcException ex)
        {
            SetWarningLabel("Error processing line " + (rowCounter + 1) /* + 1 because rowCounter is 0-based */  + ".<br>" + ex.Message + "<br>" + inserted + " record(s) inserted and " + updated + " record(s) updated.");
        }
        catch (Exception ex)
        {
            SetWarningLabel("Error processing line " + (rowCounter + 1) /* + 1 because rowCounter is 0-based */  + ".<br>" + ex.Message + "<br>" + inserted + " record(s) inserted and " + updated + " record(s) updated.");
        }
        finally
        {
            if (odbcReader != null && !odbcReader.IsClosed)
                odbcReader.Close();
            odbcCommand.Dispose();
            odbcCommand = null;
            if (odbcConnection != null && odbcConnection.State == ConnectionState.Open)
            {
                odbcConnection.Close();
                odbcConnection = null;
            }
        }
    }

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
masterpassCommented:
In this case, a validator is out of question .. what you can do is keep the modalpopup open when there is some error ....

first set behaviorid for the modalpopup .. go to the modalpopup tag and add this

BehaviourID="MyMPE"

now when you have errors ie, inside the catch have this code

Page.ClientScript.RegisterStartupScript(this.GetType(), "openMP", "$find('MyMP').show();", true);

set some text for the errors and you will be back on track
0
 
masterpassCommented:
oops .... modify the startup script like this

Page.ClientScript.RegisterStartupScript(this.GetType(), "openMP", "$find('MyMPE').show();", true);
0
 
tantormediaAuthor Commented:
Where should I set text for the errors?
0
 
masterpassCommented:
inside the catch block .... have a label like lblErrors

set lblErrors.Text ="Import failed";
0
 
tantormediaAuthor Commented:
Hmm...
Something doesn't work.
The modal popup closes, and the browser reports "$find("ShowImportErrors") is null"
Page.ClientScript.RegisterStartupScript(this.GetType(), "openMP", "$find('ShowImportErrors').show();", true);
            lblError.Text = "Error processing line " + (rowCounter + 1) /* + 1 because rowCounter is 0-based */  + ".<br>" + ex.Message + "<br>" + inserted + " record(s) inserted and " + updated + " record(s) updated.";
            lblError.Visible = true;

Open in new window

0
 
masterpassCommented:
you should set BehaviourID="ShowImportErrors" in the modal popup tag
0
 
tantormediaAuthor Commented:
I did:
    <ajaxToolkit:ModalPopupExtender ID="ImportModalPopupExtender"      runat="server" TargetControlID="btnShowImportPopup"      PopupControlID="pnlImport"      BackgroundCssClass="modalBackground" DropShadow="true" CancelControlID="btnCancelImport" BehaviorID="ShowImportErrors" >
    </ajaxToolkit:ModalPopupExtender>     

Open in new window

0
 
tantormediaAuthor Commented:
Thanks, it worked!
0
 
masterpassCommented:
thanks for the points mate ....

Just for your info : The panel was not rendered at the time of script execution ... so setting a timeout will force the script to execute the script after 1000ms ...
0
 
tantormediaAuthor Commented:
That was wise!
0
All Courses

From novice to tech pro — start learning today.