none
LINQ to SQL problem cannot assign sql query result to variable RRS feed

  • Question

  • Hi all,

    I have a linq datasource and query as below
    Code:
    protected void Page_Load(object sender, EventArgs e)
        {
            //lblMessage.Text = "";
      
            string User = Request.ServerVariables["LOGON_USER"];
            usernamesDataContext db = new usernamesDataContext();
            var query = (from USER_NAME in db.USER_NAME
                        where USER_NAME.Username == User
                         select new { Authed = USER_NAME.Department == "BI" ? "1" : "0" }).Distinct();
    
            
            GridView2.DataSource = query;
            GridView2.DataBind();
    
        
        }


    I would like to be able to assign the result of the query to a variable so that i can use this to enable disable some text boxes/labels/buttons etc based on a users security privilages.
    The query will only ever produce 1 coloumn and 1 row.

    Ideally im looking to have something similar to the below code
    Code:
      if (query = 0)
            {
                FormView1.FindControl("TextBox1").Visible = false;
            }
            if (query = 1)
            {
                FormView1.FindControl("TextBox1").Visible = true;
            }


    The above code throws the following error...

    Quote:
    Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0029: Cannot implicitly convert type 'int' to 'System.Linq.IQueryable<AnonymousType#1>' Source Error: Line 41: Line 42: Line 43: if (query = 0) Line 44: { Line 45: FormView1.FindControl("TextBox1").Visible = false;


    If you could help i would be very grateful.

    Many thanks,
    Andy

    • Edited by aown61 Thursday, November 17, 2011 4:00 PM
    Thursday, November 17, 2011 3:52 PM

Answers

  • No matter i have solved this with help from Fernando.

     

    IEnumerable<string> query = (from USER_NAME in db.USER_NAME
                                         where USER_NAME.Username == User
                                         select (USER_NAME.Department == "BI") ? "1" : "0");
    
            string userid = query.FirstOrDefault();
            if (userid == "0")
            {
                FormView1.FindControl("TextBox1").Visible = false;
                FormView1.FindControl("ScoreLbl").Visible = false;
                FormView1.FindControl("PriorityLbl").Visible = false;
                FormView1.FindControl("PriorityTextBox").Visible = false;
            }
            if (userid == "1")
            {
                FormView1.FindControl("TextBox1").Visible = true;
                FormView1.FindControl("ScoreLbl").Visible = true;
                FormView1.FindControl("PriorityLbl").Visible = true;
                FormView1.FindControl("PriorityTextBox").Visible = true;
            }
    

    Slightly chnaged fernando's query suggestion, and created a string variable 'userid' assigned query.FirstOrDeafult value.

     

    Thanks for the help Fernando

    • Marked as answer by aown61 Friday, November 18, 2011 10:43 AM
    Friday, November 18, 2011 10:43 AM

All replies

  • Hi Andy;

    Try the query like this:

    var query = (from USER_NAME in db.USER_NAME
    	where USER_NAME.Username == User
    	select (USER_NAME.Department == "BI") ? 1 : 0 ).FirstOrDefault();
    


    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Thursday, November 17, 2011 4:52 PM
  • Many thanks for your time Fernando,

    How would I go around assigning the result of the query to a variable?

    e.g.

    string UserID = query

      (returns the query text, not the resulting value)

    there doesnt seem to be any way i could do something like query.results.

     

    Hopefully you understand what im after here.

     

    Andy

    Friday, November 18, 2011 9:30 AM
  • No matter i have solved this with help from Fernando.

     

    IEnumerable<string> query = (from USER_NAME in db.USER_NAME
                                         where USER_NAME.Username == User
                                         select (USER_NAME.Department == "BI") ? "1" : "0");
    
            string userid = query.FirstOrDefault();
            if (userid == "0")
            {
                FormView1.FindControl("TextBox1").Visible = false;
                FormView1.FindControl("ScoreLbl").Visible = false;
                FormView1.FindControl("PriorityLbl").Visible = false;
                FormView1.FindControl("PriorityTextBox").Visible = false;
            }
            if (userid == "1")
            {
                FormView1.FindControl("TextBox1").Visible = true;
                FormView1.FindControl("ScoreLbl").Visible = true;
                FormView1.FindControl("PriorityLbl").Visible = true;
                FormView1.FindControl("PriorityTextBox").Visible = true;
            }
    

    Slightly chnaged fernando's query suggestion, and created a string variable 'userid' assigned query.FirstOrDeafult value.

     

    Thanks for the help Fernando

    • Marked as answer by aown61 Friday, November 18, 2011 10:43 AM
    Friday, November 18, 2011 10:43 AM