Search code examples
asp.netvb.netdata-bindingspecial-characters

Exception when data binding column with special characters and spaces in ASP.NET


Description: I am encountering an exception when trying to data bind a column with special characters and spaces in an ASP.NET application. The exception is "System.Reflection.TargetInvocationException" with an inner exception of "ArgumentException".

Here is the specific error message:

"System.Reflection.TargetInvocationException HResult=0x80131604 Message=Exception has been thrown by the target of an invocation. Source=mscorlib StackTrace: at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor) at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments) at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture) at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index) at ASP.pages_artikli_aspx.__DataBind__control16(Object sender, EventArgs e) in C:\Users\Karlo Mišković\source\repos\Miskovic_OMIS_projekt\Pages\Artikli.aspx:line 118

Inner Exception 1: ArgumentException: Jedinična cijena (bez PDV-a is neither a DataColumn nor a DataRelation for table ."

I have a table named "Artikl" with a column named "Jedinična cijena (bez PDV-a)" in the database. When I try to bind this column to a control in my ASPX page using the following code:

<tr>
    <td class="first-column">Jedinična cijena (bez PDV-a):</td>
    <td><%# DataBinder.Eval(Container.DataItem, "['Jedinična cijena (bez PDV-a)']") %></td>
</tr>

I encounter the exception mentioned above.

I suspect that the issue arises due to the presence of special characters and spaces in the column name. Is there a specific way to handle such column names during data binding in ASP.NET? How can I resolve this exception and successfully bind the column with special characters and spaces?

I have also provided the relevant Stored Procedure ("PodaciZaFormuArtikl") that retrieves the data from the "Artikl" table.

CREATE PROCEDURE PodaciZaFormuArtikl
AS
BEGIN
    SELECT
        [ID artikl]
      ,[Naziv]
      ,[Jedinična cijena (bez PDV-a)]
      ,[Jmj]
      ,[Opis]
      ,[ID skladišna lokacija]
    FROM Artikl
END

Any insights or guidance on resolving this issue would be greatly appreciated. Thank you!


Solution

  • Did this, working.

    <tr>
        <td class="first-column">Jedinična cijena (bez PDV-a):</td>
        <td><%# DataBinder.GetPropertyValue(Container.DataItem, "Jedinična cijena (bez PDV-a)") %></td>                       
    </tr>