Im trying to get an int[] array but it's returning an error which is Value cannot be null. I'm still a newbie to c#. Can someone help or give a clue how to solve this? I tried many techniques of passing the value but still no luck.
here's where the array came from
private void btnTag_Click(object sender, EventArgs e)
{
List<int> employee_id_list = new List<int>();
foreach (Control c in panelEmployee.Controls)
{
if (c is CheckBox)
{
if (((CheckBox)c).Checked)
{
employee_id_list.Add(Convert.ToInt32(c.Tag));
}
}
}
var type = Type.GetType("Payroll." + dynamic_form);
dynamic form = Activator.CreateInstance(type) as Form;
form.GetEmployeeID(employee_id_list.ToArray());
this.Close();
}
here's where i displayed it
public int[] emp_id;
public void GetEmployeeID(int[] employee_id)
{
emp_id = employee_id;
//in this code there are no errors and it is showing the array
MessageBox.Show(string.Join(Environment.NewLine, emp_id));
}
private void btnSave_Click(object sender, EventArgs e)
{
// but when i trigger this it returns and error Value cannot be null
MessageBox.Show(string.Join(Environment.NewLine, emp_id));
}
I think your problem is that you lose the value of the variable emp_id between the GetEmployee call (it assigns value to emp_id from the parameter so it will work always you pass a not null value) and the later call to btnSave_Clickthat is trying to use the previously( and hopefully) assigned value of emp_id
Maybe you are using WebForms , in that case you should assign the emp_id to a Session state or something that won't get deleted on next request.
public int[] emp_id;
public void GetEmployeeID(int[] employee_id)
{
emp_id = employee_id;
//in this code there are no errors and it is showing the array
MessageBox.Show(string.Join(Environment.NewLine, emp_id));
}
private void btnSave_Click(object sender, EventArgs e)
{
// but when i trigger this it returns and error Value cannot be null
MessageBox.Show(string.Join(Environment.NewLine, emp_id));
}
Related
I'm trying to implement DeleteItem functionality on my DataGridView.
I have the following event:
private void btnDeleteDjelatnik_Click(object sender, EventArgs e)
{
int idDjelatnik = -1;
int index = djelatnikDataGrid.CurrentRow.Index;
Int32.TryParse(djelatnikDataGrid.Rows[index].Cells[0].Value.ToString(), out idDjelatnik);
r.DeleteDjelatnik(idDjelatnik);
}
I'm trying to get selected rows ID Column so I can pass it to my Delete method: DeleteDjelatnik(int slectedID);
the following line always gives me value of 3:
int index = djelatnikDataGrid.CurrentRow.Index;
I also tried
int index = djelatnikDataGrid.SelectedRows[0].Index;
but I'm getting ArgumentOutOfRange exception, yes my SelectionMode is on FullRowSelect
How to get this to work?
Use DataGridView's UserDeletingRow event,
private void dataGridView1_UserDeletingRow(object sender, DataGridViewRowCancelEventArgs e)
{
int deletingRowIndex = e.Row.Index;
}
Hope helps,
Since DataGridView Index property doesn't return correct value I wrote this piece of code:
public int djelatnikSelectedRowIndex { get; set; }
private void djelatnikDataGrid_CellClick(object sender, DataGridViewCellEventArgs e)
{
djelatnikSelectedRowIndex = e.RowIndex;
}
The real problem was DeleteItem property in my BindingNavigator!
Before I set it to "None" it would delete the row from my DataGridView before the execution of "btnDeleteDjelatnik_Click" and mess up the indexation so I couldn't get the real ID of the row I wanted to delete!
My method now looks like this:
private void btnDeleteDjelatnik_Click(object sender, EventArgs e)
{
int index = djelatnikDataGrid.SelectedRows[0].Index;
int idDjelatnik = -1;
Int32.TryParse(djelatnikDataGrid["IDDjelatnik", index].Value.ToString(), out idDjelatnik);
r.DeleteDjelatnik(idDjelatnik);
djelatnikDataGrid.Rows.RemoveAt(index);
}
Thanks for help everyone!
I'm new to ASP.NET :) and I'd like to understand more about session. Here's a simple example: Every time I click the button it will add one more integer to listInt and I store the list using Session["listInt"].
public partial class TestSession : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["listInt"] == null)
{
Session["listInt"] = new List<Int16>();
}
}
}
protected void AddInt_Click(object sender, EventArgs e)
{
Int16 i = 0;
List<Int16> listInt = (List<Int16>)Session["listInt"];
listInt.Add(i);
Session["listInt"] = listInt;
Response.Write("Hello!");
}
}
Here's the thing I don't understand, if I comment the line Session["listInt"] = listInt;, whenever I click the variable Session["listInt"] still store the value (means still add more integer to the list):
Int16 i = 0;
List<Int16> listInt = (List<Int16>)Session["listInt"];
listInt.Add(i);
//Session["listInt"] = listInt; //No idea why....
Response.Write("Hello!");
Can anyone please tell me how session works in this case? Thanks in advance :)
Your list is a reference type, so when you retrieve it from the server via the session state container you actually get a reference to some object in the server memory. Hence no need to reassign it later.
this is event of dropdown when a value select from dropdown it will run(Input string was not in a correct format. ERROR always COMES Plz Help ME Now)
private void cmbRoom_SelectedIndexChanged(object sender, EventArgs e)
{
if (cmbRoom.SelectedValue.ToString() != "")
{
int RoomSelectedID = Convert.ToInt32(cmbRoom.SelectedValue.ToString());
BindDataRoomBeed(RoomSelectedID);
cmbBed.SelectedIndex = 0;
}
}
private void cmbRoom_SelectedIndexChanged(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(cmbRoom.SelectedValue.ToString()))
{
int RoomSelectedID = Convert.ToInt32(cmbRoom.SelectedValue.ToString());
BindDataRoomBeed(RoomSelectedID);
cmbBed.SelectedIndex = 0;
}
}
Hope your value is integer only..
What is the type of collection items binded to your control?
This type overrides ToString method?
Does overloaded ToString method of this type returns you number?
I think the reason is in not overloaded ToString method of collection item type.
// try this
int RoomSelectedID = Convert.ToInt32(cmbRoom.SelectedText.ToString());
I have a list box that displays a collection. Initially the list box only contains a default database of two objects. The user adds new objects to the collection and it is updated in the list box.
Prior to the user adding a new object, my code works as intended.
Code for loading objects into the list box.
private void FillStudentListBox()
{
lstStudents.Items.Clear();
for (int i = 0; i < students.Count; i++)
{
Student s = students[i];
lstStudents.Items.Add(s.GetDisplayText());
}
}
My update button code:
public void btnUpdate_Click(object sender, EventArgs e)
{
int target = Convert.ToInt16(lstStudents.SelectedIndex);
if (target != -1)
{
UpdateStudentScores updateStudentScores = new UpdateStudentScores(target);
updateStudentScores.Show();
}
}
If I click on one of the default users and then click Update, the form opens and is populated with the correct data. However if I add a new object and then click Update on the new object I get an error in this part of my code:
public Student this [int i]
{
get
{
return students[i];
}
set
{
students[i] = value;
Changed(this);
}
}
I'm sure the collection isn't getting updated correctly. Somewhere here:
private void button2_Click(object sender, EventArgs e)
{
if (IsValidData())
{
List<string> result = txtScores.Text.Split(new [] {' '}, StringSplitOptions.RemoveEmptyEntries).ToList();
student = new Student(txtName.Text, result.Select(int.Parse).ToList());
this.Close();
}
}
The List Box is properly being filled but the index count is still 2 when it should be 3. What simple mistake did I make on the way?
Here's where student is supposed to be added to my collection:
private void btnAddNew_Click(object sender, EventArgs e)
{
AddNewStudent addStudentForm = new AddNewStudent();
Student student = addStudentForm.GetNewStudent();
if (student != null)
{
students += student;
}
}
I get the feeling that this code is deigned to already have the student object complete at the time of the button click.
Arrays are fixed length once they're declared. OutOfRangeException when trying to pull data from an array usually indicates that you are searching in an index that doesn't exist.
Instead of using an array, why not use a list? Then, you could use stuff like foreach.
But if you don't want to do that, you're going to have to find a way to update your array (redefine it to be bigger) so that it doesn't attempt to return data that's out of range. :)
I want to create a if statement that recognizes which string has been removed from a specific list box. I thought i could do an if statement similar to the one below and get it to work but it tells me it has invalid arguements - if anyone can guide me it would appreciated
private void button2_Click(object sender, EventArgs e)
{
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
if(listBox2.Items.RemoveAt(listBox2.SelectedItems.ToString().Equals("Test")))
{
picturebox.Image = null;
}
}
You need to check the SelectedItem before removing it:
private void button2_Click(object sender, EventArgs e)
{
if (listBox2.SelectedIndex != -1)
{
if (listBox2.SelectedItem.ToString().Equals("Test")))
picturebox.Image = null;
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
}
}
I’ve also added a check to ensure that an item is actually selected (since you would get errors otherwise).
Your issue is that you are calling ListBox.Items.RemoveAt(int index) and passing in a boolean value: listBox2.SelectedItems.ToString().Equals("Test")).
In addition, you're removing the item first, and then calling RemoveAt again, which will actually remove a different item (whatever is now at that index), or throw an exception if you've gone outside of the bounds of your ListBox collection.
You should first check if your selected item equals "Test", and then remove the item from your ListBox, like so:
private void button2_Click(object sender, EventArgs e)
{
// SelectedIndex returns -1 if nothing is selected
if(listBox2.SelectedIndex != -1)
{
if( listBox2.SelectedItem.ToString().Equals("Test") )
{
picturebox.Image = null;
}
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
}
}
You should do something like :
String deletedString = listBox2.Items.ElementAt(listBox2.SelectedIndex).ToString();
listBox2.Items.RemoveAt(listBox2.SelectedIndex);
if(listBox2.Items.RemoveAt(deletedString == "Test"))
{
picturebox.Image = null;
}
It might not compile (Check if Items has SelectedItem property).