I'm working on a project where I have to add the functionality of searching for phone numbers using dot net core. on SignUp, we are storing phone numbers in SQL Server using the country code eg: "+923007418819".
So if the user searches the phone number with 03007418819 it's not matching the data stored in the database and returns null.
The main thing I want is that if the user enters the phone number 03007418819 like this, it searches with the last 10 digits.
How can it be done?
First you have to create a computed persistant column with the reversed phone number like this :
ALTER TABLE T_PHONE ADD _PHONE_NUMBER_REVERSE AS REVERSE(PHONE_NUMBER) PERSISTED;
Second you have to create an index for performances searches :
CREATE INDEX X_001 ON T_PHONE (_PHONE_NUMBER_REVERSE);
Last, you have to use a WHERE clause like this one :
WHERE _PHONE_NUMBER_REVERSE LIKE REVERSE('03007418819') + '%'
This is the most efficient way to do that !
You can use SUBSTRING :
SELECT SUBSTRING([your column name], 3, 10) AS phone,
FROM [your table name]
WHERE [your column name] = [search term];
Different approach:
the best way to solve this issue from its roots is to save all users' phone numbers in the database in a specific format e.g. 00[countrycode][rest]
Create a helper that will format any valid phone number the user enters to the format you want when signing up. All phone numbers will be then formatted before being saved in the database. (The database will be then formatted and clean)
When the user tries to sign in, the same helper will first be called and format the number the user entered, and then you can easily search in the database with no magic. With this tiny modification, you can win the database index as well.
Related
I'm trying to create an adf query panel with table but I need to limit the search values as also the result values in the table.
For example:
- I have a table with FName, LName, Dateofbirth, address, phone
- I want that the values presented in the search form are FName, Lname
- the values in the result table Fname, Lname, address, phone
I've checked several links including this one enter link description here
But I can't understand how to limit the values.
There are lot of resources available on the internet on how to implement search functionality in ADF. here are few for your reference:
http://www.baigzeeshan.com/2010/04/creating-simple-search-form-in-oracle.html
https://www.youtube.com/watch?v=LAWSfO-7xR8
http://adfnote.blogspot.com/2013/02/create-simple-search-form-in-oracle-adf.html
Normally a search functionality is implemented using a View Critreia, which is created on a View Object. It shows up in Data Control as a Named Criteria under your View Object instance.
While creating the view criteria you can specify the items that you would want to see in the panel as search fields.
When you use (drag and drop) your view criteria as an 'ADF Query Panel with Table' on a page, you get the option to specify the columns you want to see in the results table. You can restrict the number of columns there, or even afterwards by just removing the unwanted columns from your page.
I was able to make it work, by not checking the attribute option 'Queryable'.
This way the it does not appear in the query form,only in the results table.
Thank you all for your help
I am using Microsoft ASP.NET web matrix
I have data table called result in MS Access. The name of access file is "roll number".
The table consists of following columns:
Roll number, name,
subject 1, subject 2, subject 3, subject 4, subject 5,
total marks, result
I want that when a user will enter a "roll number" in some textbox and clicks a "submit" button, he is redirected to page showing his results: roll number, name, subject 1..5, total marks and result.
I want it to be two pages like this:
result.aspx -- where user enters his roll number and submits it
displayresults.aspx -- where a result is displayed like this:
ROLL NUMBER : (what user entered, looked up in MS Access)
NAME : ("name" read from record from MSAccess file)
SUBJECT
1 : (mark from "Subject1")
2 : (mark from "Subject2")
3 : (mark from "Subject3")
4 : (mark from "Subject4")
5 : (mark from "Subject5")
TOTAL MARKS (likewise)
RESULT (i.e. PASS/FAIL, as usual, from MSAccess)
How to connect such search engine with data kept in a table from an MS Access database?
Please give complete code to generate the web files.
Sorry, but I will not repeat what tutorials say. I will not provide you "the code", as it is mostly trivial, and about any database-querying ASP.NET tutorial will show you how to have a 'textbox that searches for something in a database".
I will only tell you the nontrivial bits:
firstly, the MS Access file is not just a file, it can be seen as a database. With some typical tools provided by the .Net or ASP frameworks you will be able to send simple SELECT SQL queries to that database and it will respond with results. How? -> tutorial.
most of the tutorials will teach you how to use databases in a "general way"; Tutorials usually does not really care about what database you would like to use. Most of them will will use connectionstrings pointing to a (localdb\v11.0)(or similar), .\sqlexpress, sqlserver, 'localhost', '.' or similar things. This is NOT your case. For working with MS Access files you have to provide a proper OLEDB connection string that will point exactly to that file. Please see this MSDN article and look into "OleDB Connection Strings" section. In very short, the connstring should look like:
Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\MyPathToMyDatabase\TheFILE.mdb
after you set up the connection string, you may query the database with any tool you like, be it raw SqlClient, SqlDataSource, LINQ or etc.
actually, when speakin about SqlDataSource, ASP.Net even provides you an AccessDataSource. go figure..
How to search every word separated by comma in textbox
Please refer above post
Its Working perfectly...But i have small issues.. when i enter in text box like c,c++,4-5 yrs it have to check in database like either c,c++ skills and 4-5 yrs experiecne and then the reult has to be shown... Burt as per ur query it just show results whether any one of keyword satisfy database ...I want to compare year also how? –
If you want that behavior, you have to program that behavior. One design is to have multiple input boxes: one where you check if any of the words exist, another where you check that all of the words exist. (Perhaps even another for an exact phrase match.) Another design possibility would be for you to develop a syntax to indicate optional and required words all within a single input box. The point is it is up to you.
After you've decided on a design, then you could write code that builds your query based on or matches on the optional words and and matches on the required. Something like this pseudocode
Select * From Table Where
(Field Like OptionalWord1 Or Field Like OptionalWord2 Or Field Like OptionalWord3)
And Field Like RequiredWord1
And Field Like RequiredWord2
(etc.)
I have a table that contains three columns.
"UserId" type-nvarchar
"PostAuthorId" type-nvarchar
"Post" type-text
This table will contain "wall" posts like in facebook for each user's page. I am going to use a gridview on each user's page to display the posts. The issue is I want to display them with the latest(most current) post being first and the earliest post being last.
I have never used autoincrement before and I am not sure if that is the answer. If it is, I do not know how to use it. I thought about adding a date posted column and then ordering by date.
If I end up using the date column, I could also display the date on the post. Is there a way to convert the date to a readable format?
What is the best way of implementing this type of ordering?
If you use AutoIcrement the first record will start with 1 and each record will increment from there. (default setting)
If you want to sort them by newest first do an ORDER BY ID DESC
I would suggest making a column called wallPostID then setting that to AutoIncrement and also your Primary Key
Date Formating:
If you are displaying this data in a gridView
Go to Edit Columns on your grid view
CLick on the Date field under "Selected Fields" on the bottom left
Under "BoundField properties" on the right Go to Data -> DataFormatString
{0:d} will display as 1/1/2010
This site has more info in string formatting
http://msdn.microsoft.com/en-us/library/fht0f5be.aspx
A datetime column would definitely work for something like this. Assuming you are using MS-SQL, you can also attach a default value to the column using a built-in function like GETDATE(). That way, you only have to input the data that matters and the database will take care of adding the datetime column.
For converting a datetime to a readable format try:
DateTime postDate;
string value = postDate.ToShortDateString();
You should always use an ID field that auto increments. Can also be used as your PK
I would suggest the DateTime field rather than the autoincrement simply because it will not only serve as an effective Sort field, it also preserves information that you may well want to display. If you want the most recent first you'll sort using the Date and a "DESC" modifier:
Select ... Order By [Date] DESC;
When you retrieve the data, you can retrieve it as a DateTime and modify it using C#. You can use "ToShortDateString()" as suggested by mdresser if you just wish to show the date or ToString("...") if you wish to show the time as well. You can also use SQL to convert it into a string before retrieving it:
convert(Varchar(10), #mydatetime, 101)
If you look in MSDN you'll see the various conversion codes (101 is the code used above) that can be used to translate the date in various ways.
UPDATE: You may want to use an autoincrementing field for your application for reasons other than your expressed need to sort wall entries. They are easy to use - just mark the field as an Identity if using SQL Server (other DBs are similar). As far as using them in your program, just think of the field as an Int field that you never have to set.
Now, why would you use a auto-incrementing field? Perhaps the most straightforward reason is so that they give you have an easy way to identify each record. For example, if you permit people to alter or delete their wall entries, the auto-incrementing field is ideal as it gives you a way to easily look up each record (each record will be assigned its own, unique value). You might put an "x" next to the record like StackOverflow does and make it a call back with the UID (auto-increment) value. Note that you should set up your primary key on the UID field if you'll be doing this.
Now, if you find them useful for this reason then you could also sort by the UID. I would still store the date so that you can provide Date and Time feedback as to when an entry was made on the wall but this would no longer be your indexed or sorted field.
I'm looking to create a table for user preferences and can't figure the best way to do it. The way that the ASP.NET does it by default seems extremely awkward, and would like to avoid that. Currently, I'm using one row per user, where I have a different column for each user preference (not normalized, I know).
So, the other idea that I had come up with was to split the Preferences themselves up into their own table, and then have a row PER preference PER user in a user preferences table; however, this would mean each preference would need to be the exact same datatype, which also doesn't sound too appealing to me.
So, my question is: What is the best/most logical way to design a database to hold user preference values?
Some of the ideas that I try to avoid in database work, is data duplication and unnecessary complication. You also want to avoid "insert, update, and deletion anomalies". Having said that, storing user preferences in one table with each row = one user and the columns, the different preferences that are available, makes sense.
Now if you can see these preferences being used in any other form or fashion in your database, like multiple objects (not just users) using the same preferences, then you'll want to go down your second route and reference the preferences with FK/PK pairs.
As for what you've described I see no reason why the first route won't work.
I usually do this:
Users table (user_id, .... etc.)
.
Options table (option_id, data_type, ... etc.)
(list of things that can be set by user)
.
Preferences table (user_id, option_id, setting)
I use the new SQLVARIANT data type for the setting field so it can be different data types and record the data type of the option as part of the option definition in the Options table for casting it back to the right type when queried.
If you store all your user preferences in a single row of a User table you will have a maintenance nightmare!
Use one row per preference, per user and store the preference value as a varchar (length 255 say, or some value large enough to meet your requirements). You will have to convert values in/out of this column obviously.
The only situation where this won't work easily is if you want to store some large binary data as a User preference, but I have not found that to be a common requirement.
Real quick, one method:
User(UserID, UserName, ...)
PreferenceDataType(PreferenceDataTypeID, PreferenceDataTypeName)
PreferenceDataValue(PreferenceDataValueID, PreferenceDataTypeID, IntValue, VarcharValue, BitValue, ...)
Preference(PreferenceID, PreferenceDataTypeID, PreferenceName, ...)
UserHasPreference(UserID, PreferenceID, PreferenceDataValueID)