存储过程没有获取参数

时间:2011-05-29 22:38:49

标签: c# asp.net sql-server-2008 stored-procedures

您好我有一个存储过程和数据库连接。我在我的网站上有其他类似的代码,工作得很好,但对于我的生活,我无法让这个工作。我希望登录的用户名能够作为参数传递。我可以将它存储在会话变量中。 我不确定如何将它从会话变量传递到参数,所以我将它放入标签并以这种方式发送。它表明它已经走得那么远,但每次我只是得到“没有找到”的信息 我检查了存储过程,这对我来说似乎很好。下面是代码和存储过程!请帮忙!

        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.UI;
        using System.Web.UI.WebControls;
        using System.Web.Security;
        using System.Data.SqlClient;
        using System.Configuration;
        using System.Data;

        public partial class RescueOnlyPages_EditRescueDetails : System.Web.UI.Page
        {
            protected void page_PreInit(object sender, EventArgs e)
            {
                MembershipUser user;
                try
                {
                    if (User.Identity.IsAuthenticated)
                    {
                        // Set theme in preInit event
                        user = Membership.GetUser(User.Identity.Name);
                        Session["user"] = user;
                    }

                }
                catch (Exception ex)
                {
                    string msg = ex.Message;
                    //Log error here

                    // We have set theme in web.config to Neutral so if there is
                    // an error with setting themes, an incorrect theme wont be displayed to a customer

                }


            }
            protected void Page_Load(object sender, EventArgs e)
            {
                userLabel.Text = Session["user"].ToString();

                SqlDataReader myDataReader = default(SqlDataReader);

                SqlConnection MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["RescueAnimalsIrelandConnectionString"].ConnectionString);

                SqlCommand command = new SqlCommand("sp_EditRescueDetails", MyConnection);    

                if (!User.Identity.IsAuthenticated)
                {
                }
                else 
                {
                    command.Parameters.AddWithValue("@user", userLabel.Text.Trim());

                }

                 try
               {
                MyConnection.Open();
                command.CommandType = CommandType.StoredProcedure;

                myDataReader = command.ExecuteReader(CommandBehavior.CloseConnection);
                myDataReader.Read();


                GridViewED.DataSource = myDataReader;
                GridViewED.DataBind();

                if (GridViewED.Rows.Count >= 1)
                {

                    GridViewED.Visible = true;
                    lblMsg.Visible = false;           

                }
                else if (GridViewED.Rows.Count < 1)
                {
                    GridViewED.Visible = false;

                    lblMsg.Text = "Your search criteria returned no results.";
                    lblMsg.Visible = true;
                }





                MyConnection.Close();
               }
                 catch (SqlException SQLexc)
                 {
                     Response.Write("Read Failed : " + SQLexc.ToString());
                 }
            }


            }

存储过程

 ALTER PROC [dbo].[sp_EditRescueDetails]
(
   @user nvarchar(50)

 )
 AS
 BEGIN


 SELECT [PostalAddress], [Telephone_No], [Website], [Email] 
 FROM [RescueDetails] 
 Where   [UserName] = @user 
 End

编辑 * 如果我更改存储过程并删除        '哪里[UserName] = @user' 它带来了每个用户细节没有任何问题,所以我认为它可能与这条线或          command.Parameters.AddWithValue(“@ user”,userLabel.Text.Trim()); 引起我问题的那条线

2 个答案:

答案 0 :(得分:6)

尝试设置

command.CommandType = CommandType.StoredProcedure;

之前

 MyConnection.Open();

如果您要将myDataReader.Read();设置为gridview的数据源,也不要致电myDataReader。这将使它跳过一行,如果结果只有一行,那么网格将不显示任何内容。

答案 1 :(得分:0)

添加文本类型(varchar,nvarchar)的命令参数时,在提供文本值的长度时,ADO.NET的效果最佳。 尝试添加参数,然后设置length属性,然后分配value属性,而不是使用AddWithValue。