Click here to Skip to main content
15,894,291 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
So i cant seem to connect to my database - This is my app.config:
XML
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
    
  <connectionstrings>
    <add name="Series" connectionString="Data Source=.;Initial Catalog=Series;IntegratedSecurity=True" providerName="System.Data.SqlClient" />

and this is my code in my DB class:
C#
public static SqlConnection ConnectToDB(SqlConnection connection)
        {
            try
            {
                if (connection == null)
                    connection = new SqlConnection(ConfigurationManager.ConnectionStrings["Series"].ConnectionString);
                connection.Open();
               
            }
            catch (Exception ex)
            {
                throw ex;
                
            }
            connection = DB.DisconnectFromDB(connection);
            return connection;
        }

- The name of the database i "Series"

What I have tried:

I think i tried everyting, cant really see the problem
Posted
Updated 9-Mar-17 4:00am
v2
Comments
F-ES Sitecore 9-Mar-17 9:29am    
Let's start with the basics....what error message do you get?
CHill60 9-Mar-17 9:30am    
You are setting the connection to the output from DisconnectFromDB
[no name] 9-Mar-17 9:47am    
Well you didn't describe a problem. Step through your code with the debugger and then you might figure it out.
sachin.vishwa90 10-Mar-17 0:55am    
have you mentioned the server address in your connection string?

As @NotPoliticallyCorrect has stated - you need to step through your code with the debugger, examine variables and program flow and you might be able to understand what is going on. This article should help you Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]

Your use of Try-Catch is not really sensible - you are not adding anything to the exception before escalating it, so just let it percolate up through the thread. This article might help you Exception Handling Best Practices in .NET[^]

Your problem is the line
C#
connection = DB.DisconnectFromDB(connection);
I have no idea what DB is but I'm guessing that DisconnectFromDB does what it says and disconnects your carefully created connection!

On a final note - I wouldn't pass the connection around like that. Encapsulate the connection variable in your class. For a simplistic example...
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace Sandbox
{
    public class DbClass
    {
        private SqlConnection connection;

        public bool ConnectToDb()
        {
            if (connection == null)
                connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectToDB"].ConnectionString);

            if (connection.State != ConnectionState.Open)
                connection.Open();

            return (connection.State == ConnectionState.Open);
        }
    }
}
Which I can call like this:
C#
var xdb = new DbClass();
  var b = xdb.ConnectToDb();
  Debug.Print(b.ToString());
- meaning the calling module does not need to know anything about System.Data.SqlClient ... which makes life a lot easier if I was to switch database provider
 
Share this answer
 
Start with your connection string: a Data Source of "." is probably the place to start.
Try setting up a connection in VS with the Server Explorer pane:
1) Open Server Explorer.
2) Right click "Data connections" and select "Add connection"
3) In the dialog that follows, select your DataSource, and database, specify the security info, and press the "Test connection" button.
4) When the connection works, press "OK"
5) Highlight your database in the Server Explorer pane, and look at the Properties pane. A working example of the connection string will be shown, which you can copy and paste into your app.config file.

You'll need to change it for production, of course.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900