cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
WardD
Engaged Sweeper
Our Technicians use a call ticket application, written in-house, that writes to a SQL Server database apart from the Lansweeper database. When in the Lansweeper UI, I'd like to provide a custom widget that will query the call ticket database for a given asset, and return closed call ticket information. I have added the connection string information to the web.config file, but I'm unfamiliar with how to query within the custom widget .aspx file. Any suggestions would be greatly appreciated

1 ACCEPTED SOLUTION
Daniel_B
Lansweeper Alumni
You could import System.Data.SqlClient in your aspx file and start a new connection to the external database. This could either happen in the aspx directly or in the code file in the background. The code below is just a rough example.

...
<%@ Import Namespace="System.Data.SqlClient" %>
...


string connectionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
string result = null;

connectionString = @"Data Source=HOSTNAME\INSTANCENAME;" +
@"Initial Catalog=DATABASENAME;User ID=USERNAME;Password=PASSWORD";
sql = @"Your SQL query";

connection = new SqlConnection(connectionString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
result = command.ExecuteScalar().ToString();
command.Dispose();
connection.Close();
}
catch (Exception ex)
{
result = "No connection to Database";
}

%>

<p>The result is: <%:result%></p>
...

View solution in original post

2 REPLIES 2
Daniel_B
Lansweeper Alumni
You could import System.Data.SqlClient in your aspx file and start a new connection to the external database. This could either happen in the aspx directly or in the code file in the background. The code below is just a rough example.

...
<%@ Import Namespace="System.Data.SqlClient" %>
...


string connectionString = null;
SqlConnection connection;
SqlCommand command;
string sql = null;
string result = null;

connectionString = @"Data Source=HOSTNAME\INSTANCENAME;" +
@"Initial Catalog=DATABASENAME;User ID=USERNAME;Password=PASSWORD";
sql = @"Your SQL query";

connection = new SqlConnection(connectionString);
try
{
connection.Open();
command = new SqlCommand(sql, connection);
result = command.ExecuteScalar().ToString();
command.Dispose();
connection.Close();
}
catch (Exception ex)
{
result = "No connection to Database";
}

%>

<p>The result is: <%:result%></p>
...
WardD
Engaged Sweeper
Would there happen to be a function in the Lansweeper DB class similar to ExecuteDataset that would allow me to specify a connection string?

New to Lansweeper?

Try Lansweeper For Free

Experience Lansweeper with your own data.
Sign up now for a 14-day free trial.

Try Now