steviejacques
New Member
I am trying to update the table in the database each time the file is downloaded. The database is the "DefaultConnection(WebsiteName)" that was created when I used the sample website template. It is the database that stores all the registered users. I have added this table to that database.\[code\]CREATE TABLE [dbo].[Download] ( [filename] NVARCHAR(50) NULL , [counter] INT NOT NULL DEFAULT 0 , );\[/code\]I created a HTTP Handler that gets fired when I click download and is working without the SQL connection part:\[code\]<%@ WebHandler Language="C#" Class="Download" %>using System;using System.Web;using System.IO;using System.Data;using System.Data.SqlClient;using System.Web.Configuration;public class Download : IHttpHandler { SqlConnection conn; SqlCommand cmd; private string FilesPath { get { return @"path to directory holding files"; } } public void ProcessRequest(HttpContext context) { string fileName = context.Request.QueryString["filename"]; if (!string.IsNullOrEmpty(fileName) && File.Exists(FilesPath + fileName)) { context.Response.ContentType = "application/octet-stream"; context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName)); context.Response.WriteFile(FilesPath + fileName); //connect to the db conn = new SqlConnection( "Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-websiteName-20130405020152;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-websiteName-20130405020152??.mdf"); //the sql command to increment counter by 1 cmd = new SqlCommand("UPDATE counter SET counter = counter+1 WHERE filename=@filename", conn); cmd.CommandType = CommandType.Text; cmd.Parameters.AddWithValue("@filename", "Default"); using (conn) { //open the connection conn.Open(); //send the query cmd.ExecuteNonQuery(); } conn.Close(); } else { context.Response.ContentType = "text/plain"; context.Response.Write(FilesPath + fileName + " Invalid filename"); } } public bool IsReusable { get { return false; } } }\[/code\]I cannot get it to connect with any connection strings I can find. I have tried the one that is shown in the "web.config". It will always try to connect for a while, but then throws an exception on the \[code\]conn.Open();\[/code\] line saying it couldn't connect: \[quote\] A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server) .\[/quote\]My main question is, how do I connect to this default database so that I can update the information in this table when the file is downloaded.