Gavin Gaughran
Gavin Gaughran

Reputation: 55

ASP.NET C# Display address details if logged in

On my website i am trying to get my site to display the users address details if they are logged in have tried a stored procedure:

ALTER PROCEDURE dbo.showAddress
    @userid nvarchar(256)
AS
    SELECT Fullname, Address, County, Postcode FROM Addresses WHERE @userid = UserId
RETURN

But i get an area about expecting the user id? I have tried inserting through code behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (User.Identity.IsAuthenticated)
    {
        SqlCommand oCMD = new SqlCommand();
        SqlConnection oCON = new SqlConnection();
        oCON.ConnectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        oCON.Open();
        oCMD.Connection = oCON;
        oCMD.CommandType = CommandType.StoredProcedure;
        oCMD.CommandText = "showAddress";

        SqlDataReader oRDR = oCMD.ExecuteReader();
        oRDR.Read();
        txtName.Text = oRDR.GetString(oRDR.GetOrdinal("Fullname"));
        oCON.Close();
    }
}

But this doesnt work either for me. Is there some code that could be entered as something like:

txtPostcode.Text = "sql connection; Addresses; Fullname";

Any help or ADVICE is appreciated....

Upvotes: 1

Views: 237

Answers (2)

AD.Net
AD.Net

Reputation: 13399

Seems to me you're not passing useid parameter to the stored procedure.

oCMD.Parameters.Add(new SqlParameter("@userId", User.UserId));//plz check the User object to find the id

Upvotes: 2

datadamnation
datadamnation

Reputation: 1892

In your C#, you need to add the @userid as a parameter to pass to the stored procedure.

oCMD.Parameters.Add(new SqlParameter("@userid", userid));

Obviously, you will need to get the userID from somewhere, such as the User.Identity object.

Upvotes: 2

Related Questions