Add SQL Query & Variable to SharePoint ASPX Page

I have created an aspx page on a sharepoint 2013 site. I have customized the pages css, buttons, and layout exactly as I need. My goal now is to add a sql connection to a db on our network to pull data based on the user logged in. I have written the sql query to basically be "Select "field" From DB Where SharePointUserloggedinname like 'db.table.field' . I have tested this query in sql management studio, and it works perfectly. My question, is how do I add this sql query portion into my aspx page, set the query result to a variable and then display the queries results(variable) to display via a text field/paragraph tag/or other.

I have added the namespaces, and c# portion provided below into my aspx page but I am unsure how & where to place the c# code to connect to the sql db, set the variable, and then call that variable to display in a field further down the page.

`<%@ Import Namespace="System;"%>
<%@ Import Namespace="System.Collections.Generic;"%>
<%@ Import Namespace="System.ComponentModel;"%>
<%@ Import Namespace="System.Data;"%>
<%@ Import Namespace="System.Drawing;"%>
<%@ Import Namespace="System.Linq;"%>
<%@ Import Namespace="System.Text;"%>
<%@ Import Namespace="System.Threading.Tasks;"%>
<%@ Import Namespace="System.Windows.Forms;"%>
<%@ Import Namespace="System.Data.SqlClient;"%>
<%@ Import Namespace="System.Web.UI.Page" %>


<%@ public partial class _Default : System.Web.UI.Page
{
private SqlDataReader reader = null;
public SqlDataReader Reader { get { return reader; } set { reader = value; } }
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

SqlCommand command = new SqlCommand("SELECT [totalHours] FROM [DB].[dbo].[TABLE] Where [DB].[dbo].[TABLE].[column] like 'persons name') = @variable1", connection);
command.Parameters.Add(new SqlParameter("variable1", "anonymous"));

Reader = command.ExecuteReader();
}
}
%>

Open in new window

Any ideas or thoughts would be appreciated.
cbarber22Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
You're missing the @ symbol:


command.Parameters.Add(new SqlParameter("@variable1", "anonymous"));
0
cbarber22Author Commented:
Thank you, where would i place that block of code provided above, as well as how would i call this variable further down in my aspx page.
0
Kyle AbrahamsSenior .Net DeveloperCommented:
That depends on what you're trying to do with the reader.  But you're missing the @ in front of the variable name which is why it was throwing an error.

`<%@ Import Namespace="System;"%>
<%@ Import Namespace="System.Collections.Generic;"%>
<%@ Import Namespace="System.ComponentModel;"%>
<%@ Import Namespace="System.Data;"%>
<%@ Import Namespace="System.Drawing;"%>
<%@ Import Namespace="System.Linq;"%>
<%@ Import Namespace="System.Text;"%>
<%@ Import Namespace="System.Threading.Tasks;"%>
<%@ Import Namespace="System.Windows.Forms;"%>
<%@ Import Namespace="System.Data.SqlClient;"%>
<%@ Import Namespace="System.Web.UI.Page" %>


<%@ public partial class _Default : System.Web.UI.Page
{
private SqlDataReader reader = null;
public SqlDataReader Reader { get { return reader; } set { reader = value; } }
protected void Page_Load(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
SqlConnection connection = new SqlConnection(connectionString);
connection.Open();

SqlCommand command = new SqlCommand("SELECT [totalHours] FROM [DB].[dbo].[TABLE] Where [DB].[dbo].[TABLE].[column] like 'persons name') = @variable1", connection);

/***************************  CHANGED LINE BELOW ***********************/
command.Parameters.Add(new SqlParameter("@variable1", "anonymous"));

Reader = command.ExecuteReader();
}
}
%>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cbarber22Author Commented:
thank you sorry for the delay
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.