Visual Basic Q&A

As a software engineer, I focus on .NET, especially asp.net, C#, WCF and so on, and I am also very interested in Search Engine Optimization.

Entries Tagged ‘column’

Error message when you use DataReader in Visual Basic .NET: “Invalid attempt to read from column ordinal”

Symptoms
When you use DataReader to read a row, if you try to access columns in that row, you receive an error message similar to the following:

System.InvalidOperationException: Invalid attempt to read from column ordinal ‘0′.With CommandBehavior.SequentialAccess, you may only read from column ordinal ‘2′ or greater.
Resolution
This problem occurs because you executed OleDbCommand or SqlCommand with the System.Data.CommandBehavior.SequentialAccess flag set but did not access the columns sequentially.

BUG: The DataAdapter Wizard generates an InsertCommand property that includes timestamp columns

Symptoms
When you have a timestamp column in the query of the DataAdapter Wizard, the timestamp column is included in the InsertCommand property of the DataAdapter object, and you receive the following error message:

System.Data.SqlClient.SqlException: Cannot insert a non-null value into a timestamp column. Use INSERT with a column list or with a default of NULL for the timestamp column.”The DataAdapter Wizard uses OLE DB or Microsoft SQL Client .NET Data Providers. Although the timestamp columns are read-only columns in the table, you notice that the CommandText property in the InsertCommand property is the following:

INSERT INTO Tablename(column, timestampcolumn) VALUES (@column, @timestampcolumn); SELECT column, timestampcolumn FROM Tablename
Resolution
timestamp columns are read-only columns of the table, and the values in each row of the timestamp column are unique. When the DataAdapter object connects to the Database through the Data Providers to run a set of commands, the columns in a table that cannot be updated or be inserted are marked as read-only by the Data Providers and are not included in the InsertCommand property or the UpdateCommand property of the DataAdapter object. But the timestamp columns are not marked as read-only by the .NET Data Providers in the DataAdapter object. Therefore, the InsertCommand property includes the timestamp columns.

BUG: Problems Reading and Writing Dynamic Properties of ADOX Column When You Use SQLOLEDB

Symptoms
When you connect to SQL Server using the SQL Server OLE DB Provider, if you use ActiveX Data Objects Extensions for DDL and Security (ADOX), the ADOX Column object exposes six dynamic properties in its Properties collection: Autoincrement, Default, Fixed Length, Nullable, Primary Key, and Unique.
However, any attempt to read one of these properties on an existing Column in an existing Table generates the following error message:

Error 3251, “Object or provider is not capable…” You may also encounter problems when you try to set these properties on a new Column unless you follow specific steps, which are described in the “More Information” section.
Resolution
Because you cannot use ADOX to read these Column properties, you can use an ADO Connection or SQL Server Query Analyzer to run the equivalent T-SQL statements to query object properties; or you can use SQL Server Enterprise Manager to view the properties manually.

BUG: Error message when you set the Width property of a Boolean DataGrid column to 1 in Visual Basic .NET or in Visual C# .NET: “System.ArgumentException”

Symptoms
You bind a DataGrid control to a data source that has a column with Boolean data. If you set the Width property of the DataGrid Boolean column to 1 at run time, you receive the following exception:

An unhandled exception of type ‘System.ArgumentException’ occurred in system.windows.forms.dll
Additional information: Invalid parameter used.
Resolution
To work around this bug, do not set the Width property of the DataGrid Boolean column to 1.

BUG: Cell Data Disappears in DBGrid with NumberFormat Set

Symptoms
Setting the NumberFormat property for a column in a DBGrid control to’general number’ or ‘fixed’ sometimes causes all cell data to disappear.
Resolution
At design time, right-click the DBGrid control, and click Retrieve Fieldsto set the column headers automatically to information from the bound datacontrol. Then reset any NumberFormat properties you have modified.
This will fix the problem permanently for this DBGrid control. However, youcan’t use this solution if the DatabaseName and RecordSource properties ofthe data control are not known until run time. In this case, set theNumberFormat property of each relevant column in code. This can be doneimmediately after the data control is connected to the database by usingcode similar to this:

Data1.DatabaseName = “C:\Access\Number.mdb”Data1.RecordSource = “Numbers”Data1.RefreshDBGrid.Columns(0).NumberFormat = “fixed”