Got something to write about? Check out our Article Builder.

C#

Moderators: None (Apply to moderate this forum)
Number of threads: 2452
Number of posts: 5305

This Forum Only
Post New Thread

Report
DataAccessObject.. Posted by serfreedshaft on 26 Apr 2009 at 5:17 AM
Hello people.. I'm new to this forums.. Plus, its my first day on a job (Just finished college) And my first assignment is.. Converting a VB6 code to a C# code.. I'm stuck at understanding the programmers code plus the comments on the program is not really helpfull..

Here is a sample that I tried using the VB to C# converter..

------Initialized variables------------------------------
//ado connection
private ADODB.Connection conData = new ADODB.Connection();
private ADODB.Recordset recData = new ADODB.Recordset();
private ADODB.Recordset recRNum = new ADODB.Recordset();
//dao connections
DAO.Database daodb;
//UPGRADE_WARNING: Arrays in structure daors may need to be initialized before they can be used. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="814DF224-76BD-4BB4-BFFB-EA359CB9FC48"'
DAO.Recordset daors;
//validation
string strSql;
string strCode;
string msgAmtErr;
string msgRecNoErr;
string strRecNo;
string tmpErr;
bool flgCode;
bool flgDte;
bool flgForm;
bool flgAmt;
bool flgrecNo;
bool flgExists;
string mainAmtErr;
//getting bank code
string xBnkInd;
string Sqlbank;
string subBnkInd;
//passed parameters
string xBank;
string xFilePath;
string xTrdate;
//sys object
Scripting.FileSystemObject fsys = new Scripting.FileSystemObject();
string sFile;
string CDRCFile;
Scripting.TextStream txtErr;
//balancing figures
decimal cmpFri;
decimal cmpSat;
decimal cmpSun;
decimal cmpMon;
decimal cmpTue;
decimal cmpWed;
decimal cmpThur;
decimal amtFri;
decimal amtSat;
decimal amtSun;
decimal amtMon;
decimal amtTue;
decimal amtWed;
decimal amtThur;
decimal schedAmt;
decimal schedCmp;

short EODval;
string SchedDay;
bool flgAmtValid;
bool flgBalStat;
string msgBalErr;


------One of the function that failed to update to C#-------
//Get compared amount for RB
private void SetCmpAmountRB()
{
// ERROR: Not supported in C#: OnErrorStatement

{
if (!string.IsNullOrEmpty(daors.Fields("amt1").Value)) cmpFri = (decimal)daors.Fields("amt1").Value;
if (!string.IsNullOrEmpty(daors.Fields("AMT2").Value)) cmpSat = (decimal)daors.Fields("AMT2").Value;
if (!string.IsNullOrEmpty(daors.Fields("AMT3").Value)) cmpSun = (decimal)daors.Fields("AMT3").Value;
if (!string.IsNullOrEmpty(daors.Fields("AMT4").Value)) cmpMon = (decimal)daors.Fields("AMT4").Value;
if (!string.IsNullOrEmpty(daors.Fields("AMT5").Value)) cmpTue = (decimal)daors.Fields("AMT5").Value;
if (!string.IsNullOrEmpty(daors.Fields("AMT6").Value)) cmpWed = (decimal)daors.Fields("AMT6").Value;
if (!string.IsNullOrEmpty(daors.Fields("AMT7").Value)) cmpThur = (decimal)daors.Fields("AMT7").Value;
}
}

I can't understand what those "if" statements mean (I mean, if not string is null or empty????) , plus the (!string.isNullorEmpty) return value is.

I also did a little research about dao, and on one web site, it states that DAO is pretty much useless now.. and I can't get the commands right for C# to extract data from the data base (using a MSExcel database)

I hope you people can help me.. I've been trying to figure this thing out for 5 days, and still no luck.
Report
Re: DataAccessObject.. Posted by Psightoplazm on 26 Apr 2009 at 11:16 AM
Jeeze - so I'm not saying this out of any kind of disrespect, but one of your biggest issues right now is that you are working for a company that has a VB6 code foundation. However, they are taking a good step in moving to dotnet (assuming you are building business applications and it looks like you are)

So I'm not sure how much of this code you are recognizing or not but they are simply opening a database connection and grabbing some data from a database here - hopefully all of the missing query and connection code was just simply omitted by you. You can actually replace this with any one of a multitude of connectors available in the dotnet libraries depending on your database type.

the conversion error you are seeing is from the error handling system that VB6 had. In dotnet you will need to replace this with a try/catch statement.

Somewhere in the vb6 code you will find "On Error GoTo something" or "On Error DoNext" or whatever

What this simply means is that in the current code scope, from the on error statement onward, if an error occurs do the specified action.

In dotnet you need to wrap the code you want to handle errors on inside of a try block, and then use catch blocks to handle the various different types of errors you want to handle.

so...

try
{
    //Some code that might fail for whatever reason
}
catch (ExceptionType e)
{
    // If an exception occurs matching the specified exception type
    // then this code will execute and load the exception data into e
}


It also looks like the code right below the conversion error is probably giving you some problems as well.

the string class in .Net has a static method on it that will check if a string is either null or empty (IsNullOrEmpty) which accepts a string value and returns a boolean result. So that is fine if the "Value" property on the field objects you are checking is in fact a string.

Buuut - if they are a string then the very next statement is a conversion statement that is attempting to convert a string into a decimal - and this will fail.

However, if the Value property is an object that is holding a decimal, then in the null or empty check you should call the ".ToString()" method on the .Value parameter in order to convert it to a string so the ".IsNullOrEmpty" method is happy.

Otherwise if the .Value parameter is in fact a string and you want to convert it into a decimal - then the "decimal" class has a method on it called ".Parse(string)" or ".TryParse(string, out decimal)" that will get you the decimal value you need to work with.


but to be completely honest - if this was originally written in VB6, I would be wary of the programmer that wrote it because the chances are he wasn't following very good practices as the VB6 language was never a strong promoter of good OOP design or practices. On top of the fact that you are converting from unmanaged to managed code - I would say get a spec sheet and a team together and design + write the new and improved version natively in a dotnet language.

sorry for sounding so oppinionated, just from my personal experience trying to auto-convert from vb6 to .net is like moving from one maintenance nightmare to a worse one.

></\/~Psightoplasm`~
Report
Re: DataAccessObject.. Posted by Psightoplazm on 26 Apr 2009 at 11:28 AM
Sorry - I originally missed that you are connecting to excel data instead of an actual DB. For this you just use the standard odbc connector.

It looks like there are some examples here - including connection strings
http://www.codeproject.com/KB/grid/Excel_in_CS.aspx

></\/~Psightoplasm`~
Report
Re: DataAccessObject.. Posted by serfreedshaft on 26 Apr 2009 at 9:13 PM
I know I know.. Its just that, that VB6 program was written by a team of students doing their On-the-Job-Training, so as my first assignment to my company.. I was given that to convert it to C# (which is their forte and mail PL..)

I'll read more and on the site you gave me. Expect to be hearing from me soon..

(Yeah I'm **cked up on my first week on the job *sob*)



 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A BootstrapLabs project.