473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Problem with abstract class variable etc...

I am a newb to OOP programming and I'm only just starting with C#. I've done
a TON of reading lately, and I'm currently in the process of modifying some
of the function provided by the "TimeTracker Start-up Kit" for my needs.

****** BEGINING OF TIME TRACKER SUMMARY *****
First, in the "TimeTracker Start-Up Kit" they appear to have tried to set it
up so that it can be expanded to any number of different datasource types
(SQL, Access, Oracle, etc). They have a class called "DataAccessHelper.cs"
that seems to be a wrapper class for the abstract class "DataAccess.cs",
which in turn is a base class for "SQLDataAccessLayer.cs".

DataAccessHelper.cs appears to check a value in the web.config to see what
DataAccessType the ConnectionString is for (SQL, Access, Oracle, etc) and
verifies that this DataAccessType is the same type as the object created by
DataAccess.cs. It then forwards control to DataAccess.cs.

DataAccess.cs checks to make sure there is a non-null connectionString
defined in the web.config, and defines connectionString. It then sets
contracts for several abstract methods that are overridden in
SQLDataAccess.cs.

SQLDataAccess.cs defines several constant variables that are used in it's
methods, and then overrides all the abstract methods defined in
DataAccess.cs. It also has several private methods that are used for
internal workings. It has private methods that take arguments from its other
public methods, checks to make sure the variables are good, and then submits
them to the SQL server.

****** END OF TIME TRACKER SUMMARY *****

****** BEGINNING OF "WHAT I'M TRYING TO DO" SUMMARY *****
I am farily confident that we will never use any data source other than an
SQL database (at least not for this application). But I still want to have a
DataAccessLayer to separate the functionality into more managable code.

I have created the following two classes for my data access layer.

DataAccess.cs is a non-abstract class that has one read-only property
(ConnectionString), and three "SQL Helper Methods" taken directly from the
"TimeTracker Start-Up Kit" SQLDataAccessLayer.cs class. I have modified
these methods to be protected static instead of just private. There are
three methods and they are defined below:
****BEGIN - SQL HELPER METHODS***
protected static void AddParamToSQLCmd(SqlCommand sqlCmd,
string paramId,
SqlDbType sqlType,
int paramSize,
ParameterDirection paramDirection,
object paramvalue)
{

if (sqlCmd == null)
throw (new ArgumentNullException("sqlCmd"));
if (paramId == string.Empty)
throw (new ArgumentOutOfRangeException("paramId"));

SqlParameter newSqlParam = new SqlParameter();
newSqlParam.ParameterName = paramId;
newSqlParam.SqlDbType = sqlType;
newSqlParam.Direction = paramDirection;

if (paramSize > 0)
newSqlParam.Size = paramSize;

if (paramvalue != null)
newSqlParam.Value = paramvalue;

sqlCmd.Parameters.Add(newSqlParam);
}

protected static void ExecuteScalarCmd(SqlCommand sqlCmd)
{
if (ConnectionString == string.Empty)
throw (new ArgumentOutOfRangeException("ConnectionString"));

if (sqlCmd == null)
throw (new ArgumentNullException("sqlCmd"));

using (SqlConnection cn = new SqlConnection(ConnectionString))
{
sqlCmd.Connection = cn;
cn.Open();
sqlCmd.ExecuteScalar();
}
}

protected static void SetCommandType(SqlCommand sqlCmd, CommandType
cmdType, string cmdText)
{
sqlCmd.CommandType = cmdType;
sqlCmd.CommandText = cmdText;
}
****END - SQL HELPER METHODS***

I have another class, Contact.cs that inherits the "DataAccess.cs" class.
It currently has one constant variable (the name of a SQL stored procedure)
and one method. The class is defined as follow:

***BEGIN - CONTACT.CS CLASS***
public class Contact : DataAccess
{
private const string SP_CONTACT_GetContactFirstName =
"SP_CONTACT_GetContactFirstName";

public static string GetContactFirstName(uint contactId)
{
SqlCommand sqlCmd = new SqlCommand();

AddParamToSQLCmd(sqlCmd, "@ReturnValue", SqlDbType.NVarChar, 0,
ParameterDirection.ReturnValue, null);
AddParamToSQLCmd(sqlCmd, "@contactId", SqlDbType.Int, 0,
ParameterDirection.Input, contactId);

SetCommandType(sqlCmd, CommandType.StoredProcedure,
SP_CONTACT_GetContactFirstName);
/*string blah = */ExecuteScalarCmd(sqlCmd);

string returnValue =
(string)sqlCmd.Parameters["@ReturnValue"].Value;

return returnValue;
}
}
***END - CONTACT.CS CLASS***

****** END OF "WHAT I'M TRYING TO DO" SUMMARY *****

