Привет, у меня есть хранимая процедура и подключение к базе данных. У меня есть другой подобный код на моем веб-сайте, который работает просто отлично, но на всю жизнь я не могу заставить это работать. Я хочу, чтобы имя пользователя, вошедшего в систему, передавалось как параметр. Я могу сохранить его в переменной сеанса.
Я не был уверен, как передать его из переменной сеанса в параметр, поэтому я поместил его в метку и отправил таким образом. Это показывает, что это далеко, но каждый раз, когда я просто получаю сообщение «ничего не найдено»
Я проверил хранимую процедуру, и мне кажется, что это нормально. Ниже приведен код и хранимая процедура! пожалуйста, помогите!
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
РЕДАКТИРОВАТЬ *
Если я изменю хранимую процедуру и удаляю
'Where [UserName] = @user'
линия, которая вносит в каждую деталь пользователя без каких-либо проблем, поэтому я думаю, что это может быть что-то с этой линией или
command.Parameters.AddWithValue ("@ user", userLabel.Text.Trim ());
линия, которая вызывает у меня проблемы