Click here to Skip to main content
15,910,471 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
All,

I have been trying for a few days to Convert the code below to asp.net c#.

It's a pretty simple page.

- Get the current logged on user and store it in a variable called strFullUserName for example domain\johnsonM
- I then Query the user name against a stored procedure called usp_GetDisplayName example Mike Johnson.
- It will then store the users full name (Mike Johnson) in strFullUserName

I have never used C# before and I have the rest of the site already completed and now I need to display the Full Users name on the website without querying Active directory. I have done this many times in ASP using the code below without any issues just not sure how to do this is ASP.net and C#.

Thanks!

HTML
<<blockquote class="quote"><div class="op">Quote:</div>html>
<body>

<% 


Dim strFullUserName

strFullUserName = Request.ServerVariables("AUTH_USER")

Set rs = CreateObject("ADODB.Recordset")
rs.Open "EXECUTE usp_GetDisplayName '"  & Request.ServerVariables("AUTH_USER")  & "'", "Provider=SQLOLEDB.1;Password=PASSWORD!;Persist Security Info=True;User ID=USERNAME;Initial Catalog=UserCatalog;Data Source=SERVERNAME", 3, 3

If rs.bof and rs.eof Then
Else
strFullUserName = rs.Fields("displayName").Value
End If

Set rs = Nothing


Response.Write(strFullUserName)
%>

</body>
</html</blockquote>>


What I have tried:

- I have attempted to make a connection to the sql server using the code below

}


SqlConnection sqlConnection1 = new SqlConnection("Data Source = SERVERNAME; user id = USER NAME; password = PASSWORD!; Initial Catalog = USERCATALOG;");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
Posted
Updated 29-Aug-16 21:59pm
Comments
ChauhanAjay 29-Aug-16 21:00pm    
http://www.codeproject.com/Questions/224585/how-to-call-stored-procedure-in-asp-net-csharp-cod

hope this helps

1 solution

Change values as per your requirement

C#
using (SqlConnection conn = new SqlConnection())
            {
                conn.ConnectionString = "Data Source=<server>;Initial Catalog=TDE_Test;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;";
                conn.Open();
                SqlCommand cmd = new SqlCommand("StoreProcName", conn);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter parm = new SqlParameter("@return_value", SqlDbType.Int);
                parm.Direction = ParameterDirection.ReturnValue;
                cmd.Parameters.Add(parm);
                cmd.ExecuteNonQuery();
                Label2.Text = parm.Value.ToString();
            }</server>
 
Share this answer
 
Comments
droidkid 2-Sep-16 11:43am    
hmm doesn't seem to work I get the following error.

Additional information: Procedure or function 'usp_WOF_GetDisplayName' expects parameter '@UserInfo', which was not supplied.
charmyvora 2-Sep-16 11:51am    
That is not asp.net problem. You will have to add parameter and its value as required by stored procedure.
Ex:
command.Parameters.Add("@UserInfo", SqlDbType.int);


What are other errors??

I hope you changed values in <> whatever is inside <> is sample data. Remove <> and replace with your requirement.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900