如何从单选按钮列表中的var获取数据

时间:2013-12-30 13:02:11

标签: c# asp.net linq stored-procedures linq-to-sql

我正在使用linQ to sql和我的.cs页面代码

var rrlist = db.sel_aut_rec(12); //stored procedure

if (rrlist.Count() != 0)
{
rblist1.DataSource = rrlist;  //radio button list
rblist1.DataTextField ="tbl1.j_title";
rblist1.DataValueField ="tbl1.ed_journal_id";
rblist1.DataBind();
}

我的商店程序代码是

ALTER PROCEDURE dbo.sel_aut_rec
@auth_id int
AS
 select tbl1.ed_journal_id,tbl1.j_title from tbl_aut_rec as tbl inner join editor_j_inf as tbl1 on tbl.j_id=tbl1.ed_journal_id where tbl.auth_id=@auth_id order by tbl.aut_rec_id desc

RETURN

我不知道如何从包含许多行的rrlist1中获取特定列

我的问题解决了

var rrlist = db.sel_aut_rec(12).tolist();

3 个答案:

答案 0 :(得分:0)

你的方法应该是,

  1. 更改您的SP并选择ID,名称或任何内容以唯一标识单选按钮。 在你的情况下假设Auth_Id和Auth_name。
  2. 将两个设置为您的列表。
  3. SP:

    ALTER PROCEDURE dbo.sel_aut_rec
    @auth_id int
    AS
    BEGIN
    
    select auth_id as Id, auth_name as Name 
    from tbl_aut_rec as tbl 
      inner join editor_j_inf as tbl1 
        on tbl.j_id=tbl1.ed_journal_id 
    where auth_id=@auth_id order by aut_rec_id desc
    
    END
    

    CS:

    var rrlist = db.sel_aut_rec(Convert.ToInt32(Request.Cookies["authorid"].Value); //stored procedure
    
    if (rrlist !=null)
    {
    rblist1.DataSource = rrlist;  
    rblist1.DataTextField =  "Id"  // You should pick Id and Name 
                                   //{or whatever suits your need} so as 
                                   //  to set the text and value
    rblist1.DataValueField = "Name"
    rblist1.DataBind();
    }
    

答案 1 :(得分:0)

select *将获取所有表中的所有列,因此您需要为

提供正确的列名和列ID
rblist1.DataTextField = "ColumnName" //For display purpose like description field

rblist1.DataValueField = "ColumnName" //For storing in DB like id field name 

请查看此sample

答案 2 :(得分:0)

您也可以使用Linq而不是使用存储过程:

var rrlist = dbo.tbl_aut_rec.Join(dbo.editor_j_inf, a => a.j_id, e => e.ed_journal_id, (a, e) => new { Author = a, Editor e})
.Where(AuthorAndEditor => AuthorAndEditor.Author.auth_id.Equals(Convert.ToInt32(Request.Cookies["authorid"].Value))
.OrderBy(AuthorAndEditor => AuthorAndEditor.Author.aut_rec_id).ToList();

if (rrlist.Count() != 0)
{
rblist1.DataSource = rrlist;  //radio button list
rblist1.DataTextField = "Id"  //how to take particular column from rrlist1 containing many row
rblist1.DataValueField = "Name" //how to take particular column from rrlist1 containing many row
rblist1.DataBind();
}

我在记事本中快速写了它,所以可能包含一个拼写错误。