I have a combobox and want to save the changes in the database.
What I want to do is if the combobox is selected and it is true it must run the code below. If it is false it must then skip the code and go further.
The code below is checking if the combobox is enabled. But when I'm compiling it's saying true when not selected
private void Log()
{
if (kaartburgerlijkestand.Enabled)
{
veranderingBurgelijkestaat();
}
}
The code below is saving the data in the database
private string veranderingBurgerlijkestaat()
{
string gebruiker = curMedewerker.Behandelnaam;
string bekeken = prodermaform.pKaart();
string tabblad = tabControl1.SelectedTab.Text;
string pat = CPatient.GeefPatientNaam(patient.Id);
string wijz = "Burgerlijkestaat: " + kaartBurgerlijkestand.Text;
CDb dcon = new CDb();
try
{
if (dcon.Open())
{
SqlCommand cmd = new SqlCommand("INSERT INTO dbo.loggen(Gebruiker, Bekeken, Tabblad, Patientnaam, Wijziging, Datum) VALUES(#gebruiker, #bekeken, #tabblad, #pat, #wijz, #datum)", dcon.Conn);
cmd.Parameters.AddWithValue("#gebruiker", gebruiker);
cmd.Parameters.AddWithValue("#bekeken", bekeken);
cmd.Parameters.AddWithValue("#tabblad", tabblad);
cmd.Parameters.AddWithValue("#pat", pat);
cmd.Parameters.AddWithValue("#wijz", wijz);
cmd.Parameters.AddWithValue("#datum", DateTime.Now);
cmd.ExecuteNonQuery();
cmd.Dispose();
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
dcon.Close();
}
return wijz;
}
Could someone show me a example how to do it
I Found the solution
I have made a check
private void doCheck(object sender, EventArgs e)
{
cmbox = false;
if (kaartBurgerlijkestaat.Focused)
{
veranderingBurgerlijkestand();
}
cmbox = true;
}
Then I used the SelectedValueChanged Event
private void kaartBurgerlijkestand_SelectedValueChanged(object sender, EventArgs e)
{
if (cmbox)
doCheck(sender, e);
}
And it works fine.
I want to thank you all for helping me!
Add an Event Listener, the kaartburgerlijkestand.Enabled only check your combobox is enabled to select or not.
Add this line after your InitializeComponent(); line of Form.cs file
kaartburgerlijkestand.SelectedIndexChanged += new System.EventHandler
(this.kaartburgerlijkestand_SelectedIndexChanged);
Also add a function for above code :
private void kaartburgerlijkestand_SelectedIndexChanged(object sender, EventArgs e)
{
veranderingBurgelijkestand();
}
Related
I have a project where when user login wrong for 3 times in login form, the webcam will start capturing automatically. It successfully saves the image in the database but there is one error which is Object Reference not set to an instance of an object. I know that there are a lot of questions and answers to this but I still can't solve it. Another problem is, when I just debug from the webcam form, it runs perfectly without the error message but when I debug from login form, the error appears. Another thing is it doesn't show the line of the error but I am sure that it comes somewhere from here:
private void webcam_Load(object sender, EventArgs e)
{
if ((imagecapture == null))
{
try
{
imagecapture = new Emgu.CV.VideoCapture();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Application.Idle += new EventHandler(loadimage);
}
and this is the loadimage()
public void loadimage(object sender, EventArgs e)
{
try
{
Emgu.CV.Mat imageviewer = this.imagecapture.QueryFrame();
pictureBox1.Image = imageviewer.Bitmap;
MemoryStream ms = new MemoryStream();
pictureBox1.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] picture = ms.ToArray();
connection.Open();
DateTime dtime = DateTime.Now;
string qry = "INSERT INTO WebcamPhoto (ImageData,DateAndTime) VALUES(#ImageData,#dt)";
//Initialize SqlCommand object for insert.
SqlCommand cmd = new SqlCommand(qry, connection);
cmd.Parameters.AddWithValue("#ImageData", picture);
cmd.Parameters.AddWithValue("#dt", dtime);
cmd.ExecuteNonQuery();
connection.Close();
imagecapture.Dispose();
//Close form and return to list or images.
this.Close();
}
catch (NullReferenceException ex)
{
MessageBox.Show(ex.Message);
}
}
I have tried to put return and the error message is gone but the image is not saved in the database even though the webcam is working.
private void webcam_Load(object sender, EventArgs e)
{
if ((imagecapture == null))
{
try
{
imagecapture = new Emgu.CV.VideoCapture();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
return;
}
Application.Idle += new EventHandler(loadimage);
}
Okay after few days finaly I found the answer myself. How? I try to change the way I wrote the code line by line and test it everytime I change the code and finally I found the culprit. So the culprit is:
public void loadimage(object sender, EventArgs e)
{
//my other code.....
imagecapture.Dispose(); <<<----here
}
So I change it to
private void ReleaseData()
{
if (imagecapture !=null)
{
imagecapture.Dispose();
}
}
and now the problem is solved :)
I am trying to make a simple product code search using a datagridview.
I am able to filter the database but not able to get all the functions I want
I currently have it set as
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
productsBindingSource.Filter = string.Format("Type = '{0}'",
comboBox1.SelectedItem.ToString());
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
productsBindingSource.Filter = string.Format("Type = '{0}' AND Fitting = '{1}'",
comboBox1.SelectedItem.ToString(),
comboBox2.SelectedItem.ToString());
}
This code works but after the selections are made and I change comboBox1 the data resets and doesn't keep the selection of comboBox2.
I understand in my current code that this would not happen but I cannot figure out how to get this to happen.
I would also like to add a text box in the future and have it narrow the filter even more.
You should approach this a bit more generically like so
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
FilterProducts();
}
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
FilterProducts();
}
// Create a function to handle filters
private void FilterProducts()
{
string filter = "";
if (comboBox1.SelectedItem != null)
{
filter += string.Format("Type = '{0}'", comboBox1.SelectedItem.ToString());
}
if (comboBox2.SelectedItem != null)
{
if (filter.length > 0) filter += "AND "
filter += string.Format("Fitting = '{0}'", comboBox2.SelectedItem.ToString());
}
// Add another like above for your future textbox
// if (!string.IsNullOrEmpty(textBox1.Text))
// {
// if (filter.length > 0) filter += "AND "
// filter += string.Format("OtherColumn = '{0}'", textBox1.Text);
// }
productsBindingSource.Filter = filter;
}
The code could be further refactored for even better DRY standards but this should at least get you started.
So, I have a simple ListView that users can add information to and a delete button that is only capable of deleting one selected item at a time. I'm trying to make it so that when multiple items are selected and 'delete' is pressed it deletes those selected items instead of just one. Your help is appreciated!
Add recipient code:
private void addtoRecipients_Click(object sender, EventArgs e)
{
if (recipientEmailBox.Text != "")
{
string[] S = new string[4];
S[0] = recipientEmailBox.Text;
S[1] = recipientNameBox.Text;
S[2] = txtLocation.Text;
S[3] = txtSubject.Text;
ListViewItem I = new ListViewItem(S);
recipientBox.Items.Add(I);
UpdateNoOfEmails();
}
}
My Delete Button Code (only deletes one selection at the moment)
private void deleteEntryBTN_Click(object sender, EventArgs e)
{
try { recipientBox.Items.Remove(recipientBox.SelectedItems[0]); }
catch { }
UpdateNoOfEmails();
}
Clear All Recipients Code
private void clearBTN_Click(object sender, EventArgs e)
{
recipientBox.Items.Clear();
UpdateNoOfEmails();
}
In my case, the simplest way to do it was with a while loop. This is what my new delete button code looks like:
private void deleteEntryBTN_Click(object sender, EventArgs e)
{
try
{
while (recipientBox.SelectedItems.Count > 0)
{
recipientBox.Items.Remove(recipientBox.SelectedItems[0]);
}
}
catch { }
UpdateNoOfEmails();
}
This is a simple question, but i donĀ“t know where is the problem. I have a form that have some code, when i click into "crear" button, in that button i call to the function "Comprobar" that cheks if the textboxs are empty, if "Comprobar" is false then i show a message.
The problem: After clicking the button "Crear" the form show the message (if all are empty) and then the form close.
Here is the code
public partial class FrmNuevaCita : MetroForm
{
DataTools mytool = new DataTools();
DataSet ds = new DataSet();
BindingSource bs = new BindingSource();
// string searchDate = "";
int codigoPaciente = -1;
FrmBuscarPaciente BuscarPaciente = new FrmBuscarPaciente();
public FrmNuevaCita()
{
InitializeComponent();
BuscarPaciente.SetCode += new EventHandler(YouCliked);
}
private void YouCliked(object sender, EventArgs e)
{
codigoPaciente = BuscarPaciente.GetCodigoPaciente;
//MessageBox.Show("Codigito es " + codigoPacienteActual.ToString());
txtPaciente.Text = codigoPaciente.ToString();
}
public DateTime SetDate
{
set { dtpFechaCita.Value = value; }
}
private void mbCancelar_Click(object sender, EventArgs e)
{
this.Close();
}
private void metroRadioButton_CheckedChanged(object sender, EventArgs e)
{
this.cmbHora.Items.Clear();
if (metroRadioButton1.Checked == true)
{
this.cmbHora.Items.AddRange(new object[]
{"8:00","8:30","9:00","9:30","10:00","10:30","11:00","11:30"});
}
else if (metroRadioButton2.Checked == true)
{
this.cmbHora.Items.AddRange(new object[] { "12:00", "12:30", "13:00", "13:30", "14:00", "14:30", "15:00", "15:30","16:00","16:30","17:00","17:30","18:00","18:30","19:00" });
}
}
private void mtBuscar_Click(object sender, EventArgs e)
{
BuscarPaciente.Show();
}
private void mbCrear_Click(object sender, EventArgs e)
{
if (Comprobar()==false)
MessageBox.Show("Por favor complete todos los campos");
}
private bool Comprobar()
{
bool result = false;
if (txtPaciente.Text.Trim().Length != 0 && txtObservaciones.Text.Trim().Length != 0 && cmbHora.Text.Trim().Length != 0)
result = true;
return result;
}
}
Make sure, that "Clear" button has DialogResult property set to DialogResult.None.
And that form's CancelButton property is not set to "Clear" button.
have you debug it step-by-step?
Possible Problems:
The "CancelButton"-Option of your form is set to the "mbCrear_Click"-Button.
There might be some missformed linking of the events in your Form's Designerfile.
"mbCancelar_Click"-Event & "mbCrear_Click"-Event refers to the same button
Best thing would be, to set a breakpoint to "mbCrear_Click" and debug it step by step then you see where your form is closed. ;)
Hope i could help
Having a lot of trouble with this. I'm working on a large project, so there's only a few classes I'm interested in and working on. Basically, these are forms - one is a main editor where a user edits details and the other is used to assign a pin number. In the main editor form, if the user has a pin, they can choose to edit this pin. Here's where my problem lies - if I edit the pin, what I'm doing in the code is deleting the old pin and adding the new one. However, the database doesn't update until AFTER the editor form is closed. Therefore, I'd like to call the method that does change the database on the OKButton click, if I could. The problem I'm facing is I don't know how.
Here is the DB code, we'll say the class is called DetailsConn:
public string editPin(int driverID)
{
if (SchemaChecker.PINAvailable())
{
string sql = "EditPIN";
using (SqlCommand cmd = new SqlCommand(sql, base.connection))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Remove("#nDriverID");
cmd.Parameters.AddWithValue("#nDriverID", driverID);
cmd.Parameters.Remove("#nPIN");
SqlParameter pinParameter = cmd.Parameters.Add("#nPIN", SqlDbType.Char);
pinParameter.Direction = ParameterDirection.Output;
pinParameter.Size = 32;
cmd.ExecuteNonQuery();
return pinParameter.Value.ToString();
}
}
return "";
}
Here's the code for my edit:
private void editToolStripMenuItem_Click(object sender, EventArgs e)
{
if (this.listViewDriverTags.SelectedItems.Count > 0)
{
ListViewItem lvi = this.listViewDriverTags.SelectedItems[0];
DriverTag driverTag = lvi.Tag as DriverTag;
else if (blahTag.blahType == 2)
{
buttonAssignPIN_Click(sender, e);
}
//message stuff and dialog boxes with localization info
if (dr == DialogResult.Yes)
{
this.listViewDriverTags.Items.Remove(lvi);
if (Tag.id != -1)
{
TagsToBeDeleted.Add(driverTag);
}
}
if (dr == DialogResult.No)
{
this.listViewTags.Items.Clear();
this.listViewTags.Items.Add(lvi);
}
}
}
Here's my buttonAssignPIN stuff:
private void buttonAssignPIN_Click(object sender, EventArgs e)
{
using (AssignPINForm form = new AssignPINForm())
{
if (form.ShowDialog(this) == DialogResult.OK)
{
DriverTag PIN = DriverTag.GetNewPIN(form.DriverTag);
ListViewItem lvi = this.listViewTags.Items.Add(PIN.driverTag);
lvi.SubItems.Add(this.TagTypes[PIN.TagType]);
lvi.Tag = PIN;
}
}
}
And finally, here's my AssignPINForm code:
public partial class AssignPINForm : Form
{
public AssignPINForm()
{
InitializeComponent();
this.buttonOK.Click += new EventHandler(buttonOK_Click);
this.buttonCancel.Click += new EventHandler(buttonCancel_Click);
this.buttonOK.Enabled = false;
this.textBoxPin.TextChanged += delegate(object sender, EventArgs e)
{
String pattern = #"^[0-9]{4,20}$";
Regex regex = new Regex(pattern);
buttonOK.Enabled = regex.IsMatch(textBoxPin.Text);
};
LoadStrings();
}
public void LoadStrings()
{
//stome stuff
}
public string DriverTag
{
get { return this.textBoxPin.Text; }
set { this.textBoxPin.Text = value; }
}
private void buttonOK_Click(object sender, EventArgs e)
{
}
private void buttonCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void AssignPINForm_Load(object sender, EventArgs e)
{
}
}
I know it's kind of all over the place but I've provided everything I think is relevant. The middle two snippets are in the same class too, and the DB stuff is the same solution but a different project. I'd be grateful if someone can decipher what I'm after and help me out, it's the only thing I have left to do on this particular bit!
Thanks!
Not sure I fully got what you're after and I agree with some of the comments that this isn't the best of practice but I guess what you're after is to update the buttonOK_Click method to something like this:
private void buttonOK_Click(object sender, EventArgs e)
{
using(DetailsConn connection = new DetailsConn())
{
int driver = -1;
if(int.TryParse(this.DriverTag, out driver)) {
connection.editPin(driver);
}
}
}
Also, you may want to remove any other possible references to the editPin() function.
I actually figured out that even if I got that working correctly, it wasn't going to solve my problem. I've had to call a new procedure and declare that in the database schema - basically it was a lot more complicated than what I was giving it credit for. Thanks for the responses nonetheless.