Helpful Information
 
 
Category: .Net Development
C# web service that can't remember values.

Client side (Program)
Line 1: Dim Test As New D_WebReference.Service_D() 'Create reference to object
Line 2: Test.DoThis() 'Invoke this method.
Line 3: MsgBox(Test.Message) 'Display this properties value.


Server side (Web service)
Line 1: private string l_message = ""; //declare variable in the declarations.
Line 2:// Invoke this method. Give "l_message" a value.

[WebMethod]
public void DoThis()
{
l_message="Save this!";
}

Line 3:// The property "Message" should return "l_message".

[WebMethod]
public string Message()
{
return l_message;
}

in the server side(web service) replace line 1 with this:


private static string l_message = "";



that should work

Originally posted by fetcher
in the server side(web service) replace line 1 with this:


private static string l_message = "";



that should work

Oh my god....... That worked!

for (int i; i < 100; i++)
{
System.Console.WriteLine ("Thank you!");
}

:o

If I do the webservice in VB, then it's fine!. It didn't want to work in C# (I ported all my existing VB .NET services over to C# web services).

One more question... have you ever worked with ADO Command objects?

I couldn't get the Command object to work in C#, so I created a DLL in VB that handles the execute of data to the database.

In VB .NET


CMD.CommandText = SQL
CMD.Execute()


Very simple. In C#, it wants some odd parameters (I believe a DataReader :rolleyes: )


Thanks again for your help!

I think it is asking for a datareader because C# is using ADO.NET not ADO. Yea I've worked with ADO when I coded with Visual Basic but since .NET came along I've always used ADO.NET..... Unless you have some kind of a weird reason I can't see why you are still using ADO with VB.NET or C# :confused:

Originally posted by fetcher
I think it is asking for a datareader because C# is using ADO.NET not ADO. Yea I've worked with ADO when I coded with Visual Basic but since .NET came along I've always used ADO.NET..... Unless you have some kind of a weird reason I can't see why you are still using ADO with VB.NET or C# :confused:

So.. if I used the following syntax:



Dim RSA As ADODB.Recordset = New ADODB.Recordset()

RSA.Open("Select * From Facts Order By Title", User.ConnectionString, ADODB.CursorTypeEnum.adOpenDynamic, ADODB.LockTypeEnum.adLockOptimistic)

Do While RSA.EOF = False
ListBox1.Items.Add(RSA.Fields("Title").Value)
RSA.MoveNext()
Loop


It's wrong? What's better? Going through 4 layers of objects to get the data? I LOVE the ADO Recordset. It's light, fast, and ultra efficient. Perhaps I should I read up on ADO .NET

I never realized that this was the wrong way of doing things in .NET

This is the reference:

It's not wrong but ADO.NET is a huge improvement from ADO. I don't know if it is as simple as ADO but it's pretty easy to learn and you don't go through 4 layers

Read that as a start
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconfundamentaldataconceptsinvisualstudionet.asp

populating a listbox using ado.net (accessing sql server)



SqlDataReader sdr;
SqlConnection sc = new SqlConnection("Data Source=localhost;User ID=xxxx;password=xxxx;Initial Catalog = sample");
sc.Open();

SqlCommand cmd = new SqlCommand("select role from roles",sc);

sdr = cmd.ExecuteReader();
listBox1.Items.Clear();
do {
while (sdr.Read())
listBox1.Items.Add(sdr.GetString(0));
}while(sdr.NextResult());

sc.Close();

Originally posted by fetcher
It's not wrong but ADO.NET is a huge improvement from ADO. I don't know if it is as simple as ADO but it's pretty easy to learn and you don't go through 4 layers

Read that as a start
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbconfundamentaldataconceptsinvisualstudionet.asp

populating a listbox using ado.net (accessing sql server)



SqlDataReader sdr;
SqlConnection sc = new SqlConnection("Data Source=localhost;User ID=xxxx;password=xxxx;Initial Catalog = sample");
sc.Open();

SqlCommand cmd = new SqlCommand("select role from roles",sc);

sdr = cmd.ExecuteReader();
listBox1.Items.Clear();
do {
while (sdr.Read())
listBox1.Items.Add(sdr.GetString(0));
}while(sdr.NextResult());

sc.Close();


Thanks. I did try that before

SqlDataReader sdr;

It didn't like it. I'll try it again.

I see that as backwards in certain ways. Instead of having one control (ADO Recordset), we now have the DataReader, SqlConnection, and SqlCommand.

No biggie. I've seen this before in different pieces of source code. Just thought it was the hard way of doing something so simple. If the ADO.NET way is the right way, I shall become an expert on it. :o

SqlDataReader sdr;

It didn't like it. I'll try it again.


Did u forget to put: using System.Data.SqlClient; ?

Originally posted by fetcher
Did u forget to put: using System.Data.SqlClient; ?

Ha ha ha! No...

In fact, I know remember a lot about ADO .NET - I played around with it for a few months, and everything was too slow. Things would take seconds to do in ADO .NET and half that time in the old ADO.

At the top of my web service.

// SQL Goodies
using System.Data.SqlClient;

// Access Goodies
using System.Data.OleDb;

I'm not a complete C# newbie. :o I've been a hardcore Visual Basic programmer for many years, and I'm experimenting with other languages.

By the way, I like how you're very familiar with C#. I'm going to ask one more question (if you don't mind). What's the C# ADO .NET equivalency for this:



Dim CMD As ADODB.Command = New ADODB.Command()
Dim CONN As ADODB.Connection = New ADODB.Connection()

'Connect to the database
CONN.ConnectionString = ConnectionString
CONN.Open()

'Give the Command object an active connection.
CMD.ActiveConnection = CONN

'Execute
CMD.CommandText = SQL
CMD.Execute()


The CMD.Execute() is the money line.

Let's see if you can go 3 for 3 today. ;)
I promise, this is the last question.

:D



SqlConnection sc = new SqlConnection(ConnectionString);
sc.Open();
SqlCommand cmd = new SqlCommand(SQL,sc);
int rowsAffected = cmd.ExecuteNonQuery();


:p
and you also have ExecuteReader and ExecuteScalar

and now I'm going to sleep :cool:

Later

100%

Have a good night. :)










privacy (GDPR)