LINQ TO SQL urgent help please

Last post 11-07-2009 10:53 AM by fayaz_3e. 6 replies.

Sort Posts:

  • LINQ TO SQL urgent help please

    11-07-2009, 1:19 AM
    • Member
      14 point Member
    • nwallaq
    • Member since 04-12-2009, 8:29 AM
    • Posts 80

    question



    protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
        {




            MA_TECHDataContext ma10 = new MA_TECHDataContext();

            var validity = from c in ma10.customer_infos
                            where c.customer_lname ==  TextBox2.Text.ToString()
                            select new { c.customer_lname, c.customer_ID };


            GridView1.DataSource = validity;
            GridView1.DataBind();


           // TextBox4.Text = Convert.ToString( validity.Single() );

            Array ab;

                  
            ab = validity.ToArray();

            TextBox4.Text =  ab.GetValue(0).ToString();
                
            
        i want the last name and id into individual strings, so i can display them in label and textboxes and where ever i want.


    but the result is like  """             { customer_lname = 222       , customer_ID = 2          }       """"""""""""


    ??

    please help

     

  • Re: LINQ TO SQL urgent help please

    11-07-2009, 5:17 AM
    • Member
      28 point Member
    • skphadtare
    • Member since 08-04-2009, 7:45 AM
    • Posts 9

    can you try this out... to get last record in the output.

    var temp =validity.LastOrDefault();

    if(null !=  temp)
                    {  TextBox4.Text =  temp. customer_lname + “  ” + temp.customer_ID.ToString();}


    Thanks

    Sujay,

    (Please mark as an aswer if this solves your problem)

  • Re: LINQ TO SQL urgent help please

    11-07-2009, 5:50 AM
    • Participant
      1,641 point Participant
    • vishwaraj1
    • Member since 11-07-2008, 7:44 PM
    • India
    • Posts 424


    do like this.....
    this will solve your problem..
    
     // do like this...
    // this is to get the last record from the records..
    var last = query.LastOrDefault();
                foreach (var v in last)
                {
                    textbox1.text = v.customer_id;
    label1.text= v.customer_name;
    
                }
                   
    
    the name of the database columns will appear when you will put the . after v
    
    if you face any problem please let me know...




    Regards:

    Vishwaraj Malik

    VB to C# Converter


    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: LINQ TO SQL urgent help please

    11-07-2009, 7:29 AM
    • Member
      14 point Member
    • nwallaq
    • Member since 04-12-2009, 8:29 AM
    • Posts 80

    have a prob in did this coding on ur advice and PLEASE READ ERROR

    private void ShowUsers()
        {
            try
            {

                Label1.Text = Session["username"].ToString();
                Label2.Text = Session["password"].ToString();

                MA_TECHDataContext ma10 = new MA_TECHDataContext();

                var cust_auth = from c in ma10.customer_authentications
                                where ( c.customer_username==Session["username"].ToString() )
                                select new { c.customer_ID };

                GridView1.DataSource = cust_auth;
                GridView1.DataBind();


              //  Label4.Text = cust_auth.ToString();



                var last = cust_auth.LastOrDefault();
                foreach (var v in last)
                {
                    Label4.Text = v.customer_ID;
                   
                
                }


              /*  var last = query.LastOrDefault();  
                # foreach (var v in last)  
    #             {  
    #                 textbox1.text = v.customer_id;  
    # label1.text= v.customer_name;  
    #   
    #             }  
                */




            }

            catch (Exception d)
            {
                Session["error"] = d.ToString();

                Response.Redirect("~/ERROR.aspx");

            }


        }//show products ends



    ERROR:--

    Error    1    foreach statement cannot operate on variables of type 'AnonymousType#1' because 'AnonymousType#1' does not contain a public definition for 'GetEnumerator'   Customer_Page.aspx.cs    54    13   


    PLEASE HELP





  • Re: LINQ TO SQL urgent help please

    11-07-2009, 10:24 AM
    Answer
    • Participant
      1,641 point Participant
    • vishwaraj1
    • Member since 11-07-2008, 7:44 PM
    • India
    • Posts 424

    then try to get the record in string.

    string a;

    stringb;

    foreach(var v in last)

    {

    a= v.customerid;

    b=v.customername;

    }

    Regards:

    Vishwaraj Malik

    VB to C# Converter


    Please: Don't forget to click "Mark as Answer" on the post that helped you. That way future readers will know which post solved your issue.
  • Re: LINQ TO SQL urgent help please

    11-07-2009, 10:46 AM
    • Contributor
      4,269 point Contributor
    • fayaz_3e
    • Member since 09-14-2007, 6:15 AM
    • Hyderabad
    • Posts 883

    nwallaq:
    var last = cust_auth.LastOrDefault();
                foreach (var v in last)
                {
                    Label4.Text = v.customer_ID;
                   
                
                }

    Why are you using foreach loop there?? You are getting last record. Only one record. No looping is required. You can try something like

    Label4.Text = last.customer_ID;

    Fayaz
  • Re: LINQ TO SQL urgent help please

    11-07-2009, 10:53 AM
    Answer
    • Contributor
      4,269 point Contributor
    • fayaz_3e
    • Member since 09-14-2007, 6:15 AM
    • Hyderabad
    • Posts 883

    I asume customer_authentications will have unique customer_username. So you better use FirstOrDefault() instead of last. And you can use lambda expression instead of query. Something like this...

    var cust_auth =  ma10.customer_authentications.Where (u => u.customer_username == Session["username"].ToString());

    Fayaz
Page 1 of 1 (7 items)