Add Geography.Lat/Long to View with the designer - c#

I am currently working with some GIS data and using the Geography data type in MS SQL Server 2012. I am having some difficulty when trying to display the Latitude and Longitude of the Geography type using the View designer, specifically if I have a join with another table.
For example the below works fine in the view designer:
SELECT dbo.table1.ID, dbo.table1.LocationTypeID, dbo.table1.Location, dbo.table1.Location.Lat as LocLatitude, dbo.table1.Location.Long as LocLongitude
FROM dbo.table1
However, this does not:
SELECT dbo.table1.ID, dbo.table1.LocationTypeID, dbo.table2.LocationTypeName, dbo.table1.Location, dbo.table1.Location.Lat as LocLatitude, dbo.table1.Location.Long as LocLongitude
FROM dbo.table1 INNER JOIN
dbo.table2 ON dbo.table1.LocationTypeID = dbo.table2.ID
I found that if I remove the schema (dbo) and manually create the view via an SQL command it will create the View and run correctly, however, attempting to edit it later via the designer will display errors.
For reference the error the designer throws out is the "multi-part identifier could not be bound".
Whilst the manual creation of View can resolve the issue I would really like to find a way to solve this as it is bugging me but also causes issues for other people working on the project not knowing that now the only way to edit the View in future is to create an "Alter View" SQL command.
I have tried searching for an answer to this but have yet to find anything, normally this would not be an issue as I would just grab the Geography type object from the code and get the Lat/Lon there to be displayed but I need to create an export/View for a 3rd party.

Need to reference tables by aliases instead of directly. Answer provided by #MartinSmith

Related

Missing relations in Designer of Reporting Tool List & Label using SQL Server database

I'm currently testing various reporting tools and came across to List & Label from Combit, which makes a very solid impression. As part of a PoC, I first integrated it into a simple C# Winforms application and attached a SQL Server database.
...
using(ListLabel myLL = new ListLabel())
{
SqlConnection connection = new SqlConnection(Properties.Settings.Default.ConnectionString);
myLL.DataSource = new SqlConnectionDataProvider(connection);
myLL.Design();
}
...
However, I don't understand why with the data structure in the designer I can't then also see the relational structure in/from the database.... all relations seem to be just ignored. I can only see all tables at the root level - even though they are a relation in the SQL Server database.
Couldn't find any information for this so far unfortunately - ideas?
Actually, it's all there usually. You should be able to see the full structure, as soon as you add a report container:
This way, you can add e.g. "Order Details" either as a sub element of the customers (like shown in the screenshot) or as a top level element (if I had selected it from the root of the list).
The field tree on the other hand just shows the available tables with their contents ("fields"). As each table may appear in different levels of the hierarchy, it's only added once to the tree, while the hierarchy is defined when adding a new table.
One exception from this rule is the 1:1 related identifiers of parent tables. As it might well be required to print e.g. Customer data related to an order line in a "Orders" table, you cann access them directly from the field tree:
Thus, you actually do see the relations there, albeit in reverse order. While this might seem confusing at first it really makes sense once you get your head around the concept.

How to display a search result in a treeview

Currently, I'm working in a MVC project (this is my first project). I'm doing fine but I'm stuck somewhere. I hope someone will help me out with this.
In this project I have to search for a record (with id) in SQL database from visual studio, where I should get result in a treeview... like under ID we may have a lot of sub ids or may not, if we have one sub id, it should display one if we have multiple sub ids multi-level treeview should display.
Note: This result I should get from the database when the user searched for particular id only, DATA from the database should not be loaded with the page.
Create a tree view using css and html. Populate the value using a rest controller or controller.
create an arraylist which returns your search result.
return the value from the list to a model or a url using a function.
As you can see there are many ways you can do this.
you can check this out as well: https://www.c-sharpcorner.com/UploadFile/85ed7a/searching-records-from-database-and-display-in-gridview-and/
and for only loading id from the database you can run
Select id From tablename
and if you want the details only after user clicks the id
Select * From tablename Where id = clickedvalue
you could also use where statement in sql to have more specific values
you'll need to change few codes as this tutorial uses grid view.
i hope this helps. But please provide some code snippets of what you've done or where you're stuck to get a precise answer.

Lightswitch query/subquery/drop down choice list

