|
Hi..
I m working on an asp.net website. storing users in sql server db.
what could be the best way to know whether user logged in or not ? and restrict them to login again in another browser window or another machine.
Make sure I am not using authentication. I use to store logged in user to session.
Is it possible through application state.
thanks,
By:
Hemant Thaker
|
|
|
|
|
Application state would not be the best place for that, as the users session would not timeout until the application state timedout, the app pool was recycled or the app is recompiled (a change to the web.config would do this).
I would / have used Session State for this in the past, just set session state variable to true when the user is logged in and just check that when the page is loaded. something along the lines of;
Login Page;
<code>
protected void login_click(object sender, EventArgs e){
.... code to validate credentials ......
Session["LoggedIn"] = true;
Response.Redirect("defaultpage.aspx");
}
</code>
All Pages;
<code>
protected void Page_Init(object sender, EventArgs e){
if (!(bool)Session["LoggedIn"])
Response.Redirect("login.aspx");
}
</code>
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
thanks,
I think this is all about authentication. you are redirecting the user to login.aspx if he is not logged in. that's fine.
I want to restrict one user to login in the second machine, if he is logged in , in one machine.
(simply dont want him to use application in two different browser window).
thanx
By:
Hemant Thaker
|
|
|
|
|
Oh, well in that case just store an ArrayList in Application State with all the logged in users in there (or a unique identifier for each user) then when a user tries to log in check if they are already in the ArrayList and show an error if they are.
protected void login_clicked(object sender, EventArgs e){
if (((ArrayList)Application["LoggedInUsers"]).Contains(txtUserName.text))
else{
ArrayList temp = Application["LoggedInUsers"];
temp.Add(txtUserName.Text);
Application["LoggedInUsers"] = temp;
}
}
Just dont forget to remove them when they logout or their session expires.
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
You said in last
"Just dont forget to remove them when they logout or their session expires."
will it work to remove entry in Session_End event in global.asx ?
thanks,
By:
Hemant Thaker
|
|
|
|
|
Yup, and if you have a logout page you would want to put it in there to.
But keep in mind that the session will stay alive for a bit after the user closes the browser, that period is configurable i just remember how.
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
Hi,
I've come across some articles that may have the solution to my problem, but I'm having a hard time implemeting the solutions, so I hope that anyone can help me out here...
The problem is, I have a page with a TabContainer (AjaxControlToolkit), I add each tab Dinamically on Page_Init (without checking either for PostBack or !PostBack), in each tab I also add a GridView.
So far so good, the GridView show the correct data, the problem is on SelectedIndexChanged, that never fires, I also have a RowDataBound event that fires on the creation of the gridView, but the SelectedIndexChanged doesn't, when the user changes the selection it goes straight to Page_Init.
I have the following code:
void Page_Init()
{
(...)
GridView gView = createGridView("gView_" + year.ToString());
tab.Controls.Add(gView);
(...)
}
private GridView createGridView(string ID)
{
GridView gv = new GridView();
gv.ID = ID;
CommandField cField = new CommandField();
cField.ButtonType = ButtonType.Link;
cField.ShowSelectButton = true;
cField.Visible = false;
gv.Columns.Add(cField);
gv.RowDataBound += new GridViewRowEventHandler(gv_RowDataBound);
gv.SelectedIndexChanged += new EventHandler(gv_SelectedIndexChanged);
return gv;
}
void gv_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
ViewState["index"] = e.NewSelectedIndex;
}
I was hoping to save the selectedIndex on ViewState, so I could 'catch' it after the Page PostBack in order to select the correct row (because the selected index is lost after the postback).
Hope I'm not asking a dumb question
Thanks in advance
|
|
|
|
|
I'm not 100% positive, but i think it might have something to do with you generating your controls on page_init.
I'm pretty sure that page_init is called before your event handler so the controls are destroyed then recreated so the original control that actually caused the postback doesnt exist and therefor there is nothing to call your event handler.
Try adding your controls on PreRender.
Once again i'm not 100% sure, it's friday and i'm sick, but it's worth a shot.
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
Moved it to the Page_PreRender(), same issue
The controls are created on each postback, but I neve trigger the selectedIndexChanged Event on the GridView, thus not being able to collected the selectedIndex.
|
|
|
|
|
Once again this is pure suggestion, i havent had time to build a test but i dont *think* you want to recreate the controls on each postback. try, and just try, throwing a 'if (!PostBack)' around your code and see what happens.
I'm not exactly sure what the problem is, but i'm positive it has something to do with destroying and recreating the controls.
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
Checking for PagePostPack, on building the page for the first time, the controls are created.
But when I change the selected index of a GridView the page is posted back and the controls are no longer 're-created'.
I am also on the same tought pattern :P Something to do with maintaining the state of the dinamically created controls.
I am messing around with the ViewState, but with no results so far.
|
|
|
|
|
|
Hi,
I have a .NET 2.0 web service written in C#. Within the C# code, I want to load a resource file and then get string value from it. I have been strugglling and googling this for a lone time, still no luck. Always get:
"System.Resources.MissingManifestResourceException: Could not find any resources appropriate for the specified culture or the neutral culture. Make sure "EDIResources.resources" was correctly embedded or linked into assembly "EDIWebService" at compile time, or that all the satellite assemblies required are loadable and fully signed."
Here is what I did:
1. created a resource file "EDIResources.resx" in 'App_GlobalResources' folder from VS 2008 solution exploer added some key/value pairs into it.
2. in C# class, I used below code to load the resource file:
ResourceManager resourceManager = new ResourceManager("Resources.EDIResources", Assembly.GetExecutingAssembly());
3. get string from resources:
resourceManager.GetString("E_0008");
but step 3 threw out the System.Resources.MissingManifestResourceException exception. I tried different suggestions for step 2: using file based resource manager, changing resource file name to start with my web services' namespace...none of them work!
Please help me out! Thanks a lot!
|
|
|
|
|
i dont have any experience actually using resource files, but one thing to check is that the resx file is marked as 'Embedded Resource'.
Check out this: Understanding Embedded Resources in Visual Studio .NET[^]
Might help out a bit.
If at first you don't succeed ... post it on The Code Project and Pray.
|
|
|
|
|
Thanks! Tried, still no luck!
|
|
|
|
|
|
Hi All,
I have a string variable contain text that I would like to search for "Keyword" (AND, OR ) I like to do is search the string varaible strValue for the word "AND" that follow by word "OR" and insert "(" and the last word "OR" and insert ")" .The last "OR" might have "AND" or blank space, The combination could be any order I need to place the "(" and ")" where the "OR" exist.
For Example:
Orignal strValue = " col1 = "John" and col4 = "smith" and col3 = "John" or col7 <> "closed" or col9 > 1 and col5 = "passed" "
After the replace the strValue should be :
strValue = " col1 = "John" and col4 = "smith" and (col3 = "John" or col7 <> "closed" or col9 > 1 ) and col5 = "passed" "
The strValue then send to SQL store procedure as a where clause in sql statement.
Any assistance is greatly apprecaited. please provide codes example.
Thanks
DocHoliday.
|
|
|
|
|
It's probably not ideal to be dynamically generating WHERE clauses and passing the to your procedures. Could you post the Stored Proc code as there might be an easier way round this?
|
|
|
|
|
Hi,
I started writing a function to do the replace/insert but ran into issue when there are more then one "AND" then follow by "OR", the "(" get insert after the first "AND". Also when there blank space after the "OR" at the end of the string the function fail.
Thanks,
DocHoilday
Function codes:
Private Function ReplaceString(ByVal strValue As String) As String
Dim andIndex As Integer = strValue.IndexOf("AND")
Dim blankIndex As Integer = strValue.IndexOf("Blank")
Dim orIndex As Integer = strValue.IndexOf("OR")
Dim nextAndIndex As Integer = strValue.IndexOf("AND", andIndex + 3)
Dim secondBlankIndex As Integer = strValue.IndexOf("Blank", blankIndex + 5)
Dim temp As String = String.Empty
if orIndex > andIndex AndAlso orIndex < secondAndIndex Then
temp = strValue.Substring(0, andIndex + 3) + " ("
temp += strValue.Substring(andIndex + 3, secondAndIndex - andIndex - 3).Trim() + ") "
temp += strValue.Substring(secondAndIndex)
ElseIf orIndex > andIndex AndAlso orIndex < secondBlankIndex Then
temp = strValue.Substring(0, andIndex + 3) + " ("
temp += strValue.Substring(blankIndex + 5, secondBlankIndex - blankIndex - 5).Trim() + ") "
temp += strValue.Substring(secondBlankIndex)
End If
Return temp
End Function
Here is the SP code:
CREATE PROCEDURE Get_Build_My_Report
(
@Application_id CHAR(5),
@DisplayColumn VARCHAR(2000),
@Filters VARCHAR(1000) = NULL
)
AS
DECLARE @Select VARCHAR(60)
DECLARE @FROM VARCHAR(60)
DECLARE @JOIN VARCHAR (2000)
DECLARE @WHERE VARCHAR(60)
DECLARE @TempColumnName VARCHAR(1000)
DECLARE @Var VARCHAR(1000)
SET @Select = 'SELECT'
SET @FROM = 'FROM Issues_Tracking it'
SET @JOIN = 'LEFT JOIN User_mstr um ON it.Assigned_To_id = um.userid AND it.application_id = um.application_id
LEFT JOIN Status s ON it.status = s.status_id
LEFT JOIN NGModules m ON it.module = m.mod_id AND it.application_id = m.application_id
LEFT JOIN Rank_level rl ON it.rank = rl.rank_id
LEFT JOIN NGProducts p ON it.product = p.product_id
LEFT JOIN Request_Type rt ON it.Request_Type = rt.Request_Type_id
LEFT JOIN practice pr ON it.Practice_id = pr.practice_id
LEFT JOIN Response r ON it.Submitted_to_Vendor = r.Answer_id
LEFT JOIN Response r2 ON it.Issues_Known_To_Vendor = r2.Answer_id
LEFT JOIN Environment e ON it.Environment = e.Environment_id
LEFT JOIN User_mstr um2 ON it.Fixed_by = um2.userid AND it.application_id = um.application_id
LEFT JOIN User_mstr um3 ON it.tested_by = um3.userid AND it.application_id = um.application_id
LEFT JOIN User_mstr um4 ON it.tested_2_by = um4.userid AND it.application_id = um.application_id
LEFT JOIN User_mstr um5 ON it.tested_3_by = um5.userid AND it.application_id = um.application_id
LEFT JOIN Test_Status ts ON it.test_result = ts.test_id
LEFT JOIN Test_Status ts2 ON it.test_2_result = ts2.test_id
LEFT JOIN Test_Status ts3 ON it.test_3_result = ts3.test_id '
SET @WHERE = 'WHERE it.Application_id ='
EXEC(@Select + ' ' + @DisplayColumn + ' '+
@FROM + ' ' + @JOIN + ' ' + @WHERE + ' ' + @Application_id + ' ' + @Filters)
SET QUOTED_IDENTIFIER OFF
|
|
|
|
|
I forgot to mentioned that the @Filters varaible is the where clause.
|
|
|
|
|
Hi,
I sort of see what you're trying to do here but I think there are neater ways.
You're not really going to get the benefits of stored procedures but building up your sql like this and it's going to be very difficult to maintain.
I think you're trying to get one procedure to cater for all possible data access that you might want at any point in your application. How many different combinations of data do you need from exactly these tables?
I'd recommend splitting this procedure into several more specific procedures and then calling the procedures with strongly typed parameters. Or if you're building a much bigger system try reading up on ORM.
If you want to post a typical query that you might need from this database I can have a look at reformatting it for you.
|
|
|
|
|
Hi,
I am basically building a page that allow user to create their own report. On the page I got all the columns of the table displayed for user to pick to show on the report. Next if they wish do a drill down then they would select criteria as they select the criteria(s) I need to group the "AND" and "OR" to send to the store procedure as one query.
Thanks,
DocHoliday
|
|
|
|
|
OK, so is the report a datagrid bound to the dataset created by this query?
Why don't you try something like this:
Create a view of your main query which returns all possible columns you might want to display on the report.
Then in your stored procedure have a parameter for each column you might want to filter on.
@col1 varchar(50) = null,
@col2 int = null,
@col3 bit = null
SELECT col1, col2, coln
FROM view1
WHERE 1 = 1
AND isNull(@col1, col1) = col1
AND isNull(@col2, col2) = col2
AND isNull(@col3, col3) = col3
Then depending on what the user has requested by drilling down, show/hide columns in the datagrid and pass in only the parameters that you want in the where clause. The isNull() will ignore any parameters you pass in as null and so won't take effect. If you want OR's you could run the procedure multiple times and join the datasets that are returned.
|
|
|
|
|
Can you show example regarding the OR's having run the procedure multiple times and join the datasets that are returned ?
|
|
|
|
|