Question

Im using a SOAP Header and I need to Authenticate it from the Database. So, I tried creating one Class and have one method on which, when we pass username and password it will return whether its there in the DB or not.

----- My Main Class ---
[WebMethod, SoapHeader("AuthenticateUser")]
public System.Xml.XmlElement CancelUSer(string _UserID, string _Remarks)
{

    if (UsersAuth.ValidateUser(AuthenticateUser.UserName, AuthenticateUser.Password) > 0)
    {
//METHODS

     }
 }

public class UserAuthenticateHeader : SoapHeader
{
public string UserName;
public string Password;

}

public class UsersAuth
{
static OracleConnection con;
public UsersAuth()
{
 con = new OracleConnection(WebConfigurationManager.ConnectionStrings["conString"].ToString());
}




public static int ValidateUser(string _UserName, string _Password)
{
    int Result = 0;
    using (OracleCommand cmd = new OracleCommand("SELECT COUNT(*) FROM USES WHERE UID=:UID AND PASSWORD=:PASSWORD", con)) {
        cmd.Parameters.AddWithValue(":UID", _UserName);
        cmd.Parameters.AddWithValue(":PASSWORD", _Password);
        con.Open();
        Result = Convert.ToInt32(cmd.ExecuteScalar());
        con.Close();
    }
    return Result;
}

}

Now, When I call this from my program its throwing he ObjectReferenceNotSet to an instance of an object. I have set username, passsword and Headervalue for the method from the application. But its not hitting the "ValidateUser" Method.

Is there any way on which I can achieve this?

Was it helpful?

Solution

You could keep the connection object instantiation in the constructor as long as the constructor is static. i.e.

public static UsersAuth()
{
    con = new OracleConnection(WebConfigurationManager.ConnectionStrings["conString"].ToString());
}

This would have the advantage of only creating the one connection, and not creating a new one for every method call. You'd need to consider if connection reuse is desired and if so, if and when the connection should be closed.

OTHER TIPS

I solved it. Since the Method is Static, it was not calling the OracleConnection. I changed the OracleConnection declaration into the Method. And now its working.

Since the method is Static we don't need to create Object of the Class. Without an Object the Constructor will not be called. That was the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top