Receiving a DATATable by parameter and storing the Information in SQL

Hello,

I'm creating an webservice with an Method to receive a datatable. After that i want to loop the datatable to save the information in SQL trough an Store Procedure.

So far this is what i have done: view attached code.

Now, the problem apparently is with the Webservice, it's not saving the information in the Database. I tested the store procedure and is working good. But when i send the parameters to the Webservice i does not work.

Can anyone help me with this?

Thanks,

Edward
[WebMethod]
        public Boolean SetConciliationResume(int OrdersQTY, DateTime ConciliationDate, DataTable Dt)
        {
            //DataTable Dt = ListaData.Tables["Ivr_ConciliationDetails_data"];
            string strSQLSelect2 = "[SP_GetWMOrdersConciliation @TotalOrders='" + OrdersQTY.ToString() + "',@ConciliationDate='" + ConciliationDate.ToString() + "']";
            OleDbConnection databaseConnection = new OleDbConnection(oleDBConnectionString);
            OleDbCommand selectCommand = new OleDbCommand(strSQLSelect2, databaseConnection);
            OleDbDataAdapter dsCmd = new OleDbDataAdapter();

            selectCommand.CommandType = CommandType.StoredProcedure;
            // Establish database connection 
            databaseConnection.Open();
            // Execute SQL Command 
            dsCmd.SelectCommand = selectCommand;
            databaseConnection.Close();

            try
            {
                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    DataRow DTR = Dt.Rows[i];

                    string strSQLSelect = "[SP_GetWMConciliationDetail @confno='" + DTR["Confno"].ToString() + "', @storeno='" + DTR["storeno"].ToString() + "', @pono='" + DTR["pono"].ToString() + "', @datesent='" + DTR["datesent"].ToString() + "']";
                    OleDbCommand selectCommand2 = new OleDbCommand(strSQLSelect, databaseConnection);

                    selectCommand2.CommandType = CommandType.StoredProcedure;

                    // Establish database connection 
                    databaseConnection.Open();

                    // Execute SQL Command 
                    dsCmd.SelectCommand = selectCommand2;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("****** Caught an exception:\n{0}", e.Message);
            }
            finally
            {
            }
            return true;
        }

Open in new window

Fernandez_EAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

gamarrojgqCommented:
Hi,

Try addin an ExecuteNonQuery() after assign your SelectCommand like this:

// Execute SQL Command
dsCmd.SelectCommand = selectCommand;
dsCmd.ExecuteNonQuery();   //Executes the Command

And the same in your loop

// Execute SQL Command
dsCmd.SelectCommand = selectCommand2;
dsCmd.ExecuteNonQuery();   //Executes the Command


0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Fernandez_EAuthor Commented:
Hi,

I did create a function to perform the saving with executeNonQuery() and it works but it's taking my dataset and reading it inside of the loop.

The first part is doing it but after at the moment of perform the loop it's not working. Meaby is not receiving the dataset correctly.

Any idea???
private static void cmdConciliationDetail(string queryString, string Pconfno, string Pstore, string Ppono, DateTime Pdatesent, string connectionString)
        {
            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                SqlCommand command = new SqlCommand(queryString, connection);
                command.CommandType = CommandType.StoredProcedure;

                SqlParameter ConfNo = new SqlParameter("@ConfNo", SqlDbType.VarChar, 20);
                ConfNo.Value = Pconfno.ToString();
                command.Parameters.Add(ConfNo);

                SqlParameter Storeno = new SqlParameter("@Storeno", SqlDbType.VarChar, 4);
                Storeno.Value = Pstore.ToString();
                command.Parameters.Add(Storeno);

                SqlParameter Pono = new SqlParameter("@Pono", SqlDbType.VarChar, 4);
                Pono.Value = Ppono.ToString();
                command.Parameters.Add(Pono);

                SqlParameter DateSent = new SqlParameter("@ConciliationDate", SqlDbType.DateTime);
                DateSent.Value = Pdatesent.ToString();
                command.Parameters.Add(DateSent);

                command.Connection.Open();
                command.ExecuteNonQuery();
                command.Connection.Close();
            }
        }



        [WebMethod]
        public Boolean SetConciliationResume(int OrdersQTY, DateTime ConciliationDate, DataSet ListaData)
        {
            DataTable Dt = table.Tables["Ivr_ConciliationDetails_data"];
            string strSQLSelect2 = "dbo.SP_GetWMOrdersConciliation";
            cmdConciliationResume(strSQLSelect2,OrdersQTY, ConciliationDate, SQLDBConnectionString);

            try
            {
                for (int i = 0; i < Dt.Rows.Count; i++)
                {
                    DataRow DTR = Dt.Rows[i];

                    string strSQLSelect = "dbo.SP_GetWMConciliationDetail";
                    cmdConciliationDetail(strSQLSelect, DTR["Confno"].ToString(), DTR["storeno"].ToString(), DTR["pono"].ToString(), Convert.ToDateTime(DTR["datesent"].ToString()), SQLDBConnectionString);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("****** Caught an exception:\n{0}", e.Message);
            }
            finally
            {
            }
            return true;
        }

Open in new window

0
Fernandez_EAuthor Commented:
Great!
0
gamarrojgqCommented:
Try to change a little your cmdConciliationDetail process, in the attached code I change the place where you open your connection and added a Try/catch block, to figure out where is the problem.
private static void cmdConciliationDetail(string queryString, string Pconfno, string Pstore, string Ppono, DateTime Pdatesent, string connectionString) 
        { 
            using (SqlConnection connection = new SqlConnection(connectionString)) 
            { 

		try{
			connection.Open();
	                SqlCommand command = new SqlCommand(queryString, connection); 
        	        command.CommandType = CommandType.StoredProcedure; 
 	
        	        SqlParameter ConfNo = new SqlParameter("@ConfNo", SqlDbType.VarChar, 20); 
                	ConfNo.Value = Pconfno.ToString(); 
	                command.Parameters.Add(ConfNo); 
 
 	                SqlParameter Storeno = new SqlParameter("@Storeno", SqlDbType.VarChar, 4); 
        	        Storeno.Value = Pstore.ToString(); 
                	command.Parameters.Add(Storeno); 
 
	                SqlParameter Pono = new SqlParameter("@Pono", SqlDbType.VarChar, 4); 
        	        Pono.Value = Ppono.ToString(); 
                	command.Parameters.Add(Pono); 
 
 	                SqlParameter DateSent = new SqlParameter("@ConciliationDate", SqlDbType.DateTime); 
        	        DateSent.Value = Pdatesent.ToString(); 
                	command.Parameters.Add(DateSent); 
 
        	        command.ExecuteNonQuery(); 
			connection.close();
		}
		catch (Exception e)
		{
			Console.WriteLine("****** Caught an exception:\n{0}", e.Message); 
		}


            } 
        }

Open in new window

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.