I'm working on a LightSwitch HTML application in VS2013 using C#. I have a main screen that allows a user to pick a site that they are authorized to use. It passes this site id to the search screen, and I want to fill the search screen with a bunch of search options to search against assets.
In the database, I have 3 tables using SQL Server:
Site (Id, ShortName, LongName, Description)
Unit (Id, Code, Description)
SiteUnit (Id, SiteId, UnitId)
for valid combinations.
Since I am passing in SiteId I want to present the user (among other things) a drop down of valid units they can search from (Code field from unit)
I have attempted to accomplish this in many ways, but so far its been unfruitful.
I have added dataitem (query) to the page on the SiteUnit table with a parameter of SiteId, and this returns the appropriate records.
Now I want to use this to filter against the Unit table to show the appropriate choices.
All of the appropriate foreign keys are added, but I just can't figure this out.
I only have a basic understanding of LightSwitch. You cannot directly use SQL code or do multiple table joins as you would in SQL. However, if you have access to the database you can create a view and then query that view as if it were a table. The following will create a view that will list every unit for every site. Calling the view with a filter on SiteID will provide the units just for that site.
Create View v_UnitsBySiteID as
SELECT Id, Code, Description
FROM Unit u
INNER JOIN SiteUnit su
ON u.ID = su.UnitID
Change what is returned as necessary. Does this help?
You need to add another filter query against your Unit table using a parameter just like you did for your SiteUnit table. You then need to set your new parameter to the proper UnitId. This can be accomplished in a variety of ways.
The easiest way would be to set your Parameter Binding directly to SiteUnitQuery.SelectedItem.UnitId. But if you need to do other processing when the UnitQuery executes, you can manually set the new parameter in the SiteUnitQuery_SelectionChanged() method or via a button of some sort.

Creating a database view in code

So I've been tasked with creating a tool to allow ours users to create their own 'worklists' which they use to work through their data. In our app these worklists are driven by SQL views, so for now my program is having to dynamically create views in our database based on the users input. I don't like this, but for now I have to make the best of it an am brainstorming the best ways to go about this.
Basically every view I create has a similar skeleton, it has several columns that are always pulled and several joins that always happens. Based on the users input I may add additional SELECT columns, as well as additional joins if they are necessary to access the added display columns.
So basically right now my code looks like this...
string SQL = string.Format(#"CREATE VIEW {0}
AS
SELECT
Foo.A,
Bar.B,
{1}
FROM
Table
INNER JOIN Foo on Foo.ID = Table.FooID
INNER JOIN Bar on Bar.ID = Table.BarID
{2}", viewName, displayNames, extraJoins);
Database.ExecuteNonQuery(SQL);
I really don't like this for obvious reasons. However, I cannot seem to find the equivalent of a parametrized query for view creation with ADO. I could perhaps create a stored procedure to do this, but even that seems sloppy. Is there any reasonable way to do something like this that doesn't make me sick to my stomach? Also we are using MS SQL, and have to support as far back as 2005.
In contrast to DML (like SELECT / UPDATE / INSERT / DELETE) there is no support for parameters in DDL(see here too). So basically you either hide that inside a Stored Procedure with dynamic SQL or do it the way you describe...

Entity Framework and MySql Stored Procedures

I am working with Entity Framework 4.0, VS 2010, MySql server database, and mysql-connector-net-6.4.4 for connection purpose. It works fine, that said, it can generate Model classes, csdl, ssdl etc files well. But, for stored procedures it doesn't work.
Here is what happens..
Right clicked on an SP from Model Browser, select [Add Function Import]. This opened a dialog box
Filled appropriate values like, Function Import Name, Stored Procedure Name
Click on [Get Column Information]. This results into some Grid filled up at the bottom of this button. In the grid, there is a column named [EDM Type]. This column shows [Not supported] due to some unknown reason :(
Now, clicked on [Create New Complex Type]. This goes OK, without Error
Now, clicked on OK button
After doing all above steps, there is no Complex Type created in the code however, which is the problem.
Can anyone please help?
The generated Complex Type is in the Complex Types folder in the Model part of the Model Browser.
To access ti in code, use it as any other Entity
MyComplexType ct = new MyComplexType();
And you can use it as a result type for your stored procedure:
List<MyComplexType> info = ctx.GetAllEmployees().ToList<MyComplexType>();
You must ensure that the GridView's columns mappings match the Complex Type's properties, or that the GridView's property AutoGenerateColumns is set to true.

Categories