C# -Last edited Textbox not being saved to database - c#

I have a C# program that is used for data entry and retrieval from a SQL Database. Generally it works very well however there is a saving error I am trying to get to the bottom of. It happens under this specific circumstance.
I update data in a bound textbox.
without leaving the textbox I then press the save button.
When I do this the data in the the bound textbox is not saved to the SQL Database.
This is the program
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.Entity;
namespace Labassist2017
{
public partial class Form1 : Form
{
//Declare entity objects
//____________________________________________________________________________________________
public Lab_Assistant_backendEntities Database_Backend;
public DbSet Sample_Register;
public DbSet Product;
//____________________________________________________________________________________________
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Load_Data();
}
public void Load_Data()
{
Database_Backend = new Lab_Assistant_backendEntities();
Sample_Register = Database_Backend.Sample_Register;
Sample_Register.Load();
sample_RegisterBindingSource.DataSource = Sample_Register.Local;
Product = Database_Backend.Products;
Product.Load();
productBindingSource.DataSource = Product.Local;
}
private void Link_To_Form()
{
}
private void sample_RegisterBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
Save();
}
public void Save()
{
Database_Backend.SaveChanges();
}
}
}
the data is saved under the following circumstance.
I update data in a bound textbox.
I leave the textbox and then press the save button.
Clarification: when I say leave I mean that I press tab and exit the textbox so that it no longer has focus.
Why is this happening?

What I needed to do was call .EndEdit() in my save method.

Related

Go to certain website when item from combo box is selected

I am trying to create a combo box with multiple items. When an item is selected and the button is clicked it will take you to a website. Each item in the combo box leads to a different website. I have searched with no luck finding the results I need. Any help would be appreciated. My code this far is below.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Tools
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
tools.Items.Add("Movies");
tools.Items.Add("Music");
tools.Items.Add("Documents");
tools.Items.Add("Apps");
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
In your button click event evaluate the selected value and send to the desired site.
public Form1()
{
InitializeComponent();
tools.Items.Add("Movies");
tools.Items.Add("Music");
tools.Items.Add("Documents");
tools.Items.Add("Apps");
}
private void button1_Click(object sender, EventArgs e)
{
switch(tools.SelectedValue)
{
"Movies":
Response.Redirect("google.com");
break;
"Music":
Response.Redirect("google.com");
break;
"Documents":
Response.Redirect("google.com");
break;
"Apps":
Response.Redirect("google.com");
break;
}
}

C# Calling A Flash Variable From Another Form?

I am in need of a bit of help searched around the internet for about 2 days
to try and figure out how i can call an AxShockwaveFlash Variable from a second form this is for a game trainer something simple just getting into C# so i thought id start with one of these however i love everything i make to have a nice look and as many options as i can add so for my first C# Trainer i added a MenuStrip that opens a second form(VariableMods.cs) it has a few radio buttons a text box 3 toggle switches and 2 buttons one for setting a variable to whatever is typed in the text box while checking which radio button is checked and a button to close the variable menu which is another thing i need a bit of help with
-How do i Close the Second Form but still keep my Toggles On?
-How do i Call A Variable From (Form1.cs) to Form2(VariableMods.cs)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OCULAS
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Show the Variable ModMenu
private void openVariableModsToolStripMenuItem_Click(object sender, EventArgs e)
{
VariableMods VarMenu = new VariableMods(this);
VarMenu.Show();
}
//Show About Program Box
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
InfoBox1 AboutBoxx = new InfoBox1();
AboutBoxx.Show();
}
//Close the Program
private void closeToolStripMenuItem_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
That is my Form1 Code Have Not Put anything into it about the ShockwaveFlash yet
And
here is Form2(VariableMods)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace OCULAS
{
public partial class VariableMods : Form
{
Form1 originalForm; //Using "OriginalForm" To Call "Form1"
public VariableMods(Form1 IncomingForm)
{
originalForm = IncomingForm; //originalForm(Form1) is the form to be called
InitializeComponent();
BoxHeadVariables utf = new BoxHeadVariables();
}
private void ambiance_Button_21_Click(object sender, EventArgs e)
{
this.Hide();
}
private void ambiance_Button_11_Click(Form1 incomingForm, object sender, EventArgs e)
{
if (ambiance_RadioButton1.Checked == true)
{
MessageBox.Show("Damage Mod Is Active");
}
if (ambiance_RadioButton2.Checked == true)
{
MessageBox.Show("Speed Mod Is Active");
}
if (ambiance_RadioButton3.Checked == true)
{
MessageBox.Show("Range Mod Is Active");
}
}
}
}

Multiform DataGridView and DetailView Issues

I am trying to make a multiform application that takes a DataGridView of table attributes and shows them in a DetailView upon the click of a button. When I click the button and the second form opens it is empty. Then if I close the second form I get 'An unhandled exception of type 'System.NullReferenceException' occurred'. Here is my code:
Form 1
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MultiForm
{
public partial class Form1 : Form2
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'personnelDataSet.employee' table. You can move, or remove it, as needed.
this.employeeTableAdapter.Fill(this.personnelDataSet.employee);
}
private void btnExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnDetails_Click(object sender, EventArgs e)
{
Form2 dForm = new Form2();
dForm.ShowDialog();
this.tableAdapterManager.UpdateAll(this.personnelDataSet);
}
}
}
Form 2
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace MultiForm
{
public partial class Form2 : Form
{
//public Form2()
//{
// InitializeComponent();
//}
private void employeeBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.employeeBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.personnelDataSet);
}
private void Form2_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'personnelDataSet.employee' table. You can move, or remove it, as needed.
this.employeeTableAdapter.Fill(this.personnelDataSet.employee);
}
}
}
I get the exception on form 1 at this.tableAdapterManager.UpdateAll(this.personnelDataSet); Any help would be greatly appreciated.
Didn't drag in the data components into the tray duh... Sorry guys.

How to change user control label.text from a form

Right so I have a user control called "ModbusMaster" and a form with literally a single button on it..
When I click the button I want to change the text of a label on my control..
However nothing happens..
Here is the main form
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace ModbusMaster_2._0
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
ModbusMaster mb = new ModbusMaster();
public void button1_Click(object sender, EventArgs e)
{
mb.openPort("wooooo");
}
}
}
I am calling the method openPort and passing the string "wooo" to it..
here is my control
The text does not get updated :(:(:(
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Ports;
namespace ModbusMaster_2._0
{
public partial class ModbusMaster : UserControl
{
string portName = "COM1"; //default portname
int timeOut = 300; //default timeout for response
SerialPort sp = new SerialPort();
public ModbusMaster()
{
InitializeComponent();
}
protected override void OnPaint(PaintEventArgs e)
{
portLabel.Text = portName;
}
public void openPort(string port)
{
statusLabel.Text = port;
}
/*
* Properties
*/
public string SerialPort //Set portname
{
get { return portName; }
set { portName = value;}
}
public int TimeOut //Set response timeout
{
get { return timeOut; }
set { timeOut = value; }
}
}
}
I think you must have two instances of ModbusMaster.
One of them is the one you can see on the display, and is NOT being updated.
The other one is the one you create in class Form1 with the line of code:
ModbusMaster mb = new ModbusMaster();
That is the one you are modifying, but it isn't the displayed one (I cannot see anywhere that you can be displaying that).
What you need to do is use the reference to the actual displayed one instead when you call mb.openPort("wooooo");
[EDIT]
Thinking about it - it's possible that you haven't instantiated another user control at all.
Did you use Visual Studio's Form Designer to add the user control to your main form? I had assumed that you did, but now I realise that might not be the case.
If not, you should do that, give it the name mb and remove the line that says ModbusMaster mb = new ModbusMaster(); and it might work without you having to make more extensive changes.
You are creating your UserControl but not assigning it to your Form's Control Collection. Try something like this in your Constructor.
namespace ModbusMaster_2._0
{
public partial class Form1 : Form
{
ModbusMaster mb = new ModbusMaster();
public Form1()
{
InitializeComponent();
this.Controls.Add(mb); //Add your usercontrol to your forms control collection
}
public void button1_Click(object sender, EventArgs e)
{
mb.openPort("wooooo");
}
}
}

update datagridview

i have the following code, what want it to do is update when i add new records to my table from a separate form, at the moment i am getting all the table data, but if i add new data it is not visible here, but is saved in the table, i have to close the program and run it again for the saved data to be visible in the datagridview. my question is how do i update the datagridview, in order for the table info to be there at all times.
The code i have is as follows:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace project
{
public partial class frmViewBookings : Form
{
public frmViewBookings()
{
InitializeComponent();
}
private void btnClose_Click(object sender, EventArgs e)
{
Form3 mainpage = new Form3();
mainpage.Show();
this.Close();
}
private void frmViewBookings_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed.
this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);
}
}
}
Add/Edit your existing method in frmViewBookings:
private void frmViewBookings_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'usersDataSet1.Booking' table. You can move, or remove it, as needed.
UpdateData();
}
public void UpdateData()
{
this.bookingTableAdapter.Fill(this.usersDataSet1.Booking);
}
Then call UpdateData() from your other Form
In your other form, add a property that holds the first form:
public partial class frmBooking : Form
{
public frmViewBookings myBookingsFrm = null;
}
Then when you create the frmBooking form, do:
frmBooking frmNewBookingFrm = new frmBooking();
frmNewBookingFrm.myBookingsFrm = this;//or whatever reference to the first frmViewBookings is
So after you insert the new record inside frmBooking, call:
myBookingsFrm.UpdateData();

Categories

Resources