Through debugging, I have been able to determine that the returnValue
returned by my "GetContactFirstName" method in Contact.cs, is returning an
integer value of "0". However, when I run the store procedure directly in
SQL, it returns a string (like it's supposed to). After further
investigation, I see that the "ConnectionString" value in the
"ExecuteScalarCmd" method in the DataAccess.cs class IS defined properly, but
it never seems to get added to the "sqlCmd" object. I believe the problem
lies in the following section:
****
using (SqlConnection cn = new SqlConnection(ConnectionString))
{
sqlCmd.Connection = cn;
cn.Open();
sqlCmd.ExecuteScalar();
}
****
When using intellisense to view the values of the variables, I see that
"ConnectionString" is defined in the first line, but browsing through the
object sqlCmd at the next line, it still shows the "ConnectionString" to be
empty (not null, just empty).
Now, this obviously works just fine in the "TimeTracker Start-Up Kit", but
not in my class. I don't know if I've got the connectionString defined badly
in the web.config (all the online examples seem to leave out that actual
format of the connectionString, they just show where it's supposed to go in
the <add> tag), or if I've done something wrong with elsewhere. Can anyone
tell me what to do here?

Thanks for the taking the time to read this novel! =)

-Amanda
Feb 7 '06 #1
2 2786
perhaps if you post that section of your web.config minus any usernames
or passwords you use. the proper syntax in your web.config for app
configurations variables is as follows:

<configuration>
<appSettings>
<add key="connectionString" value="SomeConnectionStringHere" />
</appSettings>

<system.web>
<other settings here />
</system.web>
</configuration>

ensure that your web.config is setup in this manner, it sounds like its
not reading the connectionstring from your web.config properly.

Feb 8 '06 #2
Here is the line from my web.config. I think the format has changed from 1.1
to 2.0, so the it's different from what you posted below.
**********
<connectionStrings>
<add name="support7_SQL" connectionString="Data Source=xena;Integrated
Security=True;Database=support7;User
ID=someusernamehere;Password=somepasswordhere"
providerName="System.Data.SqlClient"/>
</connectionStrings>
**********

I made a change to the "ExecuteScalarCmd" method to bypass the "using"
section and instead manually close the database connection. I now see that
the connection string is being passed when in debug mode, but it still
returns a value of 0. =/ Below is the modified "ExecuteScalarCmd" method.
**********
protected static void ExecuteScalarCmd(SqlCommand sqlCmd)
{
if (ConnectionString == string.Empty)
throw (new ArgumentOutOfRangeException("ConnectionString"));

if (sqlCmd == null)
throw (new ArgumentNullException("sqlCmd"));

SqlConnection cn = new SqlConnection(ConnectionString);
sqlCmd.Connection = cn;
cn.Open();
sqlCmd.ExecuteScalar();
cn.Close();
*************

There is no error msg from the SQL server anymore either (before there was
something about incorrect syntax, which I assumed meant the connection string
wasn't being passed). So I've no idea why it's returning 0. I've verified
that the parameter being passed is the right value (I can see it when
drilling down with intellisense through the SqlParameters object), but it's
still returning zero.

Just so frustrated right now...

ideas?

-Amanda

"DKode" wrote:
perhaps if you post that section of your web.config minus any usernames
or passwords you use. the proper syntax in your web.config for app
configurations variables is as follows:

<configuration>
<appSettings>
<add key="connectionString" value="SomeConnectionStringHere" />
</appSettings>

<system.web>
<other settings here />
</system.web>
</configuration>

ensure that your web.config is setup in this manner, it sounds like its
not reading the connectionstring from your web.config properly.

Feb 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Dev | last post by:
Hello, Why an Abstract Base Class cannot be instantiated ? Does anybody know of the object construction internals ? What is the missing information that prevents the construction ? TIA....
12
by: Daedalus.OS | last post by:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of you. If the only answer you can provide is "get a book about OOP" then don't loose your time and mine cause it's already...
10
by: Saso Zagoranski | last post by:
hi, this is not actually a C# problem but since this is the only newsgroup I follow I decided to post my question here (please tell me where to post this next time if you think this post...
9
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
12
by: scottt | last post by:
hi, I am having a little problem passing in reference of my calling class (in my ..exe)into a DLL. Both programs are C# and what I am trying to do is pass a reference to my one class into a DLL...
4
by: David Zha0 | last post by:
Hi, "when we call a virtual method, the runtime will check the instance who called the method and then choose the suitable override method, this may causes the performance drop down", is this...
9
by: msbs1984 | last post by:
Difference Between Interface and Abstract Class?
5
by: tshad | last post by:
In VS 2003, I am setting up an abstract class that is setting up classes for each datatype of VB.Net (as well as C#). I am trying to set it up so that most of the work is done in the Abstract...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.