hi friends,
i had a problem when connecting the database to the asp.net. My database is SQL server 2005

Dani AI

Generated

Quick practical checklist and a minimal example to connect ASP.NET to SQL Server 2005, building on points from , , and . ’s pointer to connection-string examples is useful; showed a direct SqlConnection (VB); recommended validating creds with the datasource wizard; suggested placing the string in Web.config. The items below cover the common causes that stop a connection and a safe way to wire the string into an ASP.NET app.

  • Confirm the server identifier: default-instance uses just the server name; named instances use ServerName\InstanceName; an explicit port can be given as ServerName,1433.
  • Confirm authentication mode: Windows Integrated vs SQL Server (mixed) authentication and that the SQL login exists and has permissions on the DB.
  • Ensure the SQL service is running, TCP/IP protocol enabled in SQL Server Configuration Manager, and the SQL Server Browser service is running for named instances.
  • Check firewall rules and that the database server is reachable from the web host (SSMS, sqlcmd, or a quick telnet to the port).
  • Store the connection string in Web.config and protect credentials (encrypt the section for production).

Example Web.config connectionStrings section:

<configuration>
  <connectionStrings>
    <add name="DefaultConnection"
         connectionString="Server=SERVERNAME\INSTANCE;Database=MyDatabase;User Id=myuser;Password=mypassword;"
         providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

Minimal C# usage pattern:

using System.Configuration;
using System.Data.SqlClient;

string cs = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (var conn = new SqlConnection(cs))
{
    conn.Open();
    // run commands, then dispose
}

If a connection still fails, capture the SqlException.Number and message to narrow the cause (authentication vs network vs DB permission). See Microsoft guidance on connection strings and config files: Connection strings and configuration files and on enabling remote access for SQL Server: .

Recommended Answers

All 4 Replies

this following code to connect asp.net with sqlserver

Imports System.Data
Imports System.Data.SqlClient

Module ConnectionDB
    Public conn As New SqlConnection("Server = j3rry;" & "initial Catalog = PP60;" & " Trusted_Connection=yes")
End Module

... try using the New SQL Datasource option to make sure your creds are good.

You could also place this "connection" in a Web.config file, for security purposes...

And to be sure... what version of ASP.NET are you speaking of? .Net 1.0, 1.1, 2.0, 3.0 ?

2.0 Tutorial:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.