Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Thursday, October 6, 2011 6:33 PM
Is there any way to retrieve a value from a text box that is hidden? Say I have a text box that is bound to a primary key value. Obviously this information is unnecessary for the end user so I hide the box. Lets say I want to update a record based on the value in the box. When I try to use the value in the hidden box, I dont get the expected results. I dont know how the hide function works. My guess is it actually removes the control from the form but there has to be some way around this.
All replies (14)
Thursday, October 6, 2011 7:35 PM âś…Answered | 1 vote
Also, try this. Don't put the visible property of your textbox to false in the property sheet, but instead do it in code when you load the form.
txtOfficerID.Visible = false;
Tom Overton
Thursday, October 6, 2011 6:37 PM
Welcome to MSDN Forums.
Making a control hidden does not have any effect in it's runtime behaviour. It just has effect in presentation. So behave it like a normal text box. i.e.
- Bind it to the code.
- Before calling the adapter.Update fill it manualy i.e. tb.Text = GenerateNewCode() when you do insert.
- You can access it's data by tb.Text.
Regards,
Yasser.
Thursday, October 6, 2011 6:39 PM
It shouldn't be affected by not being visible. This will demonstrate it:
private void button1_Click(object sender, EventArgs e)
{
textBox1.Visible = false;
textBox1.Text = "hello i'm still here";
MessageBox.Show(textBox1.Text);
}
The only way you would not be able to access it, is if it was removed from the Controls collection of the Form.
Tom Overton
Thursday, October 6, 2011 6:45 PM
This is weird because I just tested it and it does not work. I have a simple form where you can enter a badgenumber, first name, last name, and rank and submit it to a database. I also have a datagrid view which shows the table the officers are saved into. When you click on a row the binded text boxes fill in according to which officer you click in. This means my text box that holds the officer's unique primary key is also filled in correctly. When I try to update the information using my update button it does not work because my textbox is said to be null. it has a value of "" in it.
txtOfficerID = ""
I went into debug and checked my code line by line. The first line that initiates is
if (txtOfficerID != "")
{
//Query operations
}
else
MessageBox.Show("You must have a record selected in order to update it.");
When I go back, click on the textbox, and set its visible value to true then it all works and the textbox is not null........
private void btnUpdateOfficer_Click(object sender, EventArgs e)
{
Cursor.Current = Cursors.WaitCursor;
if (txtOfficerID.Text != "")
{
int intOfficerID = int.Parse(txtOfficerID.Text);
string strUpdateOfficer = "UPDATE tblOfficers SET BadgeNumber = ?, LastName = ?, FirstName = ?, Rank = ? Where OfficerID = ?";
OleDbCommand updateOfficer = new OleDbCommand(strUpdateOfficer, Launch);
updateOfficer.CommandType = CommandType.Text;
updateOfficer.Parameters.Add("@BadgeNumber", OleDbType.Char).Value = txtBadgeNumber.Text;
updateOfficer.Parameters.Add("@LastName", OleDbType.Char).Value = txtLastName.Text;
updateOfficer.Parameters.Add("@FirstName", OleDbType.Char).Value = txtFirstName.Text;
updateOfficer.Parameters.Add("@Rank", OleDbType.Char).Value = cmbRank.Text;
updateOfficer.Parameters.Add("@OfficerID", OleDbType.Integer).Value = intOfficerID;
updateOfficer.Connection = Launch;
Launch.Open();
try
{
updateOfficer.ExecuteNonQuery();
}
catch (OleDbException Hay)
{
MessageBox.Show(Hay.ToString(), "Error updating Officers Table");
AddOfficer AO = new AddOfficer((Juvenile_Database_Round_2.mdiMain)MdiParent);
vampDebugging.simpleDebug(Hay.ToString() + "2001", AO);
}
Launch.Close();
database6DataSet.tblOfficers.AcceptChanges();
SystemSounds.Beep.Play();
notifyIcon1.Icon = SystemIcons.Application;
notifyIcon1.BalloonTipTitle = "Record Saved";
notifyIcon1.BalloonTipText = "Your data has successfully been saved.";
notifyIcon1.ShowBalloonTip(1000);
database6DataSet.tblOffenses.AcceptChanges();
this.tblOfficersTableAdapter.Fill(this.database6DataSet.tblOfficers);
Cursor.Current = Cursors.Default;
}
else
{
MessageBox.Show("You must first add a record before updating it.", "Mistake?");
}
}
Thursday, October 6, 2011 6:53 PM
May I ask you putting "txtOfficerID.DataBindings["Text"].ReadValue();" before doing the update?
Thanks!
Thursday, October 6, 2011 6:57 PM
I am trying it right now but I have one question. What does string property mean? and int index? I'm not too sure what to put in the ["Text"] part of your code.
Thursday, October 6, 2011 6:59 PM
Do you have a variable named txtOfficerID and maybe you're getting mixed up with checking that, and not the text box?
Tom Overton
Thursday, October 6, 2011 7:02 PM | 2 votes
C# supports string indexing i.e. dataTable.Columns["OfficerID"] will return a column from dataTable which it's name is "OfficerID". txtOfficerID.DataBindings["Text"] means that binding of the text box which has been bound to the "Text" property of the text box because you can also bind another property of a text box e.g. txtOfficerID.DataBindings["Visible"]. So you don't need to made any change to my previous code for rewriting "Text".
Thursday, October 6, 2011 7:04 PM
Nope I do not unfortunately. I prefix everything so if I had an officerID of any other type it would be correctly prefixed. I just did the test that you suggessted on a new windows forms project and it worked; however, when I added that one line of code "messagebox.show(txtOfficerID.text)" it shows me an empty text box as long as the txtofficerID is hidden. If it is visible, then I get the correct value. It has to do something with binding a text box to data and then hiding it...
@ yasser I also tried your approach with "text" in the field like you suggested and still no go.. The text box has an empty string in it..
Thursday, October 6, 2011 7:23 PM
I think you're right, it's just a "bug" of sorts. Here is a past thread that discusses it.
http://social.msdn.microsoft.com/forums/en-US/vbgeneral/thread/48c6e228-84fa-4a1a-a67b-301afc542c19
I guess you would just have to reference the source dataset or table directly, or keep some variable to represent the textbox and mirror the value it should be.
Although really, "ReadValue" should go and retreive the data from the source, irrespective of the visibility of the control so not sure why you can't even get ReadValue to work.
Tom Overton
Thursday, October 6, 2011 7:24 PM | 1 vote
@ yasser I also tried your approach with "text" in the field like you suggested and still no go.. The text box has an empty string in it..
It's impossible! May I ask you try this:
void updateBtn_Click(..)
{
//before every thing
txtOfficerID.DataBindings["Text"].ReadValue();
MesseageBox.Show(txtOfficerID.Text);
//your rest code
}
Thursday, October 6, 2011 7:38 PM
Yasser! It still did not work!
@ Tom- blahahah it worked if I hid the text box using textbox.hide() on the loading event of my form! Thanks, good suggestion.
Thursday, October 6, 2011 7:48 PM
Very odd that it works that way, I'm sure there are developers with missing patches of hair due to this.
But yasser's suggestions were good, and you learned a little about how the binding process works at least from all this.
Tom Overton
Thursday, October 6, 2011 7:54 PM
Very odd that it works that way, I'm sure there are developers with missing patches of hair due to this.
But yasser's suggestions were good, and you learned a little about how the binding process works at least from all this.
Yes Tom, it's really odd! I'll research more and reproduce to found out if it's a bug or there is a something wrong with us.
Thanks Tom&Vamp for sharing these.