如何在ASPX页面中绑定存储过程的值 [英] How to bind values from stored procedure in ASPX page

查看:60
本文介绍了如何在ASPX页面中绑定存储过程的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含3个选择查询的存储过程。在C#代码中,我调用了这个存储过程,结果存储在数据集中。在aspx形式中,我使用了转发器控制,并且有结果应绑定的控件。但只有第一个变量,即问题被绑定,而其他两个则没有。

------------------------ -------------------------------------------------- -----

存储过程代码:

------------------------- -------------------------------------------------- ----

CREATE PROCEDURE Sp_GetUserStat

@RegId int

AS

SELECT COUNT(*)AS question FROM tblQuestion WHERE RegID = @ RegId;

SELECT COUNT(*)AS answer FROM tblAnswer WHERE RegID = @ RegId;

SELECT Points AS point FROM tblPoints WHERE RegID = @ RegId ;

返回0

------------------------------- ------------------------------------------------ <无线电通信/>
C#代码:GetUserStat(LogicLayer li)

---------------------------- -------------------------------------------------- -

  public  DataSet GetUserStat(LogicLayer li)
{
尝试
{
cmd = new SqlCommand( Sp_GetUserStat,con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue( @ RegId,li.RegId);
adp = new SqlDataAdapter(cmd);
ds = new DataSet();
adp.Fill(ds, UserStat);
return ds;
}
catch (例外)
{

;
}
最后
{
cmd.Dispose();
}
}



--------------------- -------------------------------------------------- --------

C#代码:Get_UserStat(LogicLayer li)

------------------ -------------------------------------------------- -----------

  public  DataSet Get_UserStat(LogicLayer li)
{
return dal.GetUserStat(li);
}





---------------------- -------------------------------------------------- -------

C#代码:CodeBehind

----------------------- -------------------------------------------------- ------

  public   partial   class  UserProfile:System.Web.UI.Page 
{
LogicLayer li = new LogicLayer();

受保护 void Page_Load(对象发​​件人,EventArgs e)
{
如果(!IsPostBack)
{
li.RegId = Convert.ToInt32(会话[ RegId]);
lstUserProfile.DataSource = li.Get_User_Profile(li);
lstUserProfile.DataBind();
rptUserStat.DataSource = li.Get_UserStat(li);
rptUserStat.DataBind();





}
}
}



-------------------------------------------------- ----

ASPX代码:

--------------------------- ---------------------------

 <   asp:Repeater     ID   =  rptUserStat    runat   =  server   >  
< ItemTemplate >
< < SP class =code-leadattribute> div class = ul_list ul_list-icon-ok >
< ul >
< li > < i = icon-question-sign > < / i > < a href = user_questions.html > 问题< span > < span > < span class =code-pagedirective><% #Eval( 问题 )== null 0 :Eval( question)%> < / span > < / span > < / a > < / li >
< li > < i class = icon-comment > < / i > < ; a href = user_answers.html > 答案< span < span class =code-keyword>> < span > <% #Eval( answer)== null 0:Eval( answer)% > < / span > < / span > < span class = code-keyword>< / a > < / li >
<% - < li>< i class =icon-star>< / i>< ; a href =user_favorite_questions.html>收藏的问题< span> (< span>< / span>)< / span>< / a>< / li> - %>
< li > < i < span class =code-attribute> class = icon-heart > < / i > < a href = user_points。 html > 积分& span > < span > <% - <%#Eval(point)== null?0:Eval(point)%> < span class =code-pagedirective> - %> < / span > < / span > < / a > < / li < span class =code-keyword>>
<% - < li>< i class =icon-asterisk>< / i>最佳答案< span> (< span>< / span>)< / span>< / li> - %>
< / ul >
< / div >
< / ItemTemplate >
< / asp:Repeater >





我尝试过:



我试图使用DataBinder.Eval(Container.DataItem,变量)绑定但是没有用。

解决方案

您应该从SQL返回一个select语句,以便正确绑定返回的表。只需将这些计数存储在临时变量中,然后在一个语句中选择最后一个并尝试。


I have a stored procedure that contains 3 select queries.In C# code I have called this stored procedure and the result is stored in a dataset. In aspx form I have used repeater control and in that there are controls to which the result should be binded. But only the first variable i.e, "question" is binded and other two are not.
-------------------------------------------------------------------------------
Stored procedure code:
-------------------------------------------------------------------------------
CREATE PROCEDURE Sp_GetUserStat
@RegId int
AS
SELECT COUNT(*) AS question FROM tblQuestion WHERE RegID=@RegId ;
SELECT COUNT(*) AS answer FROM tblAnswer WHERE RegID=@RegId;
SELECT Points AS point FROM tblPoints WHERE RegID=@RegId;
RETURN 0
-------------------------------------------------------------------------------
C# code : GetUserStat(LogicLayer li)
-------------------------------------------------------------------------------

public DataSet GetUserStat(LogicLayer li)
      {
          try
          {
              cmd = new SqlCommand("Sp_GetUserStat", con);
              cmd.CommandType = CommandType.StoredProcedure;
              cmd.Parameters.AddWithValue("@RegId", li.RegId);
              adp = new SqlDataAdapter(cmd);
              ds = new DataSet();
              adp.Fill(ds, "UserStat");
              return ds;
          }
          catch (Exception)
          {

              throw;
          }
          finally
          {
              cmd.Dispose();
          }
      }


-------------------------------------------------------------------------------
C# code : Get_UserStat(LogicLayer li)
-------------------------------------------------------------------------------

public DataSet Get_UserStat(LogicLayer li)
      {
          return dal.GetUserStat(li);
      }



-------------------------------------------------------------------------------
C# code : CodeBehind
-------------------------------------------------------------------------------

public partial class UserProfile : System.Web.UI.Page
  {
      LogicLayer li = new LogicLayer();

      protected void Page_Load(object sender, EventArgs e)
      {
          if (!IsPostBack)
          {
              li.RegId = Convert.ToInt32(Session["RegId"]);
              lstUserProfile.DataSource = li.Get_User_Profile(li);
              lstUserProfile.DataBind();
              rptUserStat.DataSource = li.Get_UserStat(li);
              rptUserStat.DataBind();





          }
      }
  }


------------------------------------------------------
ASPX code:
------------------------------------------------------

<asp:Repeater ID="rptUserStat" runat="server" >
                                      <ItemTemplate>
                                              <div class="ul_list ul_list-icon-ok">
                                      <ul>
                                          <li><i class="icon-question-sign"></i><a href="user_questions.html">Questions<span> ( <span><%#Eval("question")==null?"0":Eval("question") %></span> ) </span></a></li>
                                          <li><i class="icon-comment"></i><a href="user_answers.html">Answers<span> ( <span><%#Eval("answer")==null?"0":Eval("answer") %></span> ) </span></a></li>
                                          <%--<li><i class="icon-star"></i><a href="user_favorite_questions.html">Favorite Questions<span> ( <span></span> ) </span></a></li>--%>
                                          <li><i class="icon-heart"></i><a href="user_points.html">Points<span> ( <span><%--<%#Eval("point")==null?"0":Eval("point") %>--%></span> ) </span></a></li>
                                          <%--<li><i class="icon-asterisk"></i>Best Answers<span> ( <span></span> ) </span></li>--%>
                                      </ul>
                                  </div>
                                      </ItemTemplate>
                                  </asp:Repeater>



What I have tried:

I have tried to Bind using DataBinder.Eval(Container.DataItem,"variable") but did not work.

解决方案

You should return in one select statement from SQL so that the table returned will be correctly bound. Just store those counts in temp variables and select at the last in one statement and try.


这篇关于如何在ASPX页面中绑定存储过程的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