简体   繁体   中英

stored procedure not getting parameter

Hi I have a stored procedure and a connection to the database. I have other similar code on my website that works just fine but for the life of me I cannot get this to work. I want the username of the person logged in to be passed as a parameter. I can get it stored in a session variable. I wasnt sure how to transfer it from the session variable to the parameter so I put it into a label and sent it that way. It shows that it is getting that far but everytime I just get the message 'nothing found' I have checked the stored procedure and that seems fine to me. Below is the code and stored procedure! please help!

        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());
                 }
            }


            }

stored procedure

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

 )
 AS
 BEGIN


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

EDIT * If I change the stored procedure and delete the ' Where [UserName] = @user ' line it brings in every user detail without any problem so I think it maybe something with this line or the command.Parameters.AddWithValue("@user", userLabel.Text.Trim()); line that is causing me the problems

Try setting

command.CommandType = CommandType.StoredProcedure;

before

 MyConnection.Open();

Also don't call myDataReader.Read(); if you are going to set the myDataReader as the data source for gridview. That will make it skip a row and if result has only one row then the grid will display nothing.

When adding command parameters of text type (varchar, nvarchar) ADO.NET works best when you supply the length of the text value. Try adding the parameter and then setting the length property and then assigning the value property, rather than using AddWithValue.

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