Need help converting code.

Posts 1–15 of 17 · Page 1 of 2
Need help converting code.
Ok so this is my first time learning c++ and ive been teaching myself for 2 days. Im in the process of converting an application i made in vb.net to c++ but i am stuck at a point and i cannot understand why for the life of me it is happening.

Im currently converting this block:

Code:
 
 Dim con As OleDb.OleDbConnection = New  _
            OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = " & _
                                  Server & "\Database.mdb")
            con.Open()
            Dim cmd As OleDb.OleDbCommand = New  _
            OleDb.OleDbCommand(str, con)
            Dim da As New OleDb.OleDbDataAdapter(cmd)
            Dim ds As New DataSet()
            da.Fill(ds, "DatabaseName")
            Dim dr As DataRow
            For Each dr In ds.Tables(0).Rows
                If dr(1).ToString.Contains(arg(1)) Then
                 
                Else
                    Console.WriteLine("")
                    Exit For
                End If
            Next
to:

Code:
		using namespace System::Data::OleDb;
		OleDbConnection ^ myConnection = gcnew OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source = "+ server + "\\Database.mdb");
		try{
			//Open Connection
			myConnection->Open();
			OleDbCommand ^ cmd = gcnew OleDbCommand(str, myConnection);
			OleDbDataAdapter ^ adapter = gcnew OleDbDataAdapter(cmd);
			DataSet ^ ds = gcnew DataSet();
			adapter->Fill(ds,"DatabaseName");
			DataRow ^ dr;
			for each(dr in ds->Tables[0]->Rows){
				if (dr[1]->ToString->Contains(argv[1])){
				
				}
			}
		}catch(OleDbException ^ ex){
			Console::WriteLine(ex->Message);
			system("PAUSE");
		}
I have underlined the line of code thats giving me a headache. Looking at my code i have written everything correctly so why won't it work? I have tried multiple solutions but none work, i either get one of the following errors:

1>.\AU.cpp(47) : error C2227: left of '->Contains' must point to class/struct/union/generic type

or

*1>.\AU.cpp(47) : error C2064: term does not evaluate to a function taking 1 arguments

Help!
get rid of all of these "^" those are XOR operators you don't need them when declaring variables.
its managed C++ why so you need those in there

btw your problem is this:
ToString is a method - so you should use ToString()
dr[1]->ToString()->Contains(argv[1])

I've worked with managed C++ several years and it's a P.I.T.A and really only good if you want to mix C++ code with .NET..(and even then if you can split out i'd rather stick it in a dll and pinvoke the damn thing) . if you want to do these types of apps(Databases etc), just go C# (or stick with VB.NET)

If you want to learn C++ (for hacking etc) then you may want to start with 'normal' C++ & the Windows API.. in the end , if you're serious, you'll end up learning them all anyway one way or another
Im trying to stray away from vb.net now, Im converting this project as a lesson so i understand the syntax.

Ive tried your method and sadly that did not and still does not work I get the following error:

1>.\AU.cpp(47) : error C2664: 'System::String::Contains' : cannot convert parameter 1 from 'char *' to 'System::String ^'

If i do argv[1]->ToString() to cancel out this error it gives me a number which is not what i want. Im trying to get the text/string passed to the console.
Well seeing it's an array did you declare the array properly?
Quote Originally Posted by falzarex View Post
Well seeing it's an array did you declare the array properly?
Yes i declared it properly that's the part that has me puzzled. Im coding this the same exact way as i did in my vb.net code so i have no idea why this is happening.


argv[1]->ToString() returns an integer and that is not what i want. Im confused as to why it would return an integer when 1) a string was passed to it 2) im using ToString().
it's telling you argv[1] is a char* and it's expecting a string^ to be passed into the Contains() Method.
Hence why when you call argv[1]->toString() you a numeric value. Check that value in a ASCII table and you'll notice it's the first character of whatever string you had. Strings in ManagedC++/c#/VB etc are nothing like old fashioned char* strings in plain C++ and be a cause for confusion if you're used to the nice 'modern' string classes

you could do a ' gcnew string(argv[1])' to wrap it
I wish i could thank you a million times XD wow, that did the job and thanks for clarifying for me why i was having that problem. Your a life saver man
My apologies for the double post but ive come across another one:

The IBytesRead is an int so why is it giving me the following error?:
1>.\AU.cpp(69) : error C2664: 'void System::IO::Stream::Write(cli::array<Type> ^,int,int)' : cannot convert parameter 3 from 'System::Int32 ^' to 'int'
beats me... hopefully BA can help you out when he gets on.
This is a shot in the dark, but you can try casting it to an int like this: (int)
FS->Write(bBuffer, 0, (int)iBytesRead);
prbly wont work... you might have to use a function to convert it. like I said. idk Never used managed C++
I may be wrong on this but try.

[php]
FS->Write(bBuffer, 0, Convert.ToInt32(iBytesRead));
[/php]

Or

[php]
FS->Write(bBuffer, 0, Convert->ToInt32(iBytesRead));
[/php]

I've never done managed C++ before...
Quote Originally Posted by why06 View Post
beats me... hopefully BA can help you out when he gets on.
This is a shot in the dark, but you can try casting it to an int like this: (int)
FS->Write(bBuffer, 0, (int)iBytesRead);
prbly wont work... you might have to use a function to convert it. like I said. idk Never used managed C++
This seemed to have done the trick, thanks. Ill give it a try and report back.
I tested it and both (int) and Convert::ToInt32 work. Im having another issue now with the filestream. I created the filestream to write a file from the buffer but it's giving me "Access is denied".

Code:
	FileStream ^FS(gcnew FileStream(DownloadLocation,FileMode::Create));
sounds like ur program isn't privileged enough. Maybe check ur folder's read & write access, etc.
Quote Originally Posted by why06 View Post
sounds like ur program isn't privileged enough. Maybe check ur folder's read & write access, etc.
Im writing to my desktop folder =/.
Code:
String ^DownloadLocation("C:\\Users\\Kurt\\Desktop");
I tested a previous application i made that uses this line and it accessed the desktop fine.

Edit* I am so perplexed beyond belief. I thought it was something i was doing wrong so i wrote it into a seperate function and even now im still getting Access is denied.
Posts 1–15 of 17 · Page 1 of 2
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us