views:

30

answers:

1

Hi! I have a struct in a web service in c#. When I use "Selet * from TABLE1"; in a WebMethod I get a fully populated struct. But when I add a WHERE clause, I get null in response. Why is this? I have searched everywhere for a simple explanation but haven't found one.

How can I use a SELECT * FROM TABLE1 WHERE _id=" + id "'"; If I only want to retrieve a single post from the database it works fine, but not when I get a multiple row response.

Is there also a way of ordering a multiple-row response any way in c#?

Thanks in advance!

edit :

DataSet myDS = new DataSet();

   try
   {
       myConnection.Open();

       // Fill dataset with account data
       //myCommand.Fill(myDS, "userdata");
       myAdapter.Fill(myDS, "toplista");

       myConnection.Close();

       int i = myDS.Tables["toplista"].Rows.Count;
       toplista[] mytoplista = new toplista[i];
       i = 0;


       foreach (DataRow row in myDS.Tables["toplista"].Rows)
       {
           mytoplista[i].name = row["_name"].ToString();
           mytoplista[i].points = int.Parse(row["_points"].ToString());
           mytoplista[i].level = row["_level"].ToString();

           i++;
       }
       return mytoplista;
+1  A: 

Am I understanding correctly that your struct represents a single tuple from your table? If that's the case, you should either be trying to populate an IEnumerable<MyStruct> or only getting the first matching row out of your table. Otherwise, what do you expect it to do with the rest of the data that gets returned?

StriplingWarrior
So, this above works when I get all values, but not when i use a WHERE-clause in the select statement.
Anna-Karin
What error are you getting when you use the WHERE clause? Is it possible that you're constructing it incorrectly? In the question, I notice that you're missing a single-quote before the ID. As Jon Skeet pointed out, you definitely should be using a parameterized query for security reasons, but it might also help to avoid mistakes like this.
StriplingWarrior
The error is that I don't get any data at all, it returns nothing. It is really possible I am doing a lot incorrectly. This is how it looks, i think the quotes are ok: String mySQL = "SELECT * FROM tbl_test WHERE _id= '" + id + "'"; I read about the IEnumberable<struct> that you suggested above, but I have to read more to really understand it. What is the difference between the IEnumberable and using a loop through dataset like I did?
Anna-Karin
I have to admit, I don't really have any experience using a data adapter like you're using, since I pretty much exclusively use LINQ to Entities. And your included code doesn't really show enough for me to make a very educated guess. Have you tried stepping through it to capture the value in your `mySQL` string and then see what happens when you run that same SQL on the database, to make sure you're actually getting something back?
StriplingWarrior
No, I haven't tried stepping through anythingI am a real newbie with this. I just want a web service to use to test a application that I am developing in java, so I don't know a alot about structs. I thought this was the simplest way to get a group of data with a webservice. I have to read up on this and start over from square one. Thanks for your help, now I have a lot more to google!
Anna-Karin
For something simple like you're describing, I'd suggest you look at LINQ to SQL. It will give you a really easy way to get data out of a SQL database without having to hand-write any SQL strings. And rather than structs, most people use "POCO"s: plain old C# objects. Good luck... and be sure to accept an answer. ;-)
StriplingWarrior
Thanks again, and I will look into POCO and LINQ!
Anna-Karin