Hello,
The code was done in .NET Core 5, C#9 in VS2019. If using VS2017 then you will need to add the following NuGet package and add {} to both the connection and command objects. Not sure if I have the SQL correct but separated the insert and the read/
Data class
using System;
using System.Data.SqlClient;
namespace WorkingWithSqlServer.Classes
{
public class DataOperations1
{
private static string _connectionString =
"Data Source=.\\sqlexpress;" +
"Initial Catalog=NorthWind2020;" +
"Integrated Security=True";
public static (bool success, Exception exception, int? id) AddToDatabase(int barCodeIdentifier, int customerIdentifier, int quantity, string name)
{
var insertStatement =
"INSERT INTO Sales(barcode_id,customer_id, pos_id,qty) " +
"VALUES (@barcode_id,@customer_id) ";
using var cn = new SqlConnection(_connectionString);
using var cmd = new SqlCommand { Connection = cn, CommandText = insertStatement };
cmd.Parameters.AddWithValue("@barcode_id", barCodeIdentifier);
cmd.Parameters.AddWithValue("@customer_id", customerIdentifier);
cmd.Parameters.AddWithValue("@qty", quantity);
var trans = cn.BeginTransaction("Ops1");
try
{
cn.Open();
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@Name", name);
cmd.CommandText = "Select id From POS Where Name=@name";
cmd.ExecuteReader();
var identifier = Convert.ToInt32(cmd.ExecuteScalar());
return (true, null, identifier);
}
catch (Exception e)
{
try
{
trans.Rollback();
}
catch (Exception transEx)
{
return (false, transEx, null);
}
return (true, e, null);
}
}
}
}
Form code
private void InsertButton_Click(object sender, EventArgs e)
{
var (success, exception, identifier) = DataOperations1.AddToDatabase(temp_barcode, 1,1, "some name");
if (success)
{
PrintInvoice(identifier.Value);
}
else
{
MessageBox.Show($"Insert failed\n{exception}", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
private void PrintInvoice(int identifier)
{
}