Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6024977
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:14:06+00:00 2026-05-23T04:14:06+00:00

I am currently developing an IVR system, my question is more on a development

  • 0

I am currently developing an IVR system, my question is more on a development side of basic architecture and opening/closing a database connection. As you can see from the code below, in the page_load I am opening a connection, passing variables, and then closing the connection. My problem lies in the fact that the variables are not set during the page load, they are set as the call comes in, which is in the Boolean ParseXML section. I need to know what is the best way of opening the connection during page load, pass the variables after they are collected, and then finally close the connection. And most important how to do this, I have tried several different methods, with no success.

My initial thought process and approach is to split up the database connection code, and place them in different parts of the page life cycle. But I have had zero success with where to exactly put it.

The Boolean parse, writes to a text file. But I want it to also write to a database.

<%@ Page Language="C#" aspcompat="true" %>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Collections" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.Web.SessionState" %>
<%@ Import Namespace="System.Web.UI" %>
<%@ Import Namespace="System.Web.UI.WebControls" %>
<%@ Import Namespace="System.Web.UI.HtmlControls" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Text.RegularExpressions" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Import Namespace="System.Data" %>



   <script language="C#" runat="server">


       Boolean ParseXML(string XMLContent)
       {
           try
           {
               XmlDocument doc = new XmlDocument();
               doc.LoadXml(XMLContent);

               String MenuID, Duration, CallerID, CallID, DateAndTime, VoiceFileName;
               XmlNode TempNode;
               Byte[] VoiceFile;

               XmlElement root = doc.DocumentElement;
               XmlAttributeCollection attrColl = root.Attributes;

               //parse inbound values
               MenuID = attrColl["menuid"].Value;
               Duration = attrColl["duration"].Value;
               CallID = attrColl["callid"].Value;
               CallerID = attrColl["callerid"].Value;

               //writed parsed values to file
               StreamWriter w = File.AppendText(Request.MapPath("summaryincall.txt"));

               w.Write(String.Format("\"{0:MM/dd/yyyy}\",\"{0:HH:mm:ss}\"", DateTime.Now));

               XmlNodeList NodeCount = doc.SelectNodes("/campaign/prompts/prompt");
               foreach (XmlNode node in NodeCount)
               {
                   attrColl = node.Attributes;

                  w.WriteLine("Prompt ID: " + attrColl["promptid"].Value);
                  w.WriteLine("Keypress : " + attrColl["keypress"].Value);
                  w.Write(attrColl["keypress"].Value);


                   if (node.HasChildNodes)
                   {
                       TempNode = node.FirstChild;
                       attrColl = TempNode.Attributes;

                       //convert file to binary
                       VoiceFile = System.Convert.FromBase64String(TempNode.InnerText);
                       VoiceFileName = attrColl["filename"].Value;

                       //save file in application path
                       FileStream fs = new FileStream(Request.MapPath(VoiceFileName), FileMode.OpenOrCreate);
                       BinaryWriter bw = new BinaryWriter(fs);
                       bw.Write((byte[])VoiceFile);
                       bw.Close();
                       fs.Close();

                       w.WriteLine("Filename : " + VoiceFileName);
                   }
               }


               w.Close();
               return true;
           }
           catch (Exception e)
           {
               Response.Write(e.Message);
               return false;

           }
       }


       void Page_Load(object sender, System.EventArgs e)
       {

           string connectionString = "server=abc;database=abc;uid=abc;pwd=1234";
           SqlConnection mySqlConnection = new SqlConnection(connectionString);
           string procedureString = "Call_Import";
           SqlCommand mySqlCommand = mySqlConnection.CreateCommand();
           mySqlCommand.CommandText = procedureString;
           mySqlCommand.CommandType = CommandType.StoredProcedure;
           mySqlCommand.Parameters.Add("@CDate", SqlDbType.DateTime).Value = DateTime.Now;
           mySqlCommand.Parameters.Add("@CTime", SqlDbType.DateTime).Value = DateTime.Now;
           mySqlCommand.Parameters.Add("@ID", SqlDbType.Int).Value = keypress;
           mySqlCommand.Parameters.Add("@CType", SqlDbType.Int).Value = CallID;
           mySqlConnection.Open();
           mySqlCommand.ExecuteNonQuery();
           SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter();
           mySqlDataAdapter.SelectCommand = mySqlCommand;
           mySqlConnection.Close();


           try
          {
             String xmlcontent, PostResponse, campaign;
             Byte[] Bindata = Request.BinaryRead(Request.TotalBytes);

             string XML;
             XML = System.Text.Encoding.ASCII.GetString(Bindata);
             StreamWriter w = File.AppendText(Request.MapPath("xmlsummaryincall.txt"));
             w.WriteLine("--- "  + DateTime.Now + " ------------------------------------------------------");
             w.WriteLine(XML.Replace("<?xml version=\"1.0\"?>", ""));  //needed so ?xml tag will display as text
             w.WriteLine("");
             w.WriteLine("");          
             w.Close();

             if (!ParseXML(XML)) Response.Write("Failed");

           }
           catch (Exception error)
           {
             Response.Write(error.Message);
           }
       }

   </script>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T04:14:07+00:00Added an answer on May 23, 2026 at 4:14 am

    I am assuming that the SqlDataAdapter is being used after the ParseXml method is called. Try the following:

    • Declare a field for SqlConnection and SqlCommand.
    • Open the connection in Page_Load.
    • Set the command parameters in ParseXml.
    • Close the connection in Page_Unload.
    <script language="C#" runat="server">
        private SqlConnection mySqlConnection;
        private SqlCommand mySqlCommand;
               
        Boolean ParseXml(string XMLContent){
    
            // Do other work
    
            mySqlCommand.Parameters["@CDate"].Value = DateTime.Now; 
            mySqlCommand.Parameters["@CTime"].Value = DateTime.Now; 
            mySqlCommand.Parameters["@ID"].Value = keypress; 
            mySqlCommand.Parameters["@CType"].Value = CallID; 
    
            // Do other work
    
        }
    
         void Page_Load(object sender, System.EventArgs e)
         {
            string connectionString = "server=abc;database=abc;uid=abc;pwd=1234"; 
            mySqlConnection = new SqlConnection(connectionString); 
            string procedureString = "Call_Import"; 
            mySqlCommand = mySqlConnection.CreateCommand(); 
            mySqlCommand.CommandText = procedureString; 
            mySqlCommand.CommandType = CommandType.StoredProcedure; 
            mySqlCommand.Parameters.Add(new SqlParameter("@CDate", SqlDbType.DateTime));
            mySqlCommand.Parameters.Add(new SqlParameter("@CTime", SqlDbType.DateTime));
            mySqlCommand.Parameters.Add(new SqlParameter("@ID", SqlDbType.Int));
            mySqlCommand.Parameters.Add(new SqlParameter("@CType", SqlDbType.Int));
            mySqlConnection.Open(); 
            SqlDataAdapter mySqlDataAdapter = new SqlDataAdapter(); 
            mySqlDataAdapter.SelectCommand = mySqlCommand;
         }
    
         void Page_UnLoad(object sender, System.EventArgs e){
            if (mySqlConnection.State != ConnectionState.Closed)
                mySqlConnection.Close();
         }
    </script>
    

    Here is a link that will help you understand the life cycle of an ASP.NET page.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am currently developing a Rails application using a database that was designed before
I'm currently developing a PHP application that's using an Access database as a backend.
Currently developing an ASP.NET WebApplication with VS2008. I stopped development for a few weeks,
Im currently developing a system where the user will end up having large arrays(
I currently developing an hotel web site and they use the iHotelier system. I
We are currently developing a server whereby a client requests interest in changes to
I was currently developing a desktop application in C# using mono and testing in
I am currently developing an approval routing WCF service that will allow an user
I'm currently developing for a handheld device running Windows XP Professional (not Tablet PC
I'm currently developing an application that is comprised of five separate executables that communicate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.