PetaPoco update just modified records in winforms - c#

I have this code :
namespace PetaPocoTest
{
public partial class Form1 : Form
{
PetaPoco.Database db = new PetaPoco.Database("PgConnection");
IEnumerable<customers> allCustomers;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
allCustomers = db.Query<customers>("SELECT * FROM customers");
mGrid.DataSource = allCustomers .ToList();
}
private void saveButton_Click(object sender, EventArgs e)
{
foreach (var a in allCustomers)
{
db.Save("customers", "custumer_id", a);
}
}
}
}
bat this updates all record, no matter if they are changed or not.
So, my question is does anyone know how to update only changed records in petapoco ?

This is how I did it (eventually!) :
namespace PetaPocoTest
{
public partial class Form1 : Form
{
PetaPoco.Database db = new PetaPoco.Database("PgConnection");
Dictionary<string, int> modRows = new Dictionary<string, int>();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
bindingSource = db.Fetch<customers>("SELECT * FROM customers");
mGrid.DataSource = bindingSource;
}
private void saveButton_Click(object sender, EventArgs e)
{
db.BeginTransaction();
foreach (customers c in bindingSource)
{
if (modRows.ContainsKey(c.id.ToString()))
{
db.Save("customers", "id", c);
}
}
db.CompleteTransaction();
}
private void mGrid_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
int recId = ((customers)bindingSource.Current).id;
if (!modRows.ContainsKey(recId.ToString()))
{
modRows.Add(recId.ToString(), recId);
}
}
}
}
It functions, but if anyone has better idea pls share!

This is the simplest way from me to do this by db.BeginTransaction on Form_Load
private void Form1_Load(object sender, EventArgs e)
{
studentBindingSource.DataSource = db.Query<student>("SELECT * FROM student");
db.BeginTransaction(); // Begin Transaction here
}
On Update (CellValueChanged) just simply do db.Save() this doesn't commit yet because our transaction is not complete yet
private void studentDataGridView_CellValueChanged(object sender, DataGridViewCellEventArgs e)
{
db.Save((student)studentBindingSource.Current);
}
And yes we can do Insert and Delete as well !!!
Insert
private void buttonInsert_Click(object sender, EventArgs e)
{
student newStudent = new student
{
StudentName = "POCONEW"
}
studentBindingSource.Add(newStudent);
db.Save(newStudent);
}
Delete
private void studentDataGridView_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
int rowIndex = e.Row.Index; // We use Gridview row index instead because BindingSource.Current isn't work when user drag then deleting multiple row
db.Delete(studentDataGridView.Rows[rowIndex].DataBoundItem as student);
}
Finally to save change we just do db.CompleteTransaction()
:Just a little note here after user press the Save Button if you don't close the form you have to call db.BeginTransaction() again if not everything user edit after this will be saved automatically because we don't have Transaction anymore
private void btSave_Click(object sender, EventArgs e)
{
db.CompleteTransaction();
db.BeginTransaction(); // everything user edit after this will be saved automatically because we don't have Transaction anymore so we Begin it again
}

Related

How to make a screen share application

I am trying to make a simple screen share application in C# and found this guide: https://www.c-sharpcorner.com/uploadfile/ulricht/how-to-create-a-simple-screen-sharing-application-in-C-Sharp/ and followed it but it doesn't work i tried it on the same computer and on two different PCs but nothing seems to work
//Host
public partial class ScreenShareHandler : Form
{
RDPSession x = new RDPSession();
public ScreenShareHandler()
{
InitializeComponent();
}
private void ScreenShareHandler_Load(object sender, EventArgs e)
{
}
private void Incoming(object Guest)
{
IRDPSRAPIAttendee MyGuest = (IRDPSRAPIAttendee)Guest;//???
MyGuest.ControlLevel = CTRL_LEVEL.CTRL_LEVEL_INTERACTIVE;
}
private void button1_Click(object sender, EventArgs e)
{
x.OnAttendeeConnected += Incoming;
x.Open();
}
private void button2_Click(object sender, EventArgs e)
{
IRDPSRAPIInvitation Invitation = x.Invitations.CreateInvitation("Trial", "MyGroup", "", 10);
textBox1.Text = Invitation.ConnectionString;
}
private void button3_Click(object sender, EventArgs e)
{
x.Close();
x = null;
}
}
//Client
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
string Invitation = textBox1.Text;// "";// Interaction.InputBox("Insert Invitation ConnectionString", "Attention");
axRDPViewer1.Connect(Invitation, "User1", "");
}
private void button2_Click(object sender, EventArgs e)
{
axRDPViewer1.Disconnect();
}
}
As written in my comments:
Have you hooked up the eventhandlers correctly? If you click on the button in the designer you can go to the Events Tab in the Property-window and check if the Click-event points to the right eventhandler. Another way to check if the correct handler is used is to put a breakpoint inside each handler. Then debug and check if you get into the right method when you click the button. If not you didn't hook up the Eventhandlers correctly.

