简体   繁体   中英

Show Linq-to-SQL table rows in separate text boxes

I have an array where it gets its data from a Linq-to-SQL query. I am using ASP.NET Webforms.

We say I have following table:

tblNVRchannels :

ID      fltNVR     fltNamn
1       75         Kam1-75
2       75         Kam2-75  
3       75         Kam3-75  
4       75         Kam4-75  
5       76         Kam1-76  
6       76         Kam2-76  
7       76         Kam3-76  

C# code:

using (myDataContext sls = new myDataContext())
{
     IQueryable<tblNVRChannel> channels= (from channel in sls.tblNVRChannels
                                      where kanal.fltNVR == 75
                                      select channel);

     tblNVRChannel[] kanArray = channels.ToArray();

     foreach (var item in channelArray)
     {
         TextBox1.Text = item.fltNamn;
         TextBox2.Text = item.fltNamn;
         TextBox3.Text = item.fltNamn;
         TextBox4.Text = item.fltNamn;
         TextBox5.Text = item.fltNamn;
     }
}

Using above query I get the values from the table but I cannot show each item in separate textboxes, what I get is the last item of the query (Kam4-75) in each textbox, but I want Kam1-74 in textbox1, kam2-75 in textbox2 and so on.

There are some examples using FirstOrDefault() but when I tried it I get only the first row which does not work for me, though I am not sure.

Populating textboxes using LINQ2SQL and Linq to Sql: How to Fetch specific row using stored procedure and display columns data into textboxes

I tried to assign textbox.text data using a loop but I could not.

I want something like below in final result:

   TextBox1.Text = kam1-75;
   TextBox2.Text = kam2-75;
   TextBox3.Text = kam3-75;
   TextBox4.Text = kam4-75;
   TextBox5.Text = NULL;

Try this code.

using (myDataContext sls = new myDataContext())
{
 IQueryable<tblNVRChannel> channels= (from channel in sls.tblNVRChannels
                                  where kanal.fltNVR == 75
                                  select channel);
 tblNVRChannel[] kanArray = channels.ToArray();
 var textbox = GetAll(this, typeof(TextBox));
        // Do whatever you want to do with your textbox.
        foreach (var item in channelArray)
         {
             foreach(Control c in textbox)
            {
                if (c is TextBox)
                { 
                    var tx = ((TextBox) x);
                    if (string.IsNullOrEmpty(tx.Text))
                    {
                        tx.Text = item.fltNamn;
                        break;
                    }
                }
            }
        }

}

public IEnumerable<Control> GetAll(Control control, Type type)
    {
        var controls = control.Controls.Cast<Control>();

        return controls.SelectMany(ctrl => GetAll(ctrl, type))
                                  .Concat(controls)
                                  .Where(c => c.GetType() == type);
    }

Make changes in your code according to your requirement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM