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.
Related
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.
I am converting a VB6 app to C# with an SQL Server back end. The app includes a very general query editor that allows the user to write any select query and return the results visually in a grid control. Some of the tables have several hundred columns (poor design, I know but I have no control over this). A typical use case for an admin user would be to
select * from A_Table_With_Many_Columns
However, while they want to be able to view all the data, they are particularly interested in 2 columns and they want these to be displayed as the first 2 columns in the grid (instead of 67th and 99th for example) so instead they execute the following statement:
select First_Interesting_Field, Second_Interesting_Field, *
from A_Table_With_Many_Columns
Then they will go and modify the data in the grid. However, when saving this data, it results in a concurrency violation (DBConcurrencyException). This worked fine with the connected RecordSets of VB6 but not so well in C#. I have tried a myriad of solutions to no avail.
Does anyone know how to handle this exception in a generic way? (Remember, the user can type ANY select statement or join etc. into the query editor)
Does anyone know how I might manipulate the columns returned such that I delete the 2 columns that appear further on in the list? (My difficulty here is that if the column name in the database is EMail so I do select Email, * from Blah the 2 pertinent columns returned are EMail and ADO.NET or C# aliases the second EMail column from the * portion of the query as EMail1 so I am not able to detect the second column as a duplicate and remove it)
Does anyone have an alternate solution I have not thought of?
Thank you very much
Actually, you could rename all variables to something like email_userdefined by doing something like this:
SELECT First_Interesting_Field as First_Interesting_Field_userdefined, Second_Interesting_Field as Second_Interesting_Field_userdefined, *
from A_Table_With_Many_Columns
Replace user_defined with whatever you want, like order number or anything else user acceptable
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...
I'm creating a database where users can enter some Error Reports and we can view them. I'm making these database with C# in the ASP MVC 3 .NET framework (as the tags imply). Each Error Report has a unique ID, dubbed ReportId, thus none of them are stored under the same Id. However, whenever a User creates a new Error, I pass their User Name and store it in with the rest of the report (I use User.Identity.Name.ToString() to get their name and store it as a string). I know how to get a single item from the data using a lambda expression, like so:
db.DBSetName.Single(g => g.Name == genre)
The above code is based on an MVC 3 tutorial (The Movie Store one) provided by ASP. This was how they taught me how to do it.
My major question is: is there a member function like the .Single one that will parse through the whole database and only output database entries whose stored User Name matches that of the currently logged in user's? Then, I can use this to restrict User's to being only able to edit their own entries, since only their entries would be passed to the User's View.
What would be the best way to implement this? Since the ReportId will not be changed, a new data structure can be created to store the user's Errors and passed through to the Index (or Home) View of that particular controller. From there they should be able to click any edit link, which will pass the stored ReportId back to the Edit Action of this particular controller, which can then search the entire database for it. Am I right in assuming this would work? And would this be ideal, given that the other items in the database are NOT passed through to the Index in this method, meaning the User does not have access to the other items' ReportId's, which the user needs to pass into the Edit Action for it to work? If this is ideal, this is the method that requires me to know how to parse through a database and grab every element that fits a particular description (stored User Name matches User's current User Name).
Or would a better approach be to pass the whole database to the Index View and only output the database entries that have User Name values that match the current logged in user's? I guess this could be done in a foreach loop with a nested if loop, like so:
#foreach(var item in db.Reports)
{
if(item.UserName == User.Identity.Name.ToString())
{
...code to output table...
}
}
But this passes the whole database which gives the user a lot more info than they need. It also gives them potential access to info I don't want them to have. However, I don't have to make a new data structure or database, which should lower server memory usage and fetch time, right? Or are databases passed by copy? If so, this method seems kinda dumb. However, I don't know if the first method would fracture the database potentially, this one certainly would not. Also don't remember if I NEED an else statement in C#, I'm more familiar with C++, where you don't need one and you also don't need {}'s for single line if's, if I need one: please don't judge me too harshly on it!
Small note: I am using CRUD Controllers made with the Entity First Framework in order to edit my database. As such, all creation, reading, updating, and deletion code has been provided for me. I have chosen not to add such basic, common code. If it is needed, I can add it. I will add what the Edit Action looks like:
public ActionResult Edit(string id)
{
Report report = db.Reports.Find(id);
return View(report);
}
It accepts a string as an id, ReportId is the id used and it IS a string. It is a randomly generated GUID string made with the GUID.NewGuid().ToString() function. I will also be doing the comparison of names with:
Model.UserName == User.Identity.Name.ToString()
Which was shown earlier. Sorry if this is too much text, I wanted to provide as much info as possible and not make anyone mad. If more info is needed, it can certainly be provided. So at the end of the post, the major question actually comes down to: which of the above two methods is best? And, if it's the first one, how do I implement something like that?
Thanks for your help!
Unless I'm completely misunderstanding you, you just want .Where()
Like this:
var reports = db.Reports.Where(r => r.genre == inputGenre);
This would get you an IEnumerable of Report, which you could then use however you wish.
I am working on a site in which as user logs in (first database request) the stored procedure varify password and user id and then returns user record that I put in session to use next.
After this I do a second db request. it returns addresses of user which I put in cache.
Can you pleas guide me is there some way that I can get both sets of data (user record and his address from 2nd table) in one database requests.
plz guide me on this, I am using DAAB (enter prise library) for data access.
Thanks
Modify your SP which has multiple select statements, as in you case is 2. Two select statements in one SP will return two record sets. Verify in SQL Management Studio, when you run your SP, it should show you multiple Grid in bottom panel.
Once your SP is done, call SP from C# code and load result in DataSet. Dataset will have two table, and you can get the data from different table
You can write two select queries in a stored procedure or
Execute two queries one after another . In single query you can execute and receive the data in DataSet .
ExecuteDataset()
So two tables will be returned inside the dataset . You can get the values like
dataset.tables(0)
dataset.tables(1)
Thanks
You would gain nothing from retrieving two results sets in one go, But the code will become more incoherent. Why do you thing you need to merge two logically separate operations into one? Instead of using such questionable methods you can use join to get one result set that contains all the data in one go, but still that seems wrong. I can not see a clean way of doing what you are asking for and any benefits that might be gained.