Xamarin - Morse code app usuing the flaslight

I am trying to make a morse code for a college project, what I'm trying to do is use a 2 dimensional array to save the morse code people input to a text file and then be able to load it from the text file, my logic was to was that within the array was this array[morse name][morse input]. what I need to figure out first is how to send data from methods / buttons OBtn_Clicked , LBtn_Clicked, SBtn_Clicked and EndBtn_Clicked to NewMorseBtn_Clicked to add into the array which will then write it out to a text file I've created.
namespace FlashLightApp2018
{
[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class MorsePage : ContentPage
{
//bool exitLoop = false;
public MorsePage()
{
InitializeComponent();
}
private async void NewMorseBtn_Clicked(object sender, EventArgs e)
{
bool isTextEmpty = String.IsNullOrEmpty(MorseName.Text);
if (isTextEmpty)
{
}
else
{
OBtn.IsEnabled = true;
LBtn.IsEnabled = true;
SBtn.IsEnabled = true;
EndBtn.IsEnabled = true;
// String morseName = MorseName.Text;
//String[,] morseSave = new String[100,100];
}
//File.WriteAllText(morseName, text);
//while (exitLoop != true)
//{
//}
}
private void LoadMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void PlayMorseBtn_Clicked(object sender, EventArgs e)
{
}
private void OBtn_Clicked(object sender, EventArgs e)
{
}
private void LBtn_Clicked(object sender, EventArgs e)
{
}
private void SBtn_Clicked(object sender, EventArgs e)
{
}
private void EndBtn_Clicked(object sender, EventArgs e)
{
}
}
}
first, declare you data at the class level (outside of a single method) so that it is accessible from throughout your class
string morseData = string.Empty;
then have your different button methods update the data
private void OBtn_Clicked(object sender, EventArgs e)
{
morseData += ".";
}
private void LBtn_Clicked(object sender, EventArgs e)
{
moreseData += "-";
}

Add items from a listbox to another listbox on another form

Here is my code.
private void PlaceOrder_Click(object sender, EventArgs e)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
I want to add what is in the menu box to a another list box on another form.
Update
(added by jp2code)
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
OtherForm.OtherListbox.Items.Clear();
foreach(var itm in MenuBox.Items)
OtherForm.OtherListbox.Items.Add(itm);
Form1 (Main):
namespace WindowsFormsApplication1 {
public partial class RESTAURANT : Form
{
double soup = 2.49;
double ordertotal;
public RESTAURANT()
{
InitializeComponent();
}
private void RESTAURANT_Load(object sender, EventArgs e)
{
}
private void Add_Click(object sender, EventArgs e)
{
MenuBox.Items.Add("Soup");
TotalBox.Items.Add(String.Format("{0:C}", soup));
ordertotal += soup;
total.Text = Convert.ToString(String.Format("{0:C}", ordertotal));
}
private void TotalBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void PlaceOrder_Click(object sender, EventArgs e)
{
new AreYouSure().Show();
this.Show();
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Clear();
ordertotal = 0;
}
}
}
Form2 (Confirmation)
namespace WindowsFormsApplication1 {
public partial class Confirmation : Form
{
public Confirmation()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void Confirmation_Load(object sender, EventArgs e)
{
}
private void MenuBox_SelectedIndexChanged(object sender, EventArgs e)
{
}
}
}
When clicking the 'Send Order' button the items from 'MenuBox' in form 1 need to be sent to the 'MenuBox' in form 2
It is better that the controls on your other form (ListBox, in this case) are set to Private by default.
In that case, you would either need to set the control's visibility to Public (bad form, in my opinion) or create a method in your other form to accept the parameters from your form.
Consider something like this:
public void ListBoxData(object[] array)
{
listBox1.Clear();
listBox1.AddRange(array);
}
To get the data or selected item information back to your main form, you would likewise create another public object that you could check, like the property below:
public object SelectedItem { get { return listBox1.SelectedItem; } }
I hope that is what you were looking for.
UPDATE:
Using the code you supplied in the post below, I can see you do not have anything in your Confirmation form to send data to, much less a way to pass that information.
If you had a ComboBox, you could do something like follows:
public partial class Confirmation : Form
{
private ComboBox comboBox1;
public void AddRange(object[] array)
{
comboBox1.Items.AddRange(array);
}
}
That does not place the ComboBox anywhere on your form. You would need to work that out.
With that done, I'm guessing you need to edit your PlaceOrder_Click routine:
private void PlaceOrder_Click(object sender, EventArgs e)
{
//new AreYouSure().Show();
//this.Show();
using (var obj = new Confirmation())
{
var list = new List<object>(MenuBox.Items.Count);
foreach (var o in MenuBox.Items)
{
list.Add(o);
}
obj.AddRange(list.ToArray());
if (obj.ShowDialog(this) == DialogResult.OK)
{
MenuBox.Items.Clear();
TotalBox.Items.Clear();
total.Items.Clear();
ordertotal = 0;
}
}
}
If you are struggling with this, you might need to look into some C# Windows "multiple forms" tutorials.
Here is a YouTube (that I did not sit all the way through): https://www.youtube.com/watch?v=qVVtCPDu9ZU

PetaPoco in WinForms

How to commit changes done in DataGridView using PetaPoco ? Something like :
namespace PetaPocoTest
{
public partial class Form1 : Form
{
PetaPoco.Database db = new PetaPoco.Database("PgConnection");
IEnumerable<customers> allCustomers;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
allCustomers = db.Query<customers>("SELECT * FROM customers");
mGrid.DataSource = allCustomers .ToList();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
db.Save("customers", "custumer_id", allCustomers);
}
}
}
Try my answer Here
This is the simplest and easiest and can do what you want so far
I finally got it. I just wasted hours on something so obvious :\
namespace PetaPocoTest
{
public partial class Form1 : Form
{
PetaPoco.Database db = new PetaPoco.Database("PgConnection");
IEnumerable<customers> allCustomers;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
allCustomers = db.Query<customers>("SELECT * FROM customers");
mGrid.DataSource = allCustomers .ToList();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
foreach (var a in allCustomers)
{
db.Save("customers", "custumer_id", a);
}
}
}
}

