web service in asp.net - asp.net

I want to create an web service for 'sign up' page or database connectivity..
I am done with database connectivity
but i am unable to get how ca i design an sign up page in web service.. as there is no interface in web service.
Please tell me
my web service code is :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
namespace WcfService1
{
/// <summary>
/// Summary description for WebService1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
string Connection = "Data Source=SHUMAILA-PC;Initial Catalog=kse;User ID=sa;Password=sa";
[WebMethod]
public void SQLconn()
{
SqlConnection DataConnection = new SqlConnection(Connection);
// the string with T-SQL statement, pay attention: no semicolon at the end of //the statement
string Command = "INSERT INTO login VALUES ('hina','me12')";
// create the SQLCommand instance
SqlCommand DataCommand = new SqlCommand(Command, DataConnection);
// open the connection with our database
DataCommand.Connection.Open();
// execute the statement and return the number of affected rows
int i = DataCommand.ExecuteNonQuery();
//close the connection
DataCommand.Connection.Close();
}
}
}

but i am unable to get how ca i design an sign up page in web
service.. as there is no interface in web service.
In fact your reasoning is correct. An ASMX web service implements the SOAP protocol and cannot have any GUI interface whatsoever. Top design an interface of a web application in .NET you could use ASP.NET for example. So you could create an ASP.NET application which will consume this web service. By the way ASX web services are considered as deprecated technology and you should use WCF instead.

There are different techniques. However, its common to utilize Ajax requests to your webmethod via AJAX.
Here is an older walk-through but its still valid - http://dotnetslackers.com/articles/ajax/Using-jQuery-with-ASP-NET.aspx
It demonstrates two approaches using jQuery (which I recommend) and Microsoft Ajax.
Here are some more decent links -
http://www.asp.net/ajaxlibrary/jquery_dibs.ashx

Related

how to convert date format for ASP.Net Web API with signalR?

I have implemented asp.net web api along with signalr in order to push data to the client.
The payload consists of pushing a DateTime value to the browser thru signalR in Asp.Net Web API.
The problem is when the data is recievied on browser I am getting date in iso format
i.e. 2012-12-04T19:30:18 but i want data in microsoftdate format i.e "\date(...)"
For this to work I tried following in the application_start event in global.asax of asp.net web api:
var json =config.Formatters.JsonFormatter;
json.SerializerSettings.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
but still getting date as "2012-12-04T19:30:18".
Any idea?
Starting with SignalR 1.0, you can register your own JsonNetSerializer on app start with custom settings like so:
using System;
using System.Web;
using Microsoft.AspNet.SignalR;
using Newtonsoft.Json;
namespace MyApp
{
public class Global : HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
var settings = new JsonSerializerSettings();
settings.DateFormatHandling = DateFormatHandling.MicrosoftDateFormat;
var serializer = new JsonNetSerializer(settings);
GlobalHost.DependencyResolver.Register(typeof(IJsonSerializer), () => serializer);
}
}
}
I didn't entirely follow the issue you're running into but I'm assuming you're trying to pass the current DateTime down from the server to the client and read it?
If so you can send it/parse via the following (no need for any serialization changes):
SignalR datetime format
This is the JavaScript way to read the DateTIme.

ASP.NET 2.0, SQL Server Express 2008, Sync Framework 1.0 C# Simplest Scenario Example

