locked
Assign SQL query output to variable RRS feed

  • Question

  • User-1662567569 posted
    I have a page that allows users to filter db results based on the selection of dropdown lists; server, customer and error message.
    The database returns documents with information around issues and problem resolutions. A doc could apply to an entire customer or a server. A server always has a customer.
    If a user filters by server I need the gridview to display all rows associated with the server as well as rows associated with the customer.
    I have written the following code to get the customer name from the server name when server name is selected. Once assigned to a variable I will use it in the sqldatasource associated withthe gridview.
    Problem is that this could returns a syntax error even though it compiles OK...
    Error...

    Incorrect syntax near '='.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

    Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '='.

    Source Error: 

    Line 26:             cmd.CommandText = "SELECT Customer from Devices WHERE + svrName + = ID";
    Line 27:             con.Open();
    Line 28: SqlDataReader reader = cmd.ExecuteReader(); Line 29:             int getCust = (int)reader["Customer"];
    Line 30:             Label3.Text = Convert.ToString(getCust);

    
    
    Code...
     protected void Page_Load(object sender, EventArgs e)
    {
    string cusName = Request.QueryString["getCustomer"];
    string svrName = Request.QueryString["getServer"];
    string errorMsg = Request.QueryString["errorMessage"];

    if (svrName != null)
    {
    string strConn = ConfigurationManager.ConnectionStrings["trackerConnectionString1"].ConnectionString;
    SqlConnection con = new SqlConnection(strConn);
    SqlCommand cmd = new SqlCommand();
    cmd.Connection = con;
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = "SELECT Customer from Devices WHERE + svrName + = ID";
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    int getCust = (int)reader["Customer"];
    Label3.Text = Convert.ToString(getCust);





    }

    }
    
    
    Thanks for your help!
    Wednesday, July 20, 2011 11:54 AM

Answers

  • User-1516073966 posted

    I couldn't understand what exactly is your requirement or your problem but there is an error your query.

    1. In below query is svrName value is column name in Devices table?

    "SELECT Customer from Devices WHERE + svrName + = ID"

    Probably your query would be as follows: try the below one

    "SELECT Customer from Devices WHERE ID = " + svrName;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 20, 2011 12:23 PM