Adding data to a gridview in c# from multiple forms

How would I go about inserting the text that I have entered in to the textbox in NewActivity into the first column in the datagridview on form1?
Here is the coding I have thus far.
Form1
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
this.IsMdiContainer = true;
}
private void viewToolStripMenuItem1_Click(object sender, EventArgs e)
{
}
private void newActivityToolStripMenuItem_Click(object sender, EventArgs e)
{
NewActivity NewAc = new NewActivity();
NewAc.MdiParent = this;
NewAc.Show();
}
private void deleteActivityToolStripMenuItem_Click(object sender, EventArgs e)
{
}
}
}
NewActivity
public partial class NewActivity : Form
{
public string activityName;
public NewActivity()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
activityName = "";
this.Close();
}
private void btnAddActivity_Click(object sender, EventArgs e)
{
activityName = txtActivityName.Text;
this.Close();
}
}
}
you can insert it in your event click
private void btnAddActivity_Click(object sender, EventArgs e)
{
activityName = txtActivityName.Text;
int index = yourDataGridView.Rows.Add();
DataGridViewRow row = yourDataGridView.Rows[index];
row.Cells[0].Value = activityName ;
this.Close();
}
Here is an example of how to bind data from a textbox control to a DataGrid
// create new row
DataGridViewRow row = new DataGridViewRow();
// create cells
row.CreateCells(this.dataGridView1, textBox1.Text, textBox2.Text, textBox3.Text);
// add to data grid view
this.dataGridView1.Rows.Add(row);
---------------Below is how you would use it in your case--------------------
private void btnAddActivity_Click(object sender, EventArgs e)
{
activityName = txtActivityName.Text;
int index = dgvActivityList .Rows.Add();
DataGridViewRow row = dgvActivityList .Rows[index];
row.Cells[0].Value = activityName;
this.Close();
}

Categories

Resources