none
why am I getting null values in a new datarow that has been set with row.ItemArray[i]=value.... RRS feed

  • Question

  • ViewRegexOptionDataAdapter.SelectCommand = new System.Data.SqlClient.SqlCommand("SELECT     regexoption, displayName, helpText\r\n" +
    				"FROM         viewRegexOption\r\nORDER BY regexoption", myConnection);
    try
    {
    				this.ViewRegexOptionDataAdapter.Fill(ViewRegexOption);
    	ViewRegexOption.NewRow();
    	DataRow row = ViewRegexOption.NewRow();
    	row.ItemArray[0] = -1;
    	row.ItemArray[1] = "Plain Text Replace";
    	row.ItemArray[2] = "Do not use Regex replace";
    // this point I found  in immediate windows
    // for I=0 to 2, row.ItemArray[i].ToString() is ""
    // why?
    	ViewRegexOption.Rows.InsertAt(row, 0);
    
    upon running the above, I got "...<column name> does not allow null. the datatable does not allow any null in the 3 columns. but How do set up the row correctly to the desired values?

    Thursday, October 18, 2018 9:48 AM

Answers

  • Hello, 

    > // why?

    Because ItemArray are created "on fly". The only reason for it is to create an array and import this array in one go or get a copy of existing array from DataRow.

    The rest:

    row[0] = -1;


    Sincerely, Highly skilled coding monkey.

    • Marked as answer by fs - ab Thursday, November 1, 2018 10:46 PM
    Thursday, October 18, 2018 10:20 AM

All replies

  • Hello, 

    > // why?

    Because ItemArray are created "on fly". The only reason for it is to create an array and import this array in one go or get a copy of existing array from DataRow.

    The rest:

    row[0] = -1;


    Sincerely, Highly skilled coding monkey.

    • Marked as answer by fs - ab Thursday, November 1, 2018 10:46 PM
    Thursday, October 18, 2018 10:20 AM
  • thank you.

    should have known better.

    However I do wish Ms will have simpler syntax. Allow use for row[index] syntax always and deprecate row.ItemArray being assigned directly

    Also found out not a good idea to use strongly typed DataTable for listBox when one like to add rows to the table before binding to the listbox datasource ; one can get null exception or other exception.


    • Edited by fs - ab Thursday, October 18, 2018 10:43 PM
    Thursday, October 18, 2018 10:41 PM
  • When working with a ListBox which you want to add items down the road consider using a DataReader to obtain data from the database table, use a while loop and populate a class that represents data from the database.

    The following is a mock-up devoid of the read from database, instead focuses on the ListBox.

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    
    namespace Temp
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                Shown += Form1_Shown;
            }
            private readonly BindingSource _bsPeople  = new BindingSource();
            private void Form1_Shown(object sender, EventArgs e)
            {
                var data = new List<Person>()
                {
                    new Person() { Id = 1, FirstName = "Karen"},
                    new Person() { Id = 2, FirstName = "Mary" }
                };
    
                _bsPeople.DataSource = data;
                listBox1.DataSource = _bsPeople;
    
            }
            private void button1_Click(object sender, EventArgs e)
            {
                ((List<Person>)_bsPeople.List).Add(new Person() { Id = 3, FirstName = "Jane" });           
                _bsPeople.ResetBindings(false);
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                ((List<Person>)_bsPeople.List).ForEach(p => Console.WriteLine(p.FirstName));
            }
        }
    
        public class Person
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public override string ToString()
            {
                return FirstName;
            }
        }
    }
    

    Reading from the database

    using System.Collections.Generic;
    using System.Data.SqlClient;
    
    namespace Temp
    {
        public class Operations
        {
            public List<Person> Read()
            {
                var results = new List<Person>();
                using (var cn = new SqlConnection() {ConnectionString = "Your connection string"})
                {
                    using (var cmd = new SqlCommand() {Connection = cn})
                    {
                        cmd.CommandText = "SELECT Id, FirstName FROM users";
    
                        cn.Open();
                        var reader = cmd.ExecuteReader();
                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                                results.Add(new Person() {Id = reader.GetInt32(0), FirstName = reader.GetString(1)});
                            }
                        }
    
                    }
                }
    
                return results;
            }
        }
    
    }
    


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Thursday, October 18, 2018 11:27 PM
    Moderator