How to display a search result in a treeview - c#

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.

Related

sort data displayed in table with c# and mongodb

I have to implement a function to sort the data displayed in frontend table. There the user can click the up and down arrow in the relevant column he wants to sort. then all the data in all the columns should be sorted according to that relevant column user clicked. How can i do this in c# with mongodb.
I have wrote this to sort according to the date (one column in the table. There are so many others like name, id etc.)
result = await _mongoDb.GetCollection<Entity.Mongo.MyModelClassName>(MyCollectionName)
.Sort(Builder<Entity.Mongo.MyModelClassName>.Sort.Ascending(x => x.Date))
this is working fine.
But what i need now is to remove this hard coded one and sort according to the user preference.
Can someone help me with this?
You should do a simple query and according to the sortOrder selected by the user, server-side you execute the sorting.
This should help you understand this concept : https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/getting-started-with-ef-using-mvc/sorting-filtering-and-paging-with-the-entity-framework-in-an-asp-net-mvc-application
So,
In the URL, you should have a parameter for the sortOrder.
In the OnGet() execute your query and then the sorting on this collection based on this parameter.
Then you should have a sorted collection and be able to do what you want to do.

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.

Sitecore WFFM check duplicate email

I am developing a form validation action for WFFM, which will not allow people to use same email for submitting multiple entries. So far, the only document I've got is the WFFM v2.3 Ref from Sitecore SDN, which only have few example of how to access submitted data form by form.
I don't know how to select data by using field value. So, my current solution is to retrieve all data from database and check all email fields; which doesn't seem right when putting in scale.
Do you have any code snippet that can help me add GridFilter like email="abc#def.com", if count > 0 definitely the email is duplicated?
Thank you.
Instead of finding the API supporting this very own demand, I found it easier to make a direct connection to the WFFM database and look up for what I want. Thank you for reading this.
The webforms database has 3 tables only. If for example, you want the list of email ids available in the 'Support Form' below:
Here's the query:
SELECT DISTINCT Value FROM [dbo].[Field]
WHERE FieldId = '5F5643B6-0535-49D8-B3C9-CF8E65A415C0'
Field Id corresponds to the field GUID of the form:
Ps. WebForms connections string should be available in App_Config\Include\forms.config.

Knockout-style dynamic page with Razor viewmodels and #functions?

I'm building a page in which the user can query a set of data by building up a set of search criteria, something like the way the Visual Studio TFS plugin lets you search work items: a table of conditions, where you can keep adding rows. You select "and" or "or" for the join condition, then select a field, enter a value, and select whether you want things that do or do not match it:
1. show items where [Field] [is|is not] [value]
2. [and|or] [Field] [is|is not] [value]
3. [and|or] [Field] [is|is not] [value]
etc...
Now, I'm looking at ways to build this, and I had a thought. In the past, I've used Knockout, but this requires me to have models in Javascript to map the data to, which seems redundant when I already have those models in C# in the server-side code. Of course, I can use Razor code to foreach through a list of criteria that's part of the model in a strongly-typed view, but I can't find a tidy way to add to this list.
The model structure in C# is (roughly) like this:
Field:
field name
list of options for the value
boolean value for the is/is not option.
Criterion:
Field
selected value
combination type (and/or)
Query:
list of Criterions (that looks weird not saying Criteria)
start and end date
the user's access level
view field and sorting options
QueryViewModel:
Query
assorted lists to populate the view options selection area
a little metadata for other (unrelated) displays on the page
In Knockout, I'd add an on-click method to the "add search criteria" button to add new entries to the list of criteria. Can I use a Razor functions block (#functions { ... }) to achieve a similar result? I've tried a few things, but I either find that the viewmodel doesn't seem to be in scope, or that there's no way to update the page to show the new contents of the viewmodel (although I'm experimenting with something that involves passing the newly-updated viewmodel to a partial view, which might work somehow). Can this be done, or do I need to take a deep breath and go back to the Javascript?
You can serialize/deserialize your C# objects as JSON, either with standard MVC controllers and JSONResult or with Web API in MVC 4. This means you dont have to explicitly redefine your c# objects in javascript.
In the browser you can use the knockout mapping plugin to make your json properties into knockout observables if required.
This is generally a cleaner and more robust approach than dynamically loading and rendering html from the server (if thats what you were suggestiong - wasnt 100% clear from your post).

Getting more than one item from a database in ASP .NET MVC 3

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.

Categories