Good Day Everyone,
I know, the versions I am talking about are rather obsolete by now but that's the tools I am stuck to work with at the workplace. This is my first question on StackOverflow, and I hope I will get the formatting right hehe ;-) Please pardon me for the long text, I am used to give a lot of details and, in a sense, I feel the more details I provide the more accurate might be the answers ;-)
In almost 10 years working in IT, I've always been able to find answers to my questions (i.e. solutions to my problems) by Googling well chosen keywords and expressions. Well, it looks like the aforementioned Sync Framework is either not very well known to the Internet community, or it is a real pain to try to understand its simplest concepts for most mortals. After extensive research I have to found a single, simple example of syncing SQL Express using Sync Framework 1.0 and the C# language, not even on MSDN! I am fairly new to ASP.NET / C# but I understand the concepts and I have a working web application that successfully stores and retrieve data from an SQL Server 2008 database. It has been in use by clients for two years now. We now have a requirement for the clients to be able to bring their data offline and be able to update it offline and then sync with the server. UPDATEs, INSERTs and DELETEs will occur at both ends.
What I am trying to find is VERY simple (or so I thought): C# code examples that uses SQL Server Change Tracking information (NOT custom Change Tracking) to sync the server (SQL Server 2008) and client computers (SQL Server 2008 Express, NOT Compact Edition). The simplest case would be a single table with few columns. I am fairly confident to understand the SQL Server part and I've prepared both sides of the database to receive sync requests from the client web application (enabled Change Tracking, PrimaryKeyID has data type GUID, the application's user account on the server has VIEW_CHANGE_TRACKING permission, etc. etc.)
I know it's the web application that serves as the interface between the two and that manage the sync session (in C#). I was quite naive to think that the only thing left to do was to provide the two connection strings, tell what tables are to sync and specify a bidirectional sync. Apparently, that's more complicated than that hehe. In a desperate attempt, I have tried to base my code on the following from Microsoft and adapt it to SQL Express (the example is for Compact). I am close to admit defeat and shamefully lower my head :-(
http://msdn.microsoft.com/en-us/library/bb726015%28v=sql.100%29.aspx
Based on the above (second section "Complete Example Using SQL Server Change Tracking"), I removed everything I do not need: things related to the password, the statistics, and the code applying changes to the data. I've also removed MS' numerous comment lines for clarity. I've manually applied changes at the SQL Server itself at both ends executing scripts in SSMS (and therefore there must be Change Tracking info that was generated and that is usable when the web app will request the sync). QUESTION 1: Am I wrong in saying so? Finally, I changed some stuff in an attempt to use objects relevant to SQL Express instead of Compact.
QUESTION 2: The code at Microsoft is apparently able to tell whether it is the initial (first) or subsequent sync of this replica. I don't have a clue how it can!
In the end, the code left in its simplest form is as below (with QUESTIONS 3, 4, 5 ;-), but shows some errors. I thank you VERY MUCH in advance for your help. Any comments and/or suggestions are welcome. I am sure if/when this is resolved it will benefit quite a lot of people out there. I will continue to research on it on my end (the boss won't give me a choice ;-) and I promise I will post the solution here if I ever succeed in syncing!
Thanks and have a great day everyone!
Kindest Regards,
Zyxy
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
//using System.Data.SqlServerCe;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.Server;
//using Microsoft.Synchronization.Data.SqlServerCe;
namespace some_namespace
{
public class SyncProgram
{
public SyncProgram()
{
// empty constructor
}
public static bool MainSync() // Entry point, say, called by a Sync button on an ASPX page.
{
bool boolSyncRes = false; // tells whether sync was a success or not
// Initial sync: they create a new instance of the Orchestrator.
ZyxySyncOrchestrator zyxySyncOrchestrator = new ZyxySyncOrchestrator();
// Subsequent synchronization.
// They don't. there was only irrelevant stats stuff here.
boolSyncRes = true;
return boolSyncRes;
}
}
public class ZyxySyncOrchestrator : SyncOrchestrator
{
public ZyxySyncOrchestrator()
{
Utility util = new Utility();
this.LocalProvider = new ZyxyServerSyncProvider(); // QUESTION 3: ??? cannot implicitly convert type DbServerSyncProvider to Microsoft.Synchronization.SyncProvider
//Instantiate a server synchronization provider and specify it
//as the remote provider for this synchronization agent.
this.RemoteProvider = new ZyxyServerSyncProvider(); // cannot implicitly convert type DbServerSyncProvider to Microsoft.Synchronization.SyncProvider
// QUESTION 4: Is the following code actually creating the base (user) table ZyxySync
// (as opposed to its change tracking metadata table)??
// I wasn't sure whether this part of the code on Microsoft's webpage was part of
// populating the db with sample data and structure or if it's really meant to deal with
// the change tracking metadata.
SyncTable zyxySyncTable = new SyncTable("ZyxySync");
zyxySyncTable.CreationOption = TableCreationOption.DropExistingOrCreateNewTable;
zyxySyncTable.SyncDirection = SyncDirection.DownloadOnly;
this.Configuration.SyncTables.Add(zyxySyncTable);
}
}
//Create a class that is derived from Microsoft.Synchronization.Server.DbServerSyncProvider.
public class ZyxyServerSyncProvider : DbServerSyncProvider
{
public ZyxyServerSyncProvider()
{
Utility util = new Utility();
SqlConnection serverConn = new SqlConnection(util.ServerConnString);
this.Connection = serverConn;
//Retrieve a new anchor value from the server. We use a timestamp value
//that is retrieved and stored in the client database.
//During each sync the new and last anchor values are used to determine the set of changes
SqlCommand selectNewAnchorCommand = new SqlCommand();
string newAnchorVariable = "#" + SyncSession.SyncNewReceivedAnchor;
selectNewAnchorCommand.CommandText =
"SELECT " + newAnchorVariable + " = change_tracking_current_version()";
selectNewAnchorCommand.Parameters.Add(newAnchorVariable, SqlDbType.BigInt);
selectNewAnchorCommand.Parameters[newAnchorVariable].Direction = ParameterDirection.Output;
selectNewAnchorCommand.Connection = serverConn;
this.SelectNewAnchorCommand = selectNewAnchorCommand;
//Create a SyncAdapter for the ZyxySync table by using
//the SqlSyncAdapterBuilder.
// Specify a name for the SyncAdapter that matches the
// the name specified for the corresponding SyncTable.
SqlSyncAdapterBuilder zyxyBuilder = new SqlSyncAdapterBuilder(serverConn);
zyxyBuilder.TableName = "dbo.ZyxySync";
zyxyBuilder.ChangeTrackingType = ChangeTrackingType.SqlServerChangeTracking;
SyncAdapter zyxySyncAdapter = zyxyBuilder.ToSyncAdapter();
zyxySyncAdapter.TableName = "ZyxySync";
this.SyncAdapters.Add(zyxySyncAdapter);
}
}
// Class derived from Microsoft.Synchronization.Data.Server.DbServerSyncProvider
// QUESTION 5: Or should have I used the two below? I believe they only apply to SQL Compact...
//Microsoft.Synchronization.Data.ClientSyncProvider
//Microsoft.Synchronization.Data.ServerSyncProvider
//http://msdn.microsoft.com/en-us/library/microsoft.synchronization.data.clientsyncprovider%28v=sql.100%29.aspx
//http://msdn.microsoft.com/en-us/library/microsoft.synchronization.data.server.dbserversyncprovider%28d=printer,v=sql.100%29.aspx
public class ZyxyClientSyncProvider : DbServerSyncProvider
{
public ZyxyClientSyncProvider()
{
Utility util = new Utility();
SqlConnection clientConn = new SqlConnection(util.ClientConnString);
this.Connection = clientConn;
}
}
public class Utility
{
public string ClientConnString
{
get { return #"Data Source=localhost\LocalExpressInstance;Initial Catalog=DatabaseName;User ID=UserName;Password=WontTellYou;"; }
}
public string ServerConnString
{
get { return #" Data Source=ServerName\ServerInstance;Initial Catalog=DatabaseName;User ID=UserName;Password=WontTellYou;"; }
}
}
}
the SyncOrchestrator will not work with the DBServerSyncProvider.
In Sync Framework, there are two types of database providers: the offline provider and the peer-to-peer/collaboration providers. (they both work in offline scenario so thats confusing).
the offline provider is used in hub-spoke topologies. Only the client tracks what was synched. The server doesnt even know its part of a sync. this is the same provider used by the Local Database Cache Project item in Visual Studio. The only supported databases out of the box is SqlCeClientSyncProvider and DBServerSyncProvider and uses the SyncAgent to synchronize.
the peer-to-peer provider can be used in a peer-to-peer sync as well as hub-spoke scenarios. Each peer maintains metadata on what was synched. This uses the much newer SyncOrchestrator/SqlCeSyncProvider/SqlSyncProvider(works with SQL Server, Express, LocalDB, and SQL Azure). This uses a custom change tracking.
you can't interchange the providers used by SyncAgent and SyncOrchestrator. You can'r reuse the SQL commands either because they differ in the way they track, select, apply changes and record what was synched.
Ok I managed to make it work so here is a SIMPLE code sample that works (in my case anyway). In addition to the above steps (enabling Change Tracking, setting the right user permissions, etc.), what I did not understand is the following:
1) I found out that I can set it up so that the Sync Framework and sync session is all managed on the client side. Without any dependency on what's installed on the server, I was able to use SF 2.1 instead of the old 1.0. That helped a lot.
2) In preparation for the sync session, one must first PROVISION the database so that it is ready for sync. What I did is to run the following C# with the client connection string (so that it provisions the client db) and then run it again with the server connection string (so that it provisions the server db). This is a run once program (on both sides) to prepare the db. You do NOT run it for every sync session you establish.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Security.Principal;
using System.IO;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.Server;
using Microsoft.Synchronization.Data.SqlServer; // SF 2.1
using Microsoft.Synchronization.SimpleProviders; // SF 2.1
using Microsoft.Synchronization.MetadataStorage; // SF 2.1
// ZYXY: Based on:
// http://msdn.microsoft.com/en-us/library/ff928603.aspx
// NOTES:
// - Microsoft Sync Framework 2.1 redistributable package must be installed on Client computers but is not required on the Server, as long as a server-side synchronization setup is performed by a client computer.
// This is a run once program.
namespace DISS_Database_Sync_Provisioning_Console
{
class Program
{
static void Main(string[] args)
{
SqlConnection sqlConn = new SqlConnection("Data Source=ServerName\\InstanceName;Initial Catalog=SomeDatabase;User ID=SOmeUser;Password=SomePassword;");
Console.Write("Provisioning database...");
// define a new scope named DISS_Sync_Scope
DbSyncScopeDescription scopeDesc = new DbSyncScopeDescription("DISS_Sync_Scope");
// get the description of the ZyxySync table
DbSyncTableDescription tableDesc = SqlSyncDescriptionBuilder.GetDescriptionForTable("dbo.ZyxySync", sqlConn);
// add the table description to the sync scope definition
scopeDesc.Tables.Add(tableDesc);
// create a server scope provisioning object based on the DISS_Sync_Scope
SqlSyncScopeProvisioning sqlProvision = new SqlSyncScopeProvisioning(sqlConn, scopeDesc);
// skipping the creation of base table since table already exists
sqlProvision.SetCreateTableDefault(DbSyncCreationOption.Skip);
// start the provisioning process
sqlProvision.Apply();
sqlConn.Close();
sqlConn.Dispose();
Console.Write("\nDatabase has been successfully configured for synchronization. Please press any key to exit.");
Console.Read();
}
}
}
3) Below is the code that is run everytime the synchronization is launched (e.g. when the user click their "Synchronize" button in their web application.)
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Security.Principal;
using System.IO;
using Microsoft.Synchronization;
using Microsoft.Synchronization.Data;
using Microsoft.Synchronization.Data.Server;
using Microsoft.Synchronization.Data.SqlServer; // SF 2.1
using Microsoft.Synchronization.SimpleProviders; // SF 2.1
using Microsoft.Synchronization.MetadataStorage; // SF 2.1
namespace diss_ssmb
{
public class SyncProgram
{
public SyncProgram()
{
// empty constructor
}
public static bool MainSync() // Entry point, say, called by a Sync button on an ASPX page.
{
bool boolSyncRes = false; // tells whether sync was a success or not
// Initial sync: they create a new instance of the Orchestrator.
ZyxySyncOrchestrator zyxySyncOrchestrator = new ZyxySyncOrchestrator();
// Subsequent synchronization.
// They don't. there was only irrelevant stats stuff here.
boolSyncRes = true;
return boolSyncRes;
}
}
public class ZyxySyncOrchestrator : SyncOrchestrator
{
public ZyxySyncOrchestrator()
{
Utility util = new Utility();
this.LocalProvider = new ZyxyClientSyncProvider();
//Instantiate a server synchronization provider and specify it
//as the remote provider for this synchronization agent.
this.RemoteProvider = new ZyxyServerSyncProvider();
SyncTable zyxySyncTable = new SyncTable("ZyxySync");
zyxySyncTable.CreationOption = TableCreationOption.DropExistingOrCreateNewTable;
zyxySyncTable.SyncDirection = SyncDirection.Bidirectional;
// this.Configuration.SyncTables.Add(zyxySyncTable);
this.Synchronize();
}
}
public class ZyxyServerSyncProvider : SqlSyncProvider
{
public ZyxyServerSyncProvider()
{
Utility util = new Utility();
SqlConnection serverConn = new SqlConnection(util.ServerConnString);
this.Connection = serverConn;
this.ScopeName = "DISS_Sync_Scope";
//Retrieve a new anchor value from the server. We use a timestamp value
//that is retrieved and stored in the client database.
//During each sync the new and last anchor values are used to determine the set of changes
SqlCommand selectNewAnchorCommand = new SqlCommand();
string newAnchorVariable = "#" + SyncSession.SyncNewReceivedAnchor;
selectNewAnchorCommand.CommandText =
"SELECT " + newAnchorVariable + " = change_tracking_current_version()";
selectNewAnchorCommand.Parameters.Add(newAnchorVariable, SqlDbType.BigInt);
selectNewAnchorCommand.Parameters[newAnchorVariable].Direction = ParameterDirection.Output;
selectNewAnchorCommand.Connection = serverConn;
// this.SelectNewAnchorCommand = selectNewAnchorCommand; // SF 2.1 commented out because SelectNewAnchorCommand isn't there.
SqlSyncAdapterBuilder zyxyBuilder = new SqlSyncAdapterBuilder(serverConn);
zyxyBuilder.TableName = "dbo.ZyxySync";
zyxyBuilder.ChangeTrackingType = ChangeTrackingType.SqlServerChangeTracking;
SyncAdapter zyxySyncAdapter = zyxyBuilder.ToSyncAdapter();
zyxySyncAdapter.TableName = "ZyxySync";
// this.SyncAdapters.Add(zyxySyncAdapter); // SF 2.1 commented out because SelectNewAnchorCommand isn't there.
}
}
public class ZyxyClientSyncProvider : SqlSyncProvider
{
public ZyxyClientSyncProvider()
{
Utility util = new Utility();
SqlConnection clientConn = new SqlConnection(util.ClientConnString);
this.Connection = clientConn;
this.ScopeName = "DISS_Sync_Scope";
}
}
public class Utility
{
public string ClientConnString
{
get { return #"Some connection string such as in the above code sample"; }
}
public string ServerConnString
{
get { return #"Some serverconnection string such as in the above code sample"; }
}
}
}
4) The above successfully synched bidirectionally when INSERTs, UPDATEs and DELETEs occurred simultaneously at both ends between two consecutive sync session, HOWEVER, when no conflict had to be resolved (e.g. when the same record is updated at both ends). I yet have to do further testing in cases when there are conflicts that have to be resolved. How is the Sync Framework resolving such conflicts by default? I assume we can adjust these settings to tell it to establish the winner based either on...
- a timestamp value
- the replicaID
- the user role
- the transaction type
- ...
Anyways I truly hope that helps somebody, because I had a real hard time to figure it out from the web! Good luck!
Zyxy

Is there a way to do an Ajax Post to an ASPX page that doesn't call a static function?

The question says it all. I am having no trouble making Ajax calls into my ASPX page - as long as I use static methods. However, I'd like to store a result in the session and so need a "live" function call. Any ideas?
Post to a Generic Http handler (.ashx) and have it inherit from IRequireSession interface. Then you can save to session.
I have done quite a bit of research on this problem and, while #latr0dectus' answer may work, I am looking to implement quite a few calls back to the application from within the page. Perhaps I needed to do more research but I did not find a way to call multiple methods within the Generic Http handler nor a way to pass in complex arguments. So, this didn't work very well for me.
A much easier solution is just to use a a WebService and to use the [WebMethod(EnableSession = true)] attribute.
A complete example of a Session-aware web service that you can reach from Ajax is:
using System;
using System.Collections.Generic;
using System.Web.Services;
using System.Web.SessionState;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService]
public class SCMasterService : System.Web.Services.WebService {
public SCMasterService () {
}
[WebMethod(EnableSession = true)]
public string GetSummaryList(string ID)
{
return Session["SomeVal"];
}
}
To call this from a Javascript/JQuery/Ajax "Post" you will need two key lines:
[System.Web.Script.Services.ScriptService]
Makes this web service available to Ajax and
[WebMethod(EnableSession = true)]
enables session management in the called method.

Getting an error on website when multiple users access same page simultaneoulsy

I'm getting the following error message on an ASP.NET 4 website when multiple users access the same page:
The underlying provider failed to open. at
System.Data.EntityClient.EntityConnection.OpenStoreConnectionIf
etc......
I use Entity Framework 4 to access the SQL Server 2008 database.
The page works sometimes so I know the connection string is correct. Also the Database is set to multi user and I have MARS set to true in connection string.
Also in the event viewer I sometimes get the SQL Server message :
The server will drop the connection, because the client driver has
sent multiple requests while the session is in single-user mode.
As I say, this only happens if I try accessing the same page simultaneously on two different machines by just refreshing the page or clicking the same link.
Any help would be much appreciated.
Connection strings added (first one for ASPNETDB and second for main database):
<add name="ApplicationServices" connectionString="Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=False;Persist Security Info=False;User ID=****;Password=****;Connect Timeout=120" />
<add name="MyDBEntities" connectionString="metadata=res://*/App_Code.MyDBModel.csdl|res://*/App_Code.MyDBModel.ssdl|res://*/App_Code.MyDBModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=False;User ID=****;Password=****;Connect Timeout=120;User Instance=false;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
The Class I use to access the context is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MyDBModel;
using System.Web.Security;
/// <summary>
/// Summary description for MyDataAccess
/// </summary>
public static class MyDataAccess
{
// Private Class Members
//-----------------------------------------------------------------------------------------
private static MyDBModel.MyDBEntities dal = new MyDBModel.MyDBEntities();
// Class Constructor / Destructor
//-----------------------------------------------------------------------------------------
static MyDataAccess()
{
// Set Entity ObjectContext Merge Options - this basically ensures database items aren't cached
dal.NEWS.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
dal.NEWS_IMAGES.MergeOption = System.Data.Objects.MergeOption.OverwriteChanges;
}
// Public Methods for Data Access
//-----------------------------------------------------------------------------------------
/// <summary>
/// Get All Current News Items
/// </summary>
public static List<NEWS> GetCurrentNewsItems()
{
var items = from news in dal.NEWS
where news.NEWS_ACTIVE == true &&
news.NEWS_STARTDATE <= DateTime.Today &&
news.NEWS_EXPIRYDATE >= DateTime.Today
orderby news.NEWS_DATE descending
select news;
return NewsManager.GetMyNewsItems(items).ToList();
}
}
Then within my .aspx.cs page I would use something like:
var news = MyDataAccess.GetCurrentNewsItems();
I believe your issue is being caused by storing your ObjectContext object in a static variable.
from msdn documentation of ObjectContext Class
The ObjectContext class is not thread safe. The integrity of data
objects in an ObjectContext cannot be ensured in multithreaded
scenarios.

How to delete temporary files using Asp.net

In my application, I have some reports which needs to be viewed frequently.After viewing the reports many times by different users, it shows load error.For different systems, many temporary files are created.i need to delete those files in my single system.now i manually deleting all the temporary files in the temp directory and configure the IIS again.then the report loads properly.But we need to delete these temporary files frequently which makes our life dreadful.Only the report files needs to be deleted.How can i delete these temporary files automatically using code?
I have used the following code for this.but some files cant be deleted as those files are in use.Do those temporary files in other system can cause load error in our system?how can i solve this?
dim temp as string=Environment.GetEnvironmentVariable("TEMP")
dim k as sting()=System.IO.Directory.GetFiles(temp)
dim i as integer
For i=0 to k.length
On Error Resume Next
If k(i).Contains(".rpt") then
kill(k(i))
System.IO.File.Delete(k(i))
Next
Create a thread from the Application_Start() (or write a standalone exe)
You can just automate what you are manually doing.
You can delete these files with a older modifier day, once an hour, with a very simple program .
Assuming you are presenting these temporary files to the user can I suggest creating an HTTP handler. The handler will provide the ability to generate a file and deliver it to the user to be either downloaded or viewed in browser. This approach allows for the customization of caching.
The example below is only showing the handler portion, this is as basic as it gets as doesn't go into the file creation as I am not sure how you are creating the files currently. You can send a stream of some sort.
Example: (Sorry in C#, but you can go from here.)
using System;
using System.Data;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.IO;
namespace Handlers
{
/// <summary>
/// Summary description for $codebehindclassname$
/// </summary>
[WebService(Namespace = "http://www.tempuri.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class ColorImage : IHttpHandler
{
public bool IsReusable { get { return true; } }
public void ProcessRequest(HttpContext context)
{
Bitmap bmGenerate = CreateBitmapMethod();
context.Response.ContentType = "image/png";
context.Response.AddHeader("Response-Type", "image/png");
using (MemoryStream memoryStream = new MemoryStream())
{
bmGenerate.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Png);
memoryStream.WriteTo(context.Response.OutputStream);
}
}
}
}

Resources