-1

I have a Winform program that has a lot of SqlConnection function that interacts with a local MDF database.

This is how I create the connection string:

String dbPath = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Bot\DB\BotDB.mdf";
String con = String.Format("Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"{0}\";Integrated Security=True;Connection Timeout=10;", dbPath);
return con;

And this is one of many calls i made to the Database:

public bool InsertNewUserToTable(String username)
{
    using (SqlConnection con = new SqlConnection(DataBase.GetConString()))
    {
        con.Open();

        String query = @"INSERT INTO dbo.Users(username) VALUES(@username);";

        using (SqlCommand cmd = new SqlCommand(query, con))
        {
            cmd.Parameters.AddWithValue("@username", username);

            int count = cmd.ExecuteNonQuery();

            con.Close();

            return (count == 0) ? false : true;
        }
    }
}

I have more methods like this one that deletes, update, insert data from the Database.

The program working with multithread that access the database in this way.

The problem is that sometimes I get a crash error(on publish mode):

Problem signature:
  Problem Event Name:   CLR20r3
  Problem Signature 01: Bot.exe
  Problem Signature 02: 1.0.0.0
  Problem Signature 03: 5cbce361
  Problem Signature 04: System.Data
  Problem Signature 05: 4.7.2623.0
  Problem Signature 06: 5a1f67d8
  Problem Signature 07: 1a55
  Problem Signature 08: 5e
  Problem Signature 09: System.Data.SqlClient.Sql
  OS Version:   6.3.9600.2.0.0.272.7
  Locale ID:    1033
  Additional Information 1: 4e4b
  Additional Information 2: 4e4be395a3a959f2a72f71ab9c9204ab
  Additional Information 3: 783e
  Additional Information 4: 783ea04fb900812a4241ad9aeb1b45b6

Read our privacy statement online:
  http://go.microsoft.com/fwlink/?linkid=280262

If the online privacy statement is not available, please read our privacy statement offline:
  C:\Windows\system32\en-US\erofflps.txt

I tried to find a way to find the specific Exception with this information: https://stackoverflow.com/a/4053325/679099

But I wasn't able to find any specific thing that crash my application.

Any idea what can be the issue? Something else that I'm missing?

YosiFZ
  • 7,792
  • 21
  • 114
  • 221
  • Cant you log the exception? That would make life a lot easier ... ps, more info might be written to the event log. – Stefan May 09 '19 at 20:50
  • @Stefan This is a code with more than 50 methods that connect to the DB, i even don't know which of them made this problem. where i can find this log? – YosiFZ May 09 '19 at 20:53
  • 1
    You could make an general uncautch exeption handler. As for the event log, if it's on windows, than it would be something like this https://www.howto-connect.com/event-viewer-on-windows-10/ – Stefan May 10 '19 at 07:03

1 Answers1

0

Try to define a private SqlConnection and put this code under InitializeComponent();

con = new SqlConnection(@"Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"{0}\";Integrated Security=True;Connection Timeout=10;User Instance=True;MultipleActiveResultSets=True;");
con.Open();

This connection works for multiple connections opened, because you have MARS=True;

Then for SqlCommand, you need to make only like this:

SqlCommand cmd = new SqlCommand("query", con);

Final code of SqlConnection must look like this:

private SqlConnection con;
public Form1()
{
    InitializeComponent();
    con = new SqlConnection(@"Data Source=(LocalDB)\\v11.0;AttachDbFilename=\"{0}\";Integrated Security=True;Connection Timeout=10;User Instance=True;MultipleActiveResultSets=True;");
    con.Open();
}
Yamato
  • 17
  • 5