OulcanAkca-9475 avatar image
0 Votes"
OulcanAkca-9475 asked karenpayneoregon answered

how do I write data that is not deleted with database when the form is closed and opened? | SQL

 private void btnPublish_Click(object sender, EventArgs e)
             Sqlbaglan.NesneVer().thisConn = new SqlConnection(@"Data Source=(localdb)\mssqllocaldb;initial catalog = ManagerPanel;integrated security = true");
             Sqlbaglan.NesneVer().thisQuery = new SqlCommand("SELECT Kurallar FROM [Rules] WHERE TC=@TC", Sqlbaglan.NesneVer().thisConn);
             Sqlbaglan.NesneVer().thisQuery.Parameters.AddWithValue("@TC", tbxTC.Text);
             label1.Text = Sqlbaglan.NesneVer().thisQuery.ExecuteScalar().ToString();

Friends, I have 2 problems, I do not have much knowledge of sql, but I wanted to do my job with the database. I created sql conn and query object with singleton dp. I called the rule that has that TC with the TC entered in tbxTC and printed it on label1.Text in the SAME FORM. So far, everything happens as I want. My 1st problem is that when I open and close the form, label1.Text gets reset. Consider my second problem independent of my first problem. My second problem is that I have written in capital letters already - I am nervous- I have to show it in another form. This form's name is frm2. So consider label1.text is in another form.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

DuaneArnold-0443 avatar image
0 Votes"
DuaneArnold-0443 answered OulcanAkca-9475 commented

Well, if you were on the right track, then you would be using SoC in your program.

None of the database code should be in the button_click event, the event should have called a method that had the database code and let the database method do the work on the behalf of the button_click event.

Now with the database code being in its own area, the database code can be called from anywhere within the program by any form in the program and even from a Form_Closing event.

This may help you, and I say go back to the starting blocks and get a little basic understanding. You seem to be sharp minded and you should be able to easily pick it up.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

As far as I understand it has a link-layered architecture that you send me. Frankly, I'm a new candidate for C-sharp and Sql :). So I wanted to get my work done without layered architecture. What change should I make here without using layered architecture?

0 Votes 0 ·
karenpayneoregon avatar image
1 Vote"
karenpayneoregon answered

If you want what was retrieved next time the form is opened create a setting under project properties. In the following simple example there is a setting LastCustomerIdentifer with a default value of -1 which indicates it has not been used.


  • All code presented requires C#8 which will work with C# 9 too

  • Only reason for a singleton is you mentioned using one, there really is no reason to use a singleton as static methods in a class will work just fine.

Singleton class

 using System;
 using System.Data.SqlClient;
 namespace SimpleConnectSqlServer.Classes
     public sealed class DataOperations
         private static string _connectionString =
             "Data Source=.\\sqlexpress;Initial " +
             "Catalog=NorthWind2020;Integrated Security=True";
         private static readonly Lazy<DataOperations> Lazy = new(() => new DataOperations());
         public static DataOperations Instance => Lazy.Value;
         public string CompanyName(int identifier)
             using var cn = new SqlConnection { ConnectionString = _connectionString };
             using var cmd = new SqlCommand
                 Connection = cn, 
                 CommandText = "SELECT CompanyName FROM Customers WHERE CustomerIdentifier = @Id"
             cmd.Parameters.AddWithValue("@Id", identifier);
             return (string) cmd.ExecuteScalar();

Form code

 namespace SimpleConnectSqlServer
     public partial class Form1 : Form
         private int _iterations = 6;
         public Form1()
             Closing += OnClosing;
             Shown += OnShown;
         private void OnShown(object? sender, EventArgs e)
             if (Properties.Settings.Default.LastCustomerIdentifier <= -1) return;
             CompanyNameLabel.Text = DataOperations.Instance.CompanyName(Properties.Settings.Default.LastCustomerIdentifier);
             IdentifierTextBox.Text = Properties.Settings.Default.LastCustomerIdentifier.ToString();
         private void OnClosing(object sender, CancelEventArgs e)
             if (Properties.Settings.Default.LastCustomerIdentifier > -1)
         private void GetCustomerNameButton_Click(object sender, EventArgs e)
             if (!int.TryParse(IdentifierTextBox.Text, out var id)) return;
             CompanyNameLabel.Text = DataOperations.Instance.CompanyName(id);
             Properties.Settings.Default.LastCustomerIdentifier = id;

Verify in SSMS

(SQL-Server Management Studio)

Settings tab under project properties


f2.png (7.6 KiB)
f1.png (9.0 KiB)
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.