Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old October 17th, 2005, 01:25 PM
Authorized User
 
Join Date: Oct 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default URL Param Problem

I have a page that is working correctly and I am trying to add a URL parameter that is to be used in a sql statement. I am having serious problems getting what seems to be a simple task up and running.

Here is the error I'm getting

CS0120: An object reference is required for the nonstatic field, method, or property 'System.Web.HttpRequest.QueryString'

Thanks in advance:

Here is my code

<code>

<script runat="server">

     string ConnectionString = "server=edev03;database=on_track;user id=johnrose; password=jrontrack";
     string @Set_Name = HttpRequest.QueryString["Set_Name"];
     string SelectCommand = "SELECT smile_data.qsect_key, smile_data.ques_id, smile_data.esetq_id, smile_data.Ques_Order, smile_data.Q_Section, smile_data.Q_Text FROM Smile_Data WHERE Smile_Data.Set_Name='smile' ORDER BY Smile_Data.Set_Name, Smile_Data.Sect_Order, Smile_Data.Ques_Order";

          void Page_Load(object sender, EventArgs e) {

            if (!Page.IsPostBack) {

              BindGrid();
            }
          }


<code/>

 
Old October 17th, 2005, 11:51 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Not sure why that doesnt work, try just using:
string @Set_Name = Request.QueryString["Set_Name"];

 
Old October 18th, 2005, 07:24 AM
Authorized User
 
Join Date: Oct 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for the response,

That's what I had origanally, but it gave me the same error. I'm getting kinda desperate. Is there a namespace that I am not including?

 
Old October 18th, 2005, 08:36 AM
Authorized User
 
Join Date: Oct 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I tried something a little different and maybe this error will be a little easier to get help with ?

error: System.Data.SqlClient.SqlException: Must declare the variable '@Set_Name'.

It is pointing out the MyCommand.Fill(ds); line
Thanks again for your help in advance.

private string @Set_Name;
string ConnectionString = "server=edev03;database=on_track;user id=johnrose; password=jrontrack";
string SelectCommand = "SELECT smile_data.qsect_key, smile_data.ques_id, smile_data.esetq_id, smile_data.Ques_Order, smile_data.Q_Section, smile_data.Q_Text FROM Smile_Data WHERE Smile_Data.Set_Name=@Set_Name ORDER BY Smile_Data.Set_Name, Smile_Data.Sect_Order, Smile_Data.Ques_Order";


void Page_Load(object sender, EventArgs e) {

          if (!Page.IsPostBack) {
               BindGrid();

                     }

              else this.Set_Name = this.Request.QueryString["Set_Name"];
         }


public void Button_SubmitOrder_Click(object sender, EventArgs e)
   {

   foreach(DataGridItem dgi in DataGrid2.Items)
    {

     string In_Response = ((TextBox)dgi.Cells[4].FindControl("Response")).Text;
     string In_Rating = ((RadioButtonList)dgi.Cells[3].FindControl("GRID_RadioButtonList")).SelectedValu e;
     string In_SectionKey = dgi.Cells[7].Text;
     string In_EQID = dgi.Cells[5].Text;
     string In_QID = dgi.Cells[6].Text;

     SqlConnection myConnection = new SqlConnection(ConnectionString);
     SqlCommand UpdateCommand = new SqlCommand();
     UpdateCommand.Connection = myConnection;

     UpdateCommand.CommandText = "INSERT INTO Smile_Results(Section_ID, Response, Rating, ESetQ_ID, Question_ID) VALUES (@Q_SectionKey, @Response, @Rating, @EQID, @QID)";

     UpdateCommand.Parameters.Add("@Response", SqlDbType.VarChar, 150).Value = In_Response;
     UpdateCommand.Parameters.Add("@Rating", SqlDbType.VarChar, 150).Value = In_Rating;
     UpdateCommand.Parameters.Add("@Q_SectionKey", SqlDbType.VarChar, 50).Value = In_SectionKey;
     UpdateCommand.Parameters.Add("@EQID", SqlDbType.VarChar, 50).Value = In_EQID;
     UpdateCommand.Parameters.Add("@QID", SqlDbType.VarChar, 50).Value = In_QID;

    // execute the command
       try {
            myConnection.Open();
            UpdateCommand.ExecuteNonQuery();
           }
       catch (Exception ex) {
            Message.Text = ex.ToString();
             }
       finally {
             myConnection.Close();
               }

           }
       }

  void BindGrid() {

      SqlConnection myConnection = new SqlConnection(ConnectionString);
      SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);

      DataSet ds = new DataSet();
      myCommand.Fill(ds);

      DataGrid2.DataSource = ds;
      DataGrid2.DataBind();

 }

 
Old October 18th, 2005, 09:11 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

That is a sql server variable declaration. You have to assign the query stirng to a .NET variable then use it to build your sql string.

string Set_Name = HttpRequest.QueryString["Set_Name"];

sqlstring .. " ....WHERE somecol = " + Set_Name



 
Old October 18th, 2005, 09:28 AM
Authorized User
 
Join Date: Oct 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Should I include another namespace? I have included
<%@ Page Language="C#" Debug="true" ResponseEncoding="iso-8859-1" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<%@ import Namespace="System.Collections" %>
<%@ import Namespace="System.Web.UI" %>

I am getting this error:

CS0246: The type or namespace name 'sqlstring' could not be found (are you missing a using directive or an assembly reference?)


 
Old October 18th, 2005, 09:35 AM
Authorized User
 
Join Date: Oct 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I found the namespace: <%@ import Namespace="System.Data.SqlTypes" %>

I am getting a missing ";" error. Is my sql syntax incorrect?

sqlstring SelectCommand =
"SELECT smile_data.qsect_key, smile_data.ques_id, smile_data.esetq_id, smile_data.Ques_Order, smile_data.Q_Section, smile_data.Q_Text
FROM Smile_Data
WHERE Smile_Data.Set_Name="+Set_Name
"ORDER BY Smile_Data.Set_Name, Smile_Data.Sect_Order, Smile_Data.Ques_Order";

 
Old October 18th, 2005, 10:11 AM
Authorized User
 
Join Date: Oct 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm not sure if this will give me the desired result but I took out the
" + Set_Name
so now it looks like this:

SqlString SelectCommand =
"SELECT smile_data.qsect_key, smile_data.ques_id, smile_data.esetq_id, smile_data.Ques_Order, smile_data.Q_Section, smile_data.Q_Text
FROM Smile_Data
WHERE Smile_Data.Set_Name=Set_Name
ORDER BY Smile_Data.Set_Name, Smile_Data.Sect_Order, Smile_Data.Ques_Order";

I don't know if it works or not b/c I'm getting:
CS1502: The best overloaded method match for 'System.Data.SqlClient.SqlDataAdapter.SqlDataAdapt er(string, string)' has some invalid arguments

void BindGrid() {

  SqlConnection myConnection = new SqlConnection(ConnectionString);
  SqlDataAdapter myCommand = new SqlDataAdapter(SelectCommand, myConnection);

  DataSet ds = new DataSet();
  myCommand.Fill(ds);

  DataGrid2.DataSource = ds;
  DataGrid2.DataBind();
 }

Do I need to do some kind of casting in order to make the SqlString fit the 'String' argument?

Sorry for taking so much of your time but you have been very helpful, and I haven't had much luck with getting help on this subject.


 
Old October 18th, 2005, 10:34 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

   You are confusing .NET code with sql syntax, this is the main cause of your problems. Remeber, you are using .NET code to contruct the sql string.
   You are referencing set_name in the sqlstring. You can't, set_name is a string defined in the .NET code.

Let me give you an example, I don't know C# I use VB, but you can get the idea:
Dim sqlStr as string
Dim FName, LName as string

FName = "Joe"
LName = "Blow"

sqlStr = "SELECT* From Customers WHERE LastName = " + LName + " AND " _
         "FirstName = " + FName

This simple example should make it more clear to you. You are simply builing the sql string by concatenating values from string variables.







 
Old October 19th, 2005, 03:36 PM
Authorized User
 
Join Date: Oct 2005
Posts: 27
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thank you, that helped quite a bit.

I am reading in the querystring correctly now. But I still can't get the SQL statement to work. I think this is just a syntax thing in c#.

CS0236: A field initializer cannot reference the nonstatic field, method, or property 'ASP.MatrixSmile_aspx.Set_Name'

I get this error whether I use a hard coded variable or the querystring variable.

string SelectCommand =
"SELECT smile_data.qsect_key, smile_data.ques_id, smile_data.esetq_id, smile_data.Ques_Order, smile_data.Q_Section, smile_data.Q_Text
FROM Smile_Data
WHERE Smile_Data.Set_Name="+@Set_Name+
"ORDER BY Smile_Data.Set_Name, Smile_Data.Sect_Order, Smile_Data.Ques_Order";

Any c#'ers out there have any ideas?

Thanks again







Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with URL rewrite mlcy ASP.NET 2.0 Professional 3 June 21st, 2007 06:05 PM
URL Redirection Problem rch123 Need help with your homework? 0 June 18th, 2007 11:20 AM
Navigation url problem? Hannibal ASP.NET 2.0 Basics 1 December 14th, 2006 05:23 AM
URL encode problem mat41 Classic ASP Professional 3 August 23rd, 2006 07:46 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.