PDF Checkbox Error : Value cannot be null. Parameter name: value - c#

I am trying to check a checkbox in PDFform using PDFsharp. I am using below code
PdfCheckBoxField chkbox = (PdfCheckBoxField)(pdf.AcroForm.Fields["chkbox"]);
chk.ReadOnly = false;
chk.Checked = true;
chk.ReadOnly = true;
I am getting below error on line chk.Checked = true;
ArgumentNullException was unhandled
Value cannot be null.
Parameter name: value

You are reading the object into 'chkbox', but setting 'chk':
PdfCheckBoxField chkbox = (PdfCheckBoxField)(pdf.AcroForm.Fields["chkbox"]);
chkbox.ReadOnly = false;
chkbox.Checked = true;
chkbox.ReadOnly = true;
I'm not sure why it isn't failing on the first line.

This little gem came from looking at the PDFSharp source code. This is how to set the value for checkboxes with the same name. I can't tell exactly if this was the original problem posted, but based on the error, and my own frustration, I came up with this solution.
//how to handle checking multiple checkboxes with same name
var ck = form.Fields["chkbox"];
if (ck.HasKids)
{
foreach (var item in ck.Fields.Elements.Items) {
//assumes you want to "check" the checkbox. Use "/Off" if you want to uncheck.
//"/Yes" is defined in your pdf document as the checked value. May vary depending on original pdf creator.
((PdfDictionary)(((PdfReference)(item)).Value)).Elements.SetName(PdfAcroField.Keys.V, "/Yes");
((PdfDictionary)(((PdfReference)(item)).Value)).Elements.SetName(PdfAnnotation.Keys.AS, "/Yes");
}
}
else {
((PdfCheckBoxField)(form.Fields["chkbox"])).Checked = true;
}

I encountered a similar problem.
The error was in the created form checkbox field.
I had different value set as export value. Use default value.

Related

Listview has no item selected , when we click at the empty area in the listview c#

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.

Display String in a Database as a Checkbox

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));

Converting RadioButtlonList Item to Boolean for entry to Database via LINQ

I am attempting to read items from local RadiobuttonList items and take them as Boolean values and insert them to my database.
The problem the thread keeps getting aborted and I am not sure where I am going wrong. So I was wondering if could have some help troubleshooting this issue.
I have a method that reads in the Radiobuttonlist and should check if the Yes option is selected and outputs whether a true or false:
private Boolean checkRB(RadioButtonList list)
{
Boolean value;
if (list.SelectedItem.Text == "Yes")
{
value = true;
}
else
{
value = false;
}
return value;
}
Next here is the line that i am using to insert the item to the database:
table rea = new table();
rea.another_inst = checkRB(rbAnotherInst);
Could anyone help me with trouble shooting this?
From your comment: "I added this line in my code: Response.Write(oe.InnerException); Response.End(); And got the error."
Why do you call Response.End()? Response.End() triggers a ThreadAbortException by design:
http://support.microsoft.com/kb/312629

Searching ListBox control and programmatically selecting closest match

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

Assignment Problem

my problem is that for some reason, a simple assignment isn't working.
the dataGridView is binded to DB and im trying to assign a value type string to a column of type string
enter code here //the initialization of the DataGridView
bindingSourceSchema.DataSource = null;
dgwSchema.Columns["colID"].DataPropertyName = "APP_ID";
dgwSchema.Columns["colName"].DataPropertyName = "DESCRIPTION";
dgwSchema.Columns["colTextbox"].DataPropertyName = "APP_ARGS";
dgwSchema.Columns["colTextbox"].HeaderText = "Parameters";
dgwSchema.Columns["colLink"].DataPropertyName = "APP_PATH";
dgwSchema.Columns["colLink"].HeaderText = "Path";
DataGridViewLinkColumn colLink = (DataGridViewLinkColumn)dgwSchema.Columns["colLink"];
colLink.UseColumnTextForLinkValue = true;
colLink.Text = "Edit";
bindingSourceSchema.DataSource = SchemaDB.GetGenericApps();//the assignment
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
dgwSchema.CurrentRow.Cells["colLink"].Value = openFileDialog.FileName;
// !! ?? dgwSchema.CurrentRow.Cells["colLink"].Value STAYS with parameter "Edit"
}
Thanks
Eyal
It could be a problem with where you are running this code. Is this in the PageLoad event? Make sure the grid is not being bound again after you do this. What does the debugger show you if you put a break point after the assignment?
The reason is following property is set to true.
colLink.UseColumnTextForLinkValue = true;
Set it to false. Then it will resolve your problem.

Categories