consuming SAP web service from web app part 2

CL-IS
CL-IS used Ask the Experts™
on
please refer to "consuming SAP web service from web app part 1".

If i enter a value click submit to go SAP and if the value doesnt exist the my code for the consumed web service falls over and i need to get it to stop doing this, if value doenst exist then it should return nothing and go back to my web form with no data.

Consumed web service code...snippet


WebMethod
        public sap.r3dev.Aufm ZGetMatDocs(string job)
        {
            ConnectionStringSettings wtcn = ConfigurationManager.ConnectionStrings["WTCn"];
            SqlConnection cnn = new SqlConnection(wtcn.ConnectionString);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cnn;

            sap.r3dev.service MySAPService = new sap.r3dev.service();
            sap.r3dev.ZGetMatDocs MySAPParameters = new sap.r3dev.ZGetMatDocs();
            sap.r3dev.ZGetMatDocsResponse MySAPResults;
            sap.r3dev.Aufm[] MySAPServiceDetails = new sap.r3dev.Aufm[0];

            System.Net.NetworkCredential cred = new System.Net.NetworkCredential();
            cred.UserName = "TEST";
            cred.Password = "TEST";

            MySAPService.Credentials = cred;

            MySAPParameters.Docyear = "2004";
            MySAPParameters.Movementtype = "261";
            MySAPParameters.Workorder = job;
            MySAPParameters.Details = MySAPServiceDetails;
            MySAPResults = MySAPService.ZGetMatDocs(MySAPParameters);

            int i;
            string Workorder;
            string Docyear;
            string Movementtype;
            string BatchNumber;
            string Quantity;
            string MaterialNumber;
            string errorMessage = "";

            string sql = "INSERT INTO wp4tbl_TblCompVerifySapBatch (Workorder,Docyear,movementtype,BatchNumber,Qty,MaterialNumber) VALUES (@Workorder,@Docyear,@movementtype,@BatchNumber,@Qty,@MaterialNumber)";
            SqlCommand myCommand;

            try
            {
                cnn.Open();
            }
            catch (SqlException SQLex)
            {
                errorMessage = SQLex.Message;
            }
            catch (Exception ex)
            {
                errorMessage = ex.Message;
            }

            myCommand = cnn.CreateCommand();
            myCommand.CommandText = sql;

            for (i = 0; (i <= (MySAPResults.Details.Length - 1)); i++)
            {
                Workorder = MySAPResults.Details i.Aufnr; //removed bracket bcoz EE doesnt like it when trying to submit
                Docyear = MySAPResults.Details i.Mjahr;
                Movementtype = MySAPResults.Details i.Bwart;
                BatchNumber = MySAPResults.Details i.Charg;
                Quantity = Convert.ToString(MySAPResults.Details i.Menge);
                MaterialNumber = MySAPResults.Details i.Matnr;
                myCommand.Parameters.Add(new SqlParameter("@Workorder", Workorder));
                myCommand.Parameters.Add(new SqlParameter("@Docyear", Docyear));
                myCommand.Parameters.Add(new SqlParameter("@movementtype", Movementtype));
                myCommand.Parameters.Add(new SqlParameter("@BatchNumber", BatchNumber));
                myCommand.Parameters.Add(new SqlParameter("@Qty", Quantity));
                myCommand.Parameters.Add(new SqlParameter("@MaterialNumber", MaterialNumber));
                myCommand.ExecuteNonQuery();
                myCommand.Parameters.Clear();
            }
            cnn.Close();            
            return MySAPResults.Details 0; //IF NO DATA FOR THAT VALUE IT FALL OVER HERE!!!
        }
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Author

Commented:
for (i = 0; (i <= (MySAPResults.Details.Length - 1)); i++)
            {
                Workorder = MySAPResults.Details[i].Aufnr;
                Docyear = MySAPResults.Details[i].Mjahr;
                Movementtype = MySAPResults.Details[i].Bwart;
                BatchNumber = MySAPResults.Details[i].Charg;
                Quantity = Convert.ToString(MySAPResults.Details[i].Menge);
                MaterialNumber = MySAPResults.Details[i].Matnr;
                myCommand.Parameters.Add(new SqlParameter("@Workorder", Workorder));
                myCommand.Parameters.Add(new SqlParameter("@Docyear", Docyear));
                myCommand.Parameters.Add(new SqlParameter("@movementtype", Movementtype));
                myCommand.Parameters.Add(new SqlParameter("@BatchNumber", BatchNumber));
                myCommand.Parameters.Add(new SqlParameter("@Qty", Quantity));
                myCommand.Parameters.Add(new SqlParameter("@MaterialNumber", MaterialNumber));
                myCommand.ExecuteNonQuery();
                myCommand.Parameters.Clear();
            }
            cnn.Close();
            return MySAPResults.Details[0];

Author

Commented:
the error i get is - "Index was outside the bounds of the array."
Commented:
At that last line, do this;
if (MySAPResults.Details.Count == 0) {
     return null;
} else {
     return MySAPResults.Details[0];
}

Author

Commented:
if (MySAPResults.Details.length == 0) {
     return null;
} else {
     return MySAPResults.Details[0];
}

Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial