2

I am new to net developing and have managed to work my way through a lot of questions I have had just by looking through the forums.

It appears that the issue that I am having is something that a number of others have had I have found that they are all different and just haven't for the life of me been able to work through it.

I am trying to insert player registration details into database but when I try to invoke the wcf server it am met with the exception type on my conn.Open():

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code.

In addition I am using the build it sql server and the connection string used is one from properties on the database. I am not too sure how to proceed.

public string playerRegistration(playerDetails playerInfo)
{            
    string Message;
    using (SqlConnection conn = new SqlConnection("Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\\Users\\Daniel\\documents\\visual studio 2013\\Projects\\Prac4\\WebApplication1\\App_Data\\ADODatabase.mdf;Integrated Security=True"))
    {
        conn.Open();

        using (var cmd = new SqlCommand("INSERT into Player(pid, pfname, plname, pphone, paddress, pdob) VALUES (@pid, @pfname, @plname, @pphone, @paddress, @pdob)", conn))
        {
            cmd.Parameters.AddWithValue("@pid", playerInfo.Pid);
            cmd.Parameters.AddWithValue("@pfname", playerInfo.Pfname);
            cmd.Parameters.AddWithValue("@plname", playerInfo.Plname);
            cmd.Parameters.AddWithValue("@pphone", playerInfo.Pphone);
            cmd.Parameters.AddWithValue("@paddress", playerInfo.Paddress);
            cmd.Parameters.AddWithValue("@pdob", playerInfo.Pdob);

            int result = cmd.ExecuteNonQuery();
            if (result == 1)
            {
                Message = " Details inserted successfully";
            }
            else
            {
                Message = " Details not inserted successfully";
            }
            conn.Close();
            return Message;
        }
    }
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397

2 Answers2

5

Make sure to use @".." (a verbatim string literal) with connection strings to avoid simple escaping mistakes.

The code shown with "..\v.." contains a vertical tab escape which produces an invalid connection string. There is no compiler error because the string literal is syntactically valid although the resulting string is incorrect.

Recommended fix with a verbatim string literal and elimination of double slashes:

@"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\Users\Daniel.."

Alternative fix (note the \\v):

"Data Source=(LocalDB)\\v11.0;AttachDbFilename=c:\\Users\\Daniel.."
Community
  • 1
  • 1
user2864740
  • 60,010
  • 15
  • 145
  • 220
0

The problem is in your connection string

"Data Source=(LocalDB)\v11.0;AttachDbFilename=c:\\Users\\Daniel\\documents\\visual studio 2013\\Projects\\Prac4\\WebApplication1\\App_Data\\ADODatabase.mdf;Integrated Security=True"

Search the internet to find the required format for SQL Server. You do not need an MDF file name, here's a helpful link:

https://www.connectionstrings.com/sql-server/

Grantly
  • 2,546
  • 2
  • 21
  • 31
  • Why do you think that OP uses Access, he's clearly using SQL-Server. – Tim Schmelter Oct 25 '14 at 22:38
  • The name of his MDF file in the connection string, in the AttachDbFilename section. Hence my thoughts that the entire connection string is incorrect – Grantly Oct 25 '14 at 22:40
  • 2
    But a mdf-file is a sql-server file. http://stackoverflow.com/questions/1175882/what-is-an-mdf-file – Tim Schmelter Oct 25 '14 at 22:43
  • Oh ok. I had a look through there but I am using the sql server on visual studio I tried "Server =(LocalDB)\v11.0;Database = ADODatabase;Trusted_Connection=True" but received the same error. Do you think it would be the database causing the issues? – Daniel Aguirre-Davies Oct 25 '14 at 23:06
  • @TimSchmelter Ooops thanks...got confused with Access. Edited answer. – Grantly Oct 25 '14 at 23:09
  • @DanielAguirre-Davies The connection string is quite specific, and this is indeed the problem. – Grantly Oct 25 '14 at 23:09
  • I have found on the link is the detail that I have gone into the problem or perhaps the detail of the filename Server=(localdb)\v11.0;Integrated Security=true; AttachDbFileName=C:\MyFolder\MyData.mdf; – Daniel Aguirre-Davies Oct 25 '14 at 23:19
  • Hi @DanielAguirre-Davies, user user2864740 has posted a more accurate answer. You need an extra \ character before the v in v11.10 – Grantly Oct 25 '14 at 23:21