I have created an aspx page on a sharepoint 2013 site. I have customized the pages css, buttons, and layout exactly as I need. Now my goal is to add a SQL connection to the database on our network to pull data based on currently logged in user. 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();
}
}
%>
Any ideas or thoughts would be appreciated.
You should not place your C# code into the page directly.
Instead you can make a user control following this guide: https://msdn.microsoft.com/en-us/library/ee231548.aspx
Then in your page you have to register a tag for the control
<%@ Register Tagprefix="MyControls"
Namespace="KM.MyControls.MyControl"
Assembly="KM.MyControls, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=<Your token>" %>
and then you can use your control into the page:
<MyControls:MyUserControl runat="server"/>
source: https://sharepoint.stackexchange.com/questions/46629/how-to-put-custom-user-control-on-page-layout
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.