-1

I recently asked a questtion on how o write a query and thankfully you guys answered me (Thank you)

My Last Question

Im working on a WML site so I need to pass the values from the query I found a way to do so (shown below in code) , but It didnt work.

    <%   
//this Query was provided earlier in the previous question.

        string queryString = "select st.firstname + ' ' + st.lastname,se.year, c.coursename,c.NumberOfCredits,ri.mark  from Students st inner join RegisteredIn ri on ri.StudentId=st.id inner join Semester se on se.id=ri.SemesterId inner join Courses c on c.id=se.id ";

the Way I found but didnt work

         using (System.Data.SqlClient.SqlConnection connection = new System.Data.SqlClient.SqlConnection(connectionString))
         {

            System.Data.SqlClient.SqlCommand command = new System.Data.SqlClient.SqlCommand(queryString, connection);

            command.Parameters.AddWithValue("@StudentId",StudentId);

                connection.Open();
                System.Data.SqlClient.SqlDataReader reader = command.ExecuteReader();
                if (reader.HasRows)
                {
                    reader.Read();
                    string _firstname = reader[0].ToString();
                    string _lastname = reader[1].ToString();
                    string _coursename = reader[2].ToString();
                    string _credits = reader[3].ToString();
                    string _mark = reader[4].ToString();

                    string url = string.Format("~/StudentInfo.aspx?StudentId={0}&firstname={1}&lastname={2}&coursename={3}&credits={4}&mark={5}", StudentId, _firstname, _lastname, _coursename, _credits, _mark);
                    Response.Redirect(url);

    %>

The page reciving the paramaters will be somthing like

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="StudentInfo.aspx.cs" Inherits="StudentInfo" %>

<wml>
    <card>



        <p><% Response.Write(Request.Params["StudentId"]);%></p>
        <p><b>firstname:</b> <% Response.Write(Request.Params["firstname"]);%></p>
        <p><b>lastname:</b> <% Response.Write(Request.Params["lastname"]);%></p>
        <p><b>coursename:</b> <% Response.Write(Request.Params["coursename"]);%></p>
       <p><b>credits:</b> <% Response.Write(Request.Params["credits"]);%></p>
        <p><b>Mark:</b> <% Response.Write(Request.Params["mark"]);%></p>

    </card>
</wml>

the result will be somthing like

First Semester  2010
Student : Arin Rizk

Course Name     No. of Credit      Mark
    AAA                3            65
    BBB                3            23
    CCC                3            65
    DDD                3            58
    EEE                3            70

Your GPA for this semster is 3.12

My Questions are: - How to extract the values from the query and into the URL ? - How I will display multiple values in WML or in ASP ? (as the results will be multiple lines and of values)

Thank you in advance

Community
  • 1
  • 1
arin
  • 602
  • 1
  • 6
  • 17
  • kindly please tell me why I got -2 , I would like to fix it. – arin Apr 16 '12 at 16:50
  • 1
    -1. Arin, could you please make you sample 7 lines long? If you want to post something for code review there is special site for it - http://codereview.stackexchange.com/. Also if you can check your shift/caps-lock (it seem to work semi-randomly on your keyboard) it would make question to look better. Finally reread your question and see if it is clear what you are asking ("did not work"/"where is a problem" are often symptom of missing question). – Alexei Levenkov Apr 16 '12 at 16:52
  • Sorry I edited the question thank you Alexei – arin Apr 16 '12 at 17:04
  • 1
    What exactly doesn't work? Right now the redirect is commented out. Does it redirect but the querystring is blank? – scott.korin Apr 16 '12 at 17:24
  • 1
    Also, I don't see where the @StudentId parameter is used on your SQL, so it looks like it's getting all students. – scott.korin Apr 16 '12 at 17:26
  • @ scott , @StudentId is extracted from the URL but as I read in an article that I need to add this too , the results will be a list of courses registered for the specific user in a specific semester and year. I added a sample of the expected results – arin Apr 16 '12 at 17:41
  • @scott an error is shown (Invalid attempt to read when no data is present. ) Line 29: string _firstname = reader[0].ToString(); – arin Apr 16 '12 at 17:51
  • 1
    @arin, your sample is still insanely long, there is absolutely no need to post all your code - make it smallest possible and preferably compilable, show particular problem that is reflected in your title... Your last comments implies that something fail on some line 29 that has nothing to do with your question's title. – Alexei Levenkov Apr 16 '12 at 18:15

1 Answers1

1

Your code as it is now will read only first row from database. To read all rows, you must call Reader.Read() in a loop, something like:

while(reader.Read())
{
    string _firstname = reader[0].ToString();
    string _lastname = reader[1].ToString()
    ...
}

But I'm afraid that passing all this information as parameter in URL really is not good idea. You would have to join somehow results from all rows and on the other page split them back to individual values, which seems to me superfluous. Moreover, URL length is usually limited so if your query returns more than just a few rows, it will not work. Why do you want to transfer whole query result in URL? It's more common (and much easier to do) to transfer in URL just identifier of student (or whatever you want to show) and on the second page read data again from database.

UPDATE: If I understand you correctly, your actual question is not how to pass result of database query in querystring to another page, but you want to display result of your query on page. First step is to add appropriate control (Label, DataGridView, etc.) on the form of second page. Then depending on what control you have used you can either bind control to data source, or add data to control manually in method such as Page_Load in StudentInfo.aspx file. But since this is completely different topic than what you were asking first, I suggest you to ask another question. Also this topic was dicsussed here many times, so you may take a look on these questions first.

Ňuf
  • 6,027
  • 2
  • 23
  • 26
  • ok I got what you mean , but I tired to take the results from the Query directly to the show it in the page (in other words show them in the same page as a WML card) but its not working , tell me if I want as u said I want to read the data from the database how to store it in variable so I can print them out ? – arin Apr 16 '12 at 18:07