I am creating a simple staff directory program, and I am making it so the user can update the directory. However, at current time when adding data into the DataGridView, it does not save to the database. I understand that this.staffTableAdapter.Update(this.dBDataSet1.Staff); is needed, which is in my program as you will see below.
I'm just not sure what I am missing at the moment, and would be greatful if anyone could help me out.
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 StaffDirectory
{
public partial class Administration : Form
{
public Administration()
{
InitializeComponent();
}
private void staffBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.staffBindingSource.EndEdit();
this.staffTableAdapter.Update(this.dBDataSet1.Staff);
}
private void Administration_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'dBDataSet1.Staff' table. You can move, or remove it, as needed.
this.staffTableAdapter.Fill(this.dBDataSet1.Staff);
}
}
}
You should add try-catch block and analyze/provide us with the exception description (ex.Message):
private void staffBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
try{
this.Validate();
this.staffBindingSource.EndEdit();
this.staffTableAdapter.Update(this.dBDataSet1.Staff);
}
catch(Exception ex) {}
}
Also, add this.dBDataSet1.Staff.AcceptChanges(); (re: https://msdn.microsoft.com/en-us/library/ceab2k93.aspx)
Hope this may help. Best regards,
Related
I am trying to make a room request program and have two windows that I need to connect to the database (which I know how to do). The two windows are called EMERGENCY and JANITOR. My issue is I don't know how to store information in a database. Like let's say someone logs in as EMERGENCY and sends a request, the information needs to be stored and once the JANITOR logs in, he can see the requests. So far I have the database but not sure about storing information in it without manually adding them. If anyone has any guides or links on how to do this, I'd appreciate it!
EMERGENCY window code:
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.SqlClient;
namespace LOGINPAGE
{
public partial class EMERGENCY : Form
{
public EMERGENCY()
{
InitializeComponent();
}
void Fillcombo()
{
}
private void EMERGENCY_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'roomInfoDataSet6.Table' table. You can move, or remove it, as needed.
}
private void xButton1_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
private void xButton2_Click(object sender, EventArgs e)
{
}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}
private void Search_Click(object sender, EventArgs e)
{
}
}
}
JANITOR code:
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 LOGINPAGE
{
public partial class JANITOR : Form
{
public JANITOR()
{
InitializeComponent();
}
private void label1_Click(object sender, EventArgs e)
{
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
}
private void JANITOR_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'roomInfoDataSet3.Table' table. You can move, or remove it, as needed.
this.tableTableAdapter.Fill(this.roomInfoDataSet3.Table);
}
private void xButton4_Click(object sender, EventArgs e)
{
}
private void xButton2_Click(object sender, EventArgs e)
{
this.Close();
Application.Exit();
}
}
}
EMERGENCY
JANITOR
You can use Ms access db
You will find connecting to acess from c# to really easy with lots of YouTube video tutorials
I can have a table in it database that is updated by a "save emergency" button and a load emergency button by the janitor will just populate a datagridview with items from the same table filled with the first button
I have 2 order tables(master/detail). 1 is the order header, another one is in detail. 2 tables are linked (master.key_seq = detail.link_key_seq).
I just dragged this two table to a form as datagridviews. This form is working properly with update/insert/delete from sql server.
How can I implement a unique key number when I press the "add" button from the bindingNavigator ?
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.SqlClient;
namespace TestConnect
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void mPO_MASTERBindingNavigatorSaveItem_Click(object sender, EventArgs e)
{
this.Validate();
this.mPO_MASTERBindingSource.EndEdit();
this.tableAdapterManager.UpdateAll(this.vGB_PurchaseDataSet);
}
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'vGB_PurchaseDataSet.MPO_DETAIL' table. You can move, or remove it, as needed.
this.mPO_DETAILTableAdapter.Fill(this.vGB_PurchaseDataSet.MPO_DETAIL);
// TODO: This line of code loads data into the 'vGB_PurchaseDataSet.MPO_MASTER' table. You can move, or remove it, as needed.
this.mPO_MASTERTableAdapter.Fill(this.vGB_PurchaseDataSet.MPO_MASTER);
}
private void DeleteBtn_Click(object sender, EventArgs e)
{
foreach (DataGridViewRow item in this.mPO_DETAILDataGridView.SelectedRows)
{
mPO_DETAILDataGridView.Rows.RemoveAt(item.Index);
}
}
}
}
I have this error and i don't know how to fix it, i can't find whats wrong with my code so i ask help to the ppl that are reading this and know the solution.
The error is at the button voids(exept the start button void). tried to switch between the URLtexbox and the Combobox but somewhere i screwed up, i've tried a lot to fix it but i couldn't find out how to fix it.
Here is my code:
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.Web;
namespace ****
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
webBrowser1.ScriptErrorsSuppressed = true;
}
private void startbutton_Click(object sender, EventArgs e)
{
switch (Text)
{
case "full":
{
webBrowser1.Navigate(URLtextbox.Text);
}
break;
case "empety":
{
webBrowser1.Navigate(comboBox1.Text);
}
break;
}
}
}
private void stopbutton_Click(object sender, EventArgs e)
{
webBrowser1.Stop();
}
private void forwardbutton_Click(object sender, EventArgs e)
{
webBrowser1.GoForward();
}
private void backbutton_Click(object sender, EventArgs e)
{
webBrowser1.GoBack();
}
}
Thanks for helping ^.^
The 3 methods (stopbutton_Click, forwardbutton_Click and backbutton_Click) must be defined inside a class.
Or in other words: Move the closing brace of the class below the last method.
This is code for windows forms aplication:
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 WindowsFormsApplication6 {
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
}
}
}
How to create a file, file.txt, when you click on the button?
You have many methods you can use on File, like File.Create. This simply creates or overwrites a file in the local running folder:
private void button1_Click(object sender, EventArgs e)
{
if (!File.Exists("file.txt"))
{
File.Create("file.txt");
}
}
Use File.WriteAllText Method which creates a new file, write the contents to the file, and then closes the file. If the target file already exists, it is overwritten.
private void button1_Click(object sender, EventArgs e)
{
File.WriteAllText("file.txt","your text ");
}
My TextBox won't update! I am using it as a Log to update what other things are doing...
Form 1 code:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Collections.Specialized;
using System.Text;
delegate void logAdd(string message);
namespace LCR_ShepherdStaffupdater_1._0
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
public void logAdd(string message)
{
this.Log.Items.Add(message); // Doesnt add to textbox
this.Log.Items.Add("This won't update the textbox!!! Why?"); // Doesnt add to textbox
}
private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form aboutBox = new AboutBox1();
aboutBox.ShowDialog();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
settingsForm.settings.ShowDialog();
}
private void synchronize_Click(object sender, EventArgs e)
{
this.Log.Items.Add("This WILL update the textbox. It is exactly the same as the other one..."); // This will add to the textbox
DatabaseHandling.createDataSet();
}
}
public class settingsForm
{
public static Form settings = new Settings();
}
}
My Logging Class code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LCR_ShepherdStaffupdater_1._0
{
public class Logging
{
static Main mainClass = new Main();
static logAdd logAddDelegate;
public static void updateLog(string message)
{
logAddDelegate = mainClass.logAdd;
logAddDelegate(message);
}
}
}
I am just completely stumped....does anyone know how I can fix the logAdd function so I can get my textbox log to finally update?
EDIT:
I finally got it to work, here was the fix:
Form1 Code
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Data.OleDb;
using System.Collections.Specialized;
using System.Text;
delegate void logAdd(string message);
namespace LCR_ShepherdStaffupdater_1._0
{
public partial class Main : Form
{
public Main()
{
InitializeComponent();
}
public void add(string message)
{
this.Log.Items.Add(message);
}
public void logAdd(string message)
{
Log.Invoke(new logAdd(add), new object[] { message });
}
private void exitProgramToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void aboutToolStripMenuItem1_Click(object sender, EventArgs e)
{
Form aboutBox = new AboutBox1();
aboutBox.ShowDialog();
}
private void settingsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
private void settingsToolStripMenuItem1_Click(object sender, EventArgs e)
{
settingsForm.settings.ShowDialog();
}
private void synchronize_Click(object sender, EventArgs e)
{
logAdd("this works");
}
}
public class settingsForm
{
public static Form settings = new Settings();
}
}
Logging Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LCR_ShepherdStaffupdater_1._0
{
public class Logging
{
static Main mainClass = new Main();
static logAdd logAddDelegate;
public static void updateLog(string message)
{
logAddDelegate = mainClass.logAdd;
logAddDelegate(message);
}
}
}
Well...I can tell that is a really bad way to do it...but until I find a true fix as long as it works im happy. Thanks for the help guys!
From the code you posted your static Logger class is creating a new instance of the Main form. which is different than the one your running on the UI.
Overall this isn't a very good design pattern. write a comment if you want me to elaborate and give a sample.
What thread are you calling Logging.updateLog() from? If it's not the UI thread, I could see that the TextBox wouldn't get updated in that case. If you try calling logAdd() from synchronize_Click() (assuming that this is called from the UI thread based on the name), does it work then?
the synchronize_click is run on the instance of the main form that is running. The logadd function is run on a seperate main form that you create in the logging code. For this to work, you need to pass your existing main form into the log code so that it updates the text box you can see, not the extra text box you don't.
I had a problem similar once before, it was solved by calling .Invalidate() on the text box after writing to it.
You could also check your message is actually getting to the textbox with:
debug.WriteLine( this.Log.Items( this.Log.Items.Count-1));
Hope that helps you.