I have a Windows Form that was mainly made using the graphical editor. It is connected to a database called, Database1. One of the tables in the database is called Table1, and contains the column CheckBox1. When I connected the form to the database, Database1DataSet.xsd and Database1DataSet.Designer.cs were automatically created.
CheckBox1 can either hold "Yes" or blank (this wasn't my decision). I would like to make a checkbox checked if the value in the CheckBox1 column is "Yes", and unchecked if the value is blank. If I drag a bound checkbox onto the form, it doesn't work because I assume that the values in the column need to be either 1 or 0. So I'm trying to work around this.
In my form, I have the following
// Form Constructor
public myForm()
{
// Initializes all the components in the form
InitializeComponent();
// Change the checkboxes checked state
this.myCheckBox.Checked = myCheckBox_Update();
}
// Method for determining if the checkbox should be checked
private bool myCheckBox_Update()
{
// This SHOULD bring in the current record of the database
DataRowView current = (DataRowView)this.Table1BindingSource.Current;
try
{
// This SHOULD determine if the value in the CheckBox1 field has a value
return current.Row["CheckBox1"].ToString().Length > 0;
}
catch (NullReferenceException ex)
{
MessageBox.Show("NullReferenceException was thrown!", "Error");
return false;
}
}
In the InitializeComponent() function, there is the following
// This line is generated when I drag a bound checkbox to the form
// this.myCheckBox.DataBindings.Add(new System.Windows.Forms.Binding("CheckState", this.Table1BindingSource, "CheckBox1", true));
// For the miscellaneous information about the checkbox
this.myCheckBox.AutoSize = true;
this.myCheckBox.Location = new System.Drawing.Point(3, 3);
this.myCheckBox.Name = "myCheckBox";
this.myCheckBox.Size = new System.Drawing.Size(92, 23);
this.myCheckBox.TabIndex = 0;
this.myCheckBox.Text = "Check Box";
this.myCheckBox.UseVisualStyleBackColor = true;
However, it keeps throwing a NullReferenceException. I'm assuming that this is because this.Table1BindingSource.Current cannot be cast as a DataRowView.
I have looked at several other posts on SO that are somewhat related to this problem (e.g. This post, or this post), but I haven't found anything that has worked so far. The second link's answer doesn't work because I am iterating through the records with this.Table1BindingSource.MoveNext();, and I won't know the index.
Can someone steer me in the right direction? I'd really appreciate it
EDIT: The BindingSource is initialized with this
private System.Windows.Forms.BindingSource Table1BindingSource;
this.Table1BindingSource = new System.Windows.Forms.BindingSource(this.components);
I'll put this in an answer since it's not fit for a comment.
You may also try checking the value for null in your try statement.
try
{
// This SHOULD determine if the value in the CheckBox1 field has a value
object val = current.Row["CheckBox1"];
if (val == null)
{
return false;
}
else
{
return current.Row["CheckBox1"].ToString().Length > 0;
}
}
Or at least set a breakpoint at the if (val == null) statement and make sure val has a value that you would expect.
If by 'blank' you mean a String.Empty, then your code would work as you expect it to, but if blank means null, then you'll have to check for null, you can't call ToString() on a null value.
What I ended up doing is just changing the values in my database from "Yes" and blank to 1 and 0. Then in the InitializeComponent() method, I added the following line (or it might be generated for you if you drag it from the data source panel)
this.myCheckBox.DataBindings.Add(new System.Windows.Forms.Binding("CheckState", this.Table1BindingSource, "CheckBox1", true));
Related
My problem is that my List is not being retained during PostBack.
I am saving it into
Page.Session["mine"]
My design page consists of 2 controls( a Label and a CheckBoxList).
Because the number of items will change, I use a CheckBoxList instead of individual CheckBox controls and load each item dynamically in the page_load();
I am unsure of what it is called, either a member, attribute, or property. But I do have a variable that each method of the class are able to call on that holds the location of all selected items on the CheckBoxList.
Private List<int> locCheck = new List<int>();
When the OnSelectedIndexChanged() is triggered, the locCheck is renewed and stored in Session
locCheck = new List<int>();
int num = 0;
foreach(ListItem li in CheckBoxList.Items)
{
if(li.Selected)
locCheck.Add(num);
num++;
}
if(locCheck.Count == 0) //for testing purpose
locCheck.Add(1); //2nd item
Page.Session["mine"]=locCheck;
I want to keep the selected checks on PostBack, but it never happens. In Visual Studio I have no problem. But when I load to server, I am using mono, I get the following message:
Object reference not set to an instance of an object
I have this in page_load
if(Page.IsPostBack)
{
locCheck = new List<int>();
locCheck = (List<int>) Page.Session["mine"];
Label.Text = String.Format("Is PostBack {0}/{1}", locCheck.Count, CheckBoxList.Items.Count);
}
else
{
Label.Text = String.Format("Is Not PostBack 0/{0}", CheckBoxList.Items.Count);
}
I know the problem is with this line because locCheck.Count never changes in Label.Text
locCheck =(List<int>) Page.Session["mine"];
I know I have to attach
if(Page.Session["mine"] != null)
But I don't believe it is currently necessarry. Due to testing purposes, it should always have locCheck with at least one element.
i couldn't find an answer, so i started from scratch. instead of checklists i use panels of checkboxes that turn invisible when not needed.
I am still uncertain about Sessions but I have found something strange.
For example:
Session["mine"] = locCheck; //where lockCheck has 4 elements
there are times that locCheck is set to null sometime between page_load to a button click.
i have found that when this is so, debug back to a point where locCheck is not null and has correct element values. then perform following:
Session["mine"] = locCheck;
and later on, you perform the following code in click event
locCheck = new List<int>();
locCheck = (List<int>) Session["mine"];
//then make your update:
locCheck[3] = 7;
Session["mine"] = locCheck;
for some reason, if i don't do this both Session["mine"] and locCheck become null and data is lost.
try to use this
locCheck = (List<int>) Page.Session["mine"].ToString();
why to use page.session when you can use only session.
I have found some solution for this in the link below.
Disallow ListView to have zero selected items
My issue is when user clicks at the empty area in list view. the below line throws null exception.
string x = lvwIngred.SelectedItems[0].Name;
I want to use this below code before executing the above line.
sample-
if (lvwxyz.FocusedItem != null)
{
lvwxyz.FocusedItem.Selected = true;
}
string x = lvwIngred.SelectedItems[0].Name
It works fine if I use the above approach. I have set hideselection = true; multiselect = false;
First Question is - Will it work if hideselection property is set to true for all cases.
2nd Question - What will be the scenario when the lvwxyz.FocusedItem is null, and how can we fix that scenario.
I have also used lvwxyx.Items[0].Selected = true; This also fixes the issue.
Please help.
I have looked extensively to find an answer to this question but I only get extremely close. I have a web form that I use to add and edit records. When a record is selected in the gridview, a session variable is set and then used on page load to populate the text fields. If the session variable is not set, the form will be blank and the logic run as a new record. My problem is that I can add a new record successfully - I debugged and checked to make sure the asp controls passed the proper values to the code behind - but I cannot edit a record successfully. For some reason, the code behind file does not retrieve the proper values from the text boxes. Instead, it keeps the original populated values thus defeating the purpose of the edit. I imagine it is a binding issue but I am unsure and have searched upon end. Here is my code behind file:
protected void Page_Load(object sender, EventArgs e)
{
resultOutput.Visible = false;//Output results as to whether or not a record was added successfully is automatically hidden at page load
//Checking to see if session variable has been created
if (Session["editID"] != null)
{
//Create objects to get recipe data
dbCRUD db = new dbCRUD();
Recipe editRecipe = new Recipe();
//Grabbing session ID
var id = Convert.ToInt32(Session["editID"]);
//Call method to retrieve db data
editRecipe = db.SelectRecord(id);
//Populate results to text boxes
recordID.Text = editRecipe.Recipe_ID.ToString();
addName.Text = editRecipe.Name;
addTypeDDL.SelectedValue = editRecipe.Meal;
addDifficultyDDL.SelectedValue = editRecipe.Difficulty;
addCookTime.Text = editRecipe.Cook_Time.ToString();
addDirections.Text = editRecipe.Directions;
//Change Button Text
submitRecord.Text = "Edit Record";
//Change Title Text
addEditTitle.Text = "Edit Recipe";
}
}
protected void submitRecord_Click(object sender, EventArgs e)
{
Recipe recipe = new Recipe();
dbCRUD newRecord = new dbCRUD();
//Variables for execution results
var modified = "";
int returned = 0;
//Creating the recipe Object to pull the values from the form and
//send the recipe object as a parameter to the method containing insert stored procedure
//depending on Add or Edit
//recipe.Recipe_ID = int.Parse(recordID.Text);
recipe.Name = addName.Text.ToString();
recipe.Meal = addTypeDDL.SelectedValue.ToString();
recipe.Difficulty = addDifficultyDDL.SelectedValue.ToString();
recipe.Cook_Time = int.Parse(addCookTime.Text);
recipe.Directions = addDirections.Text.ToString();
//Checking to see if the page is loaded for edit or new addition
if (Session["editID"] != null)
{
recipe.Recipe_ID = Convert.ToInt32(Session["editID"]);
//If recordID exists, recipe will be passed to UpdateRecord method
returned = newRecord.UpdateRecord(recipe);
modified = "has been edited.";
Session.Remove("editID");
}
else
{
//If recordID does not exist, record will be passed to InsertRecord method (new recipe)
returned = newRecord.InsertRecord(recipe);
modified = "added";
}
//Method returns 0 if successful, 1 if sql error, 2 if other error
if (returned == 1)
{
resultOutput.Text = "There was an sql exception";
resultOutput.Visible = true;
}
else if (returned == 2)
{
resultOutput.Text = "There was a non sql exception";
resultOutput.Visible = true;
}
else
{
resultOutput.Text = "\"" + addName.Text + "\" recipe " + modified;
resultOutput.Visible = true;
}
}
Any object passed to my edit method is successful, however, as I mentioned, it does not grab the newly updated text box values.
Did you try checking PostBack property , Your code is loading the data everytime the page is posted back. So when you update the values in the form and hit update button. The Page_Load method is called first and it reloads all the data (replaces your updated values on the form) and then hit the update button event handler. So everytime your old values are being saved.
You may remove the code from page_load method and put it where you are setting the Session["EditId"] value. This will solve your problem.
I would suggest using a static dataset and bind it to the recordsource of the gridview control. Whenever you wanna edit a record update the dataset simultaneously and rebind it to the gridview control....hope that helps:)
I have a ListBox control populated with branches of a large retail chain. The staff using the system have to log in to the relevant branch, and I would like them to be able to search the ListBox to find their branch.
I have created an event handler for when text in the search box changes, and attempted to use code sound on StackOverflow already:
private int lastMatch = 0;
private void txtSearch_TextChanged(object sender, EventArgs e)
{
int x = 0;
string match = txtSearch.Text;
if (txtSearch.Text.Length != 0)
{
bool found = true;
while (found)
{
if (lbBranches.Items.Count == x)
{
lbBranches.SetSelected(lastMatch, true);
found = false;
}
else
{
lbBranches.SetSelected(x, true);
match = lbBranches.SelectedValue.ToString();
if (match.Contains(txtSearch.Text))
{
lastMatch = x;
found = false;
}
x++;
}
}
}
}
When I compile and start typing into the search box, I get this error:
Object reference not set to an instance of an object.
The line in question is:
match = lbBranches.SelectedValue.ToString();
I have no idea what could be wrong there, anyone got an idea?
Thanks!
SelectedValue of the listbox will only return a value if you have specified the ValueMember property of the listbox to indicate a property from which you would like to read the value for the selected item. The property you want to use in this case is SelectedItem:
match = lbBranches.SelectedItem.ToString();
when the user is entering text it's possible that no value has been selected (hence the error) -- keep in mind that what is being entered by the user has no mandatory or direct association with selections in the controls listbox sub-element
it's possible what you're doing might be simpler to implement with a full combo-box control and I think some of the examples at MSDN could be very helpful for you as well
I have recently switched over from Java/RMI to C# / .net, and am working on my first project using databinding to update records in Oracle. On this first form I'm building, I have a detail view for vehicle records (VIN, year/make/model, license plate number, that sort of thing). The first thing I did in terms of writing to the DB was saving updates:
private void btn_saveDesc_Click(object sender, EventArgs e)
{
bool isSaved = false;
hJA_VEHICLEBindingSource.EndEdit();
DataSet1.HJA_VEHICLEDataTable ch =
(DataSet1.HJA_VEHICLEDataTable)dataSet1.HJA_VEHICLE.GetChanges();
if (ch == null)
{
MessageBox.Show("There are no changes to save.");
}
else
{
Service<TVDDataService.IService1>.Use(svcProxy =>
{
isSaved = svcProxy.SaveVehicles(ch);
});
if (isSaved)
{
// update the vehicle in the local dataset
var modifiedRows = from row in dataSet1.HJA_VEHICLE
where row.RowState == DataRowState.Modified
select row;
foreach (DataRow row in modifiedRows)
{
row.Delete();
}
dataSet1.HJA_VEHICLE.Merge(ch);
dataSet1.HJA_VEHICLE.AcceptChanges();
}
if(isSaved)
{
MessageBox.Show("The record has been saved.");
}
else
{
MessageBox.Show("The record could not be saved.");
}
}
}
That all seemed ok, so I moved on to adding new records. I made a button (I saw online where various people had said it was as good or better to make your own than use a binding navigator) and put this in its handler:
DataRowView drv = (DataRowView)hJA_VEHICLEBindingSource.AddNew();
currVeh_id = nextID(); // generate arbitrary ID for the record
drv["VEH_ID"] = currVeh_id;
drv["GRNTE_ID"] = lastSelectedGranteeID; // just to have an initial value
hJA_VEHICLEBindingSource.Filter = "VEH_ID = " + currVeh_id;
So there (above) I'm putting initial values into some required columns (VEH_ID is the PK). Then I ran the app, entered a value in the textbox for VIN, and went to save (same code as above) and this time GetChanges() returned null.
So I tried this in the "add new" button handler, in place of the first thing:
DataSet1.HJA_VEHICLERow newRow =
(DataSet1.HJA_VEHICLERow)dataSet1.HJA_VEHICLE.NewRow();
currVeh_id = nextID();
newRow.VEH_ID = currVeh_id;
newRow.GRNTE_ID = lastSelectedGranteeID;
dataSet1.HJA_VEHICLE.AddHJA_VEHICLERow(newRow);
hJA_VEHICLEBindingSource.Filter = "VEH_ID = " + currVeh_id;
Now I have something really interesting happening.
- I can successfully enter and save data on any number of new records, until I select an existing record. If I move to an existing record and then add a new record, then when I go to save the new record, the values that were explicitly set in code get written to the DB but data entered into the GUI do not "take" for the new record.
- I can successfully change any number of existing records, until I enter a new record. If I add one or more new records, save, and then try to save changes to an existing record, the call to GetChanges() returns null (so again, apparently it is not "seeing" what's been entered through the GUI).
So in both of these cases, the change from old to new, or new to old, appears to introduce some condition that makes the datatable unaware of what was entered into the GUI. But in changing from old to new it only takes selecting an existing record, whereas with changing from new to old, it only breaks after saving (if I do a new record but then abandon it without saving, I can modify existing records without problems).
I added this into the save handler, just prior to the actual save (in a loop because ch is a datatable, but really the code is set up to where you have to either save or abandon the new record before moving on - thus the loop only executes once):
foreach (DataSet1.HJA_VEHICLERow r in ch)
{
DataRowView drv = (DataRowView)hJA_VEHICLEBindingSource.Current;
MessageBox.Show(drv["VIN_ID"].ToString());
MessageBox.Show(r.VEH_ID + "\n" + r.GRNTE_ID + "\n'"
+ r.VIN_ID + "'");
}
Where VIN_ID is the column to which this particular textbox is bound (I tried this with other fields on the form too, to verify it wasn't just something flaky about that one textbox). The first message box (DataRowView from the binding source) shows the vin that I entered into the textbox. The second message box (row from the table returned by GetChanges()) shows the empty string for VIN_ID, although it has the correct (set through code) values for VEH_ID and GRNTE_ID. The same thing happens if I select a different value for GRNTE_ID using the combo box bound to that column; the row from the datatable still has the value that was set through code, "unaware" of the value selected through the GUI.
Why would the datatable and binding source have different values for the same field? And why would the datatable be able to "see" values entered through the GUI only until the user switches from existing to new, or from new to existing?
Thanks.
Angel:
I'm doing that in my Service:
public bool SaveVehicles(DataSet1.HJA_VEHICLEDataTable vehicles)
{
bool saved = false;
if (vehicles != null && !vehicles.HasErrors)
{
HJA_VEHICLETableAdapter ta = new HJA_VEHICLETableAdapter();
int result = ta.Update(vehicles);
saved = (result > 0);
}
return saved;
}
This is called from this block from my original post:
Service<TVDDataService.IService1>.Use(svcProxy =>
{
isSaved = svcProxy.SaveVehicles(ch);
});
Johannes:
The call to EndEdit() is the second line in the save handler (near the top of my post). Should I be calling it somewhere else as well?
Thanks.
Just to clarify: SaveVehicles cannot be the source of the problem, since the problem is appearing before SaveVehicles is ever called. What condition could cause a discrepancy between the BindingSource and the DataTable after EndEdit() has been called and before anything actually writes to the DB?
You have to update your table adapter after use EndEdit(); also you can update your complete DataSet with the follow snippet :
this.BindingSource1.EndEdit();
this.TableAdapter1.Update(this.DataSet1);
Hopes Helps...
*IF you are using a BindingSource as well:
Just do a simple BindingSource.EndEdit() and your TextBox data will be sent over to the DataTable.
Example:-
_bsHeader.EndEdit();
if (_dsHeader.HasChanges())
{
DataTable dsInsert = _dsHeader.GetChanges(DataRowState.Added).Copy();
_objDal.Insert(dsInsert);
}
Hope this helps anyone who stumbles here.