locked
c# class RRS feed

  • Question

  • User-2101381540 posted

    I have a class that I am having a difficult time retrieving the values.  The values I need are the VCutoff and VFacilityID. They are returning 0 so they are not retrieving the value from the database. I am accessing these from the Page_Load. Please help or if you have suggestions on a better solution?? 

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Data.SqlClient;
    using System.Data;
    using System.Configuration;
    using System.Net;
    using System.Net.NetworkInformation;
    
    /// <summary>
    /// Summary description for GetIPLocation
    /// </summary>
    public class GetIPLocation
    {
        string hostname = Dns.GetHostName();
          public static int VCutoff; //default value
          public static int VFacilityID; //default value
    
       
        public void GetFacilityData()
        {
            
            string LocationIP = "10.245";
            string ConnString = ConfigurationManager.ConnectionStrings["VisitorTrackingConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(ConnString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.Connection = con;
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = "SELECT f.FID, f.VCountCutoff, p.FacilityIPNetworkAddress FROM  tbl_Facility f INNER JOIN  tbl_FacilityIPNetworkAddress p ON f.FID = p.FacilityID WHERE p.FacilityIPNetworkAddress = @LocationIP";
                    cmd.Parameters.AddWithValue("@LocationIP", LocationIP);
                    con.Open();
                    SqlDataReader reader;
                    reader = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                    //GetInt16(1) is referencing the 2nd column in the select column (VisitorCountCutoff) - if you change the select sequence chnage this number
                    if (reader.HasRows)
                    {
    
                     GetIPLocation.VFacilityID = reader.GetInt16(0);
                     GetIPLocation.VCutoff = reader.GetInt16(1);
    
                       // Tuple<int> FacilityInfo = new Tuple<int>(VFacilityID);
                    }
                    reader.Close();
                    
                }
            }
    
        }
        public static int GetCutOff()
        {
    
    
            return GetIPLocation.VCutoff;
            
        }
    
        public static int GetFacilityID()
        {
            return GetIPLocation.VFacilityID;
        }
    
    
    }
    
       Response.Write("This is the FacilityID: " + GetIPLocation.GetFacilityID());
       Response.Write("This is the Cutoff: " + GetIPLocation.GetCutOff());



    Friday, September 4, 2015 1:09 PM

Answers

  • User281315223 posted

    You would basically need to ensure that your GetFacilityData() method was called prior to anything attempting to access your other methods or properties. So that should work if you were to use something like :

    GetIPLocation.GetFacilityData();
    Response.Write("This is the FacilityID: " + GetIPLocation.GetFacilityID());
    Response.Write("This is the Cutoff: " + GetIPLocation.GetCutOff());

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 4, 2015 2:36 PM

All replies

  • User281315223 posted

    You would basically need to ensure that your GetFacilityData() method was called prior to anything attempting to access your other methods or properties. So that should work if you were to use something like :

    GetIPLocation.GetFacilityData();
    Response.Write("This is the FacilityID: " + GetIPLocation.GetFacilityID());
    Response.Write("This is the Cutoff: " + GetIPLocation.GetCutOff());

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 4, 2015 2:36 PM
  • User-2101381540 posted

    When I try to add  this line I get the error "An object is required for the non-static field, method, or property 'GetIPLocation.GetFacilityData()'

    GetIPLocation.GetFacilityData();
    Friday, September 4, 2015 2:49 PM
  • User281315223 posted

    This is likely because your class itself isn't static and thus requires an instance of itself to exist.

    Try adding static to the class declaration and to any methods that you want to access statically (without an instance of the class) :

    public static class GetIPLocation
    {
        public static int VCutoff; 
        public static int VFacilityID;
    
        public static void GetFacilityData()
        {
            // You may want to define the host name in here
            string hostname = Dns.GetHostName();
    	
            // Other code
        }
        public static int GetCutOff()
        {
            return GetIPLocation.VCutoff; 
        }
    
        public static int GetFacilityID()
        {
            return GetIPLocation.VFacilityID;
        }
    }

    You can see a very basic example of this here.

    Friday, September 4, 2015 2:55 PM
  • User-2101381540 posted

    Thank you. I am no longer getting the error. I realized that the code I am using to access the database worked when I had it in a function in my code behind file but is not getting the values from the database when I am using the code in the class. Am I missing a step? Thanks for all of your help.

    Friday, September 4, 2015 3:04 PM
  • User1292358045 posted

    I noticed your code has variable "LocationIP", I think you get IP from your page request instance?

    In web page code behind, you have direct access of request instance, then you can get IP address,

    In class, you lost the access to request instance, that's maybe why you didn't get result, because you got no ip address.

    This is just a wild guess.

    Friday, September 4, 2015 5:25 PM
  • User465171450 posted

    You are calling

    if (reader.HasRows)
    

    The problem here I think is that if you were to use reader.GetInt16() it would actually explode on you. The reason is you aren't calling 

    if(reader.Read())
    {
        // do stuff here.
    }

    If you don't call Read() then any attempt to access the datareader blows up, which is why I would think you aren't returning any errors as this should crash if it has rows.

    Friday, September 4, 2015 6:18 PM