1
Answer

How can i select specific records from database in asp MVC?

Amit Kumar

Amit Kumar

7y
180
1
In the following code, I want to retrieve all those records whose Q_ID equals to the ID.
Currently, I Select whole records instead of few.
 
Please help me to solve this problem.
  1. public ActionResult Solution(int ID)  
  2. {  
  3. Answers ans_obj = new Answers();  
  4. List<Ans_Table> dbobj= db.Ans_Table.ToList();  
  5. List<Answers> ansobj = dbobj.Select(x => new Answers  
  6. {  
  7. Answer = x.Answer,  
  8. Q_ID=x.Q_ID,  
  9. U_ID=x.U_ID  
  10. }).ToList();  
  11. return View();  
  12. }
Answers (1)
0
Niradhip Chakraborty

Niradhip Chakraborty

NA 6.5k 526.8k 15y

1) DataReader does not implement any specific interface so that it can
be bindable
2) DataReader is for reading data.So if some how u r able to bind
that,it won't allow u to edit that.So it's of no use.
3) DataReader is forward only,that means u can get a record at a time
but can't traverse back to the prev record.

This code shows how to bind gridview with data reader
Use this code on page load

 
void Page_Load(Object sender, EventArgs e)
{
  String Q;
  Sql command cmd;
  Sql DataReader dr;


  SqlConnection conn = new SqlConnection();

  conn.ConnectionString = ConfigurationManager.ConnectionStrings        ["cn"].ConnectionString.ToString();

conn.Open();
         
Q = "select * from tablename";
              
cmd= New Sqlcommand(Q, conn); 
         
dr = cmd.ExecuteReader();         
         
Gridview1.DataSource = dr;
           
Gridview1.DataBind();
}

 
0
Kirtan Patel

Kirtan Patel

NA 35k 2.8m 15y
When you bind Data reader to gridView


it iterates through  records one by one and All records Rendered One bye one in HTML Table row

so as a result you see data in Grid View .