csouma

csouma

  • NA
  • 4
  • 0

separating access methods from variable daclaration

Aug 8 2008 10:50 AM
hi, i am learning c# still, and OOP. And new to c-sharpcorner forums.

I have used vis studio 2005 to create a small app. It logs into a database. At the moment it is working. I can read records, however my design isn't ideal, for each button click i log into the db and make a query, I would like my login etc to happen on form load, and my button click event to only do a query, thus separating the login and logout data from the quering.

I don;t know how to do this. I am including my code as I would like it to be. I have commented as best I can. I know that I need to declare accesor methods, but I don't know how.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using APILib;

namespace UCTMonitor
{
    public partial class CallTicket : Form
    {
        public CallTicket()
        {
            InitializeComponent();
        }
         // when the form loads i want these variables set - causes errors
        public void CallTicket_Load(object sender, EventArgs e)
        {
           
            //set variables setup login credentials and  login
            string strText;
            strText = "CallID = '";
            string strRemText;
            strRemText = "'";

            string strId;
            strId = textBox1.Text;
           
            string newstring;
            newstring = string.Concat(strText + strId + strRemText);         

            //Create a new workspace to work with.
            haWorkspace wrksp = new haWorkspace();

            //Create a call ticket.
            haCallTicketRS rsCall_Ticket;                 

            //Collect possible errors.
            colError = (haErrorCollection)wrksp.Errors;

            //Create a boolean to store the connection status, set to false at start.
            bool blnLogin = false;

            //Some random object needed for opening a connection.
            object obj3 = "";
           //Connect to the database.
            blnLogin = wrksp.Login("uid", "pwd", "dsn");
        }
 
        //this is an event triggered from a main form
        public void button1_Click(object sender, EventArgs e)
        {                                     
            rsCall_Ticket = (haCallTicketRS)wrksp.OpenCallGroup(Convert.ToInt32(haCallGroupType.cgSQL),      
Convert.ToInt32(haClientScope.csGlobal),newstring, null, ref obj3);
           
            //All values read from the Call_Ticket are in string format. So we can read them
            //off into MessageBoxes.
            MessageBox.Show(rsCall_Ticket.CallID);
            MessageBox.Show(rsCall_Ticket.CallStatus);
            MessageBox.Show(rsCall_Ticket.CallDescription);          
            }

        //clicking this button should logout and clear connection variables
- causes errors
        private void button2_Click(object sender, EventArgs e)
        {
           
    //Close the connection.
            wrksp.Logout();

            //Clear out old connection variables.
            wrksp = null;
            rsCall_Ticket = null;
    this.Hide();
           
        }
    }
}

When I compile this I get a number of errors for the variables I have declared and reference outside of the class

Error 10 The name 'newstring' does not exist in the current context
Error 11 The name 'obj3' does not exist in the current context

Thanks for your help.

Answers (1)