You are always better off holding the connection string in the web.config file rather than coding it into your program. Then you merely reference it in this fashion:
[C#]
string connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString;
[VB]
Dim connStr As String = ConfigurationManager.ConnectionStrings("MainConnStr").ConnectionString
Note that the namespace for this is System.Configuration so for a console application the full namespace is required.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace BLL
{
public class signup
{
SqlConnection con = new SqlConnection("Data Source=<IP>;Initial Catalog=<DB name>;User id=<username>;Password=<password>;");
public string l_name
{
get;
set;
}
public string gender
{
get;
set;
}
public string DOB
{
get;
set;
}
public string address
{
get;
set;
}
public int create_login()
{
int result = 0;
SqlCommand cmd = new SqlCommand("sign_up",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sign_up";
In essence I think you could still reference the connection string in the web config, just place it where you create your connection. In that way, if your servers change, you don't have to recode the entire DLL file. Remember, write once, use many times.
The last thing any programmer wants to do is to re-write working code. Variables are what the web.config/app.config are for
My question is how can i use the web.config connection string on class library.
And if i make the connection string on same class i am getting this error.
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)
Er. Gursewak...
Member
65 Points
47 Posts
Connection not establish with server
Jul 10, 2012 10:39 AM|LINK
Hi Everybody,
Myself is Gursewak & i am new in this technology. I built a website & deploy to the hosting server. I am using class library for making DLL.
And i am using this connection string to connect with the server in class.
SqlConnection con = new SqlConnection("Data Source=IP(xxx.xxx.xxx.xxx);User id=<username>;Password=<password>; Initial Catalog=<DB name>;Integrated Security=True");
So any can tell me where is the issue that i am facing. Because i think the problem is in this connection string.
So plz help me guys.
Thanks in advance.
Er. Gursewak Singh
S/w Engineer
Rajneesh Ver...
All-Star
36791 Points
6749 Posts
Re: Connection not establish with server
Jul 10, 2012 10:48 AM|LINK
Try to connect Sql Server Management Studio with same credential or run the website t=with same connection string locally and check the issue.
Or you have to ask your Hosting Service provider.
www.rajneeshverma.com
Keep Forums Clean || Use Alert Moderators.
bbcompent1
All-Star
32974 Points
8500 Posts
Moderator
Re: Connection not establish with server
Jul 10, 2012 10:50 AM|LINK
You are always better off holding the connection string in the web.config file rather than coding it into your program. Then you merely reference it in this fashion:
web.config (http://connectionstrings.com/sql-server-2008)
</configSections> <appSettings /> <connectionStrings> <add name="db_connection" connectionString="Data Source=myServerAddress;Initial Catalog=myDataBase;Integrated Security=SSPI;" providerName="System.Data.SqlClient" /> </connectionStrings> <system.web>In your program: (http://weblogs.asp.net/owscott/archive/2005/08/26/Using-connection-strings-from-web.config-in-ASP.NET-v2.0.aspx)
[C#] string connStr = ConfigurationManager.ConnectionStrings["MainConnStr"].ConnectionString; [VB] Dim connStr As String = ConfigurationManager.ConnectionStrings("MainConnStr").ConnectionString Note that the namespace for this is System.Configuration so for a console application the full namespace is required.bbcompent1
All-Star
32974 Points
8500 Posts
Moderator
Re: Connection not establish with server
Jul 10, 2012 10:51 AM|LINK
Side bar, if you are remotely hosted, your connection should look more like this:
RameshRajend...
Star
7983 Points
2099 Posts
Re: Connection not establish with server
Jul 10, 2012 10:51 AM|LINK
Hi
Change and try this
"Data Source=IP(xxx.xxx.xxx.xxx); Initial Catalog=<DB name>;User id=<username>;Password=<password>;"
Thank u
Er. Gursewak...
Member
65 Points
47 Posts
Re: Connection not establish with server
Jul 10, 2012 10:52 AM|LINK
thanx for your rply but as i told i am using class library for my DLL. So there is no option of accessing web.config in it.
So please tell me the other way.
Thanx.
Er. Gursewak Singh
S/w Engineer
bbcompent1
All-Star
32974 Points
8500 Posts
Moderator
Re: Connection not establish with server
Jul 10, 2012 10:54 AM|LINK
Then you'll have to change your class library to accomodate the connection string. Post the code and we can probably help you.
Er. Gursewak...
Member
65 Points
47 Posts
Re: Connection not establish with server
Jul 10, 2012 10:57 AM|LINK
Below is the sample code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace BLL
{
public class signup
{
SqlConnection con = new SqlConnection("Data Source=<IP>;Initial Catalog=<DB name>;User id=<username>;Password=<password>;");
public string l_name
{
get;
set;
}
public string gender
{
get;
set;
}
public string DOB
{
get;
set;
}
public string address
{
get;
set;
}
public int create_login()
{
int result = 0;
SqlCommand cmd = new SqlCommand("sign_up",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "sign_up";
cmd.Parameters.Add("@f_name", SqlDbType.VarChar);
cmd.Parameters.Add("@l_name",SqlDbType.VarChar);
cmd.Parameters.Add("@gender",SqlDbType.VarChar);
cmd.Parameters.Add("@DOB", SqlDbType.VarChar);
cmd.Parameters.Add("@result", SqlDbType.Int);
cmd.Parameters["@result"].Direction = ParameterDirection.Output;
cmd.Parameters["@f_name"].Value = f_name;
cmd.Parameters["@l_name"].Value = l_name;
cmd.Parameters["@gender"].Value = gender;
cmd.Parameters["@DOB"].Value = DOB;
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
result = Convert.ToInt16(cmd.Parameters["@result"].Value);
con.Close();
}
}
}
Er. Gursewak Singh
S/w Engineer
bbcompent1
All-Star
32974 Points
8500 Posts
Moderator
Re: Connection not establish with server
Jul 10, 2012 11:01 AM|LINK
In essence I think you could still reference the connection string in the web config, just place it where you create your connection. In that way, if your servers change, you don't have to recode the entire DLL file. Remember, write once, use many times. The last thing any programmer wants to do is to re-write working code. Variables are what the web.config/app.config are for
Er. Gursewak...
Member
65 Points
47 Posts
Re: Connection not establish with server
Jul 10, 2012 11:05 AM|LINK
My question is how can i use the web.config connection string on class library.
And if i make the connection string on same class i am getting this error.
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)
Er. Gursewak Singh
S/w Engineer