Results 1 to 7 of 7

Thread: Change "Null" to empty " " help needed

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Resolved Change "Null" to empty " " help needed

    Hi guys,i have another new problem.Using the opennetcf CSV adaptor,i managed to load a CSV into the datatable on the dataset.However,when i open the datagrid,some of the cells displays (null) where there is suppose to be empty.This affects my application as i need the cells to be empty.Anyone have any idea how to clear up the (null)?
    Last edited by Irkiere; Apr 25th, 2005 at 08:07 PM.

  2. #2

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Re: How to remove (null)?

    Set the NullText property of the affected datagrid columns to any thing you want it to display to the user

    I saw this statement in another thread.how do i go about doing it?

    Sorry,i think i didn't make myself clear enough.To put it simply,i need my csv file display as it is.i want to remove all (null)that appears in the datagrid.
    Last edited by Irkiere; Apr 22nd, 2005 at 08:32 AM.

  3. #3
    Fanatic Member Strider's Avatar
    Join Date
    Sep 2004
    Location
    Dublin, Ireland
    Posts
    612

    Re: Change "Null" to empty " " help needed

    use this instead of the csvadapter class....works perfect for me

    VB Code:
    1. Public Function ReadCSV(ByVal p_fromCSVfile As String, ByVal p_toTableName As String, ByVal p_clearTable As Boolean)
    2.         If File.Exists(p_fromCSVfile) Then
    3.             'CSV variables
    4.             Dim l_sReader As New StreamReader(p_fromCSVfile)
    5.             Dim l_csvLine, l_csvArray() As String
    6.             Dim i As Integer
    7.  
    8.             'Database variables
    9.             Dim l_sqlstmt As String
    10.             Dim l_sqlcmd As SqlCeCommand
    11.  
    12.             Try
    13.                 If p_clearTable = True Then
    14.                     l_sqlstmt = "DELETE FROM " & p_toTableName
    15.                     l_sqlcmd = New SqlCeCommand(l_sqlstmt, databaseConn.getInstance().GetConnection())
    16.                     l_sqlcmd.ExecuteNonQuery()
    17.                 End If
    18.  
    19.                 l_csvLine = l_sReader.ReadLine
    20.                 'Inserts each line in the csv file into the table
    21.                 While Not l_csvLine Is Nothing
    22.                     l_csvArray = Split(l_csvLine, ",", -1)
    23.                     l_sqlstmt = "INSERT INTO " & p_toTableName & " VALUES ("
    24.  
    25.                     For i = 0 To UBound(l_csvArray)
    26.                         If i = UBound(l_csvArray) Then
    27.                             l_sqlstmt = l_sqlstmt & "'" & l_csvArray(i) & "'"
    28.                         Else
    29.                             l_sqlstmt = l_sqlstmt & "'" & l_csvArray(i) & "',"
    30.                         End If
    31.                     Next i
    32.                     l_sqlstmt = l_sqlstmt & ")"
    33.  
    34.                     l_sqlcmd = New SqlCeCommand(l_sqlstmt, databaseConn.getInstance().GetConnection())
    35.                     l_sqlcmd.CommandText = l_sqlstmt
    36.                     l_sqlcmd.ExecuteNonQuery()
    37.  
    38.                     l_csvArray = Nothing
    39.                     l_csvLine = l_sReader.ReadLine
    40.                 End While
    41.  
    42.             Catch err As SqlCeException
    43.                 MsgBox(err.Message, MsgBoxStyle.Information, "")
    44.             Catch err As IOException
    45.                 MsgBox(err.Message, MsgBoxStyle.Information, "")
    46.             Finally
    47.                 l_sReader.Close()
    48.             End Try
    49.         Else
    50.             MsgBox("File Does Not Exist", MsgBoxStyle.Information, "")
    51.         End If
    52.     End Function
    Last edited by Strider; Apr 25th, 2005 at 11:05 AM.
    Barry


    Visual Studio .NET 2008/Visual Studio .NET 2005/Visual Studio .NET 2003
    .NET Framework 3.0 2.0 1.1/ASP.Net 3.0 2.0 1.1/Compact Framework 1.0

    SQL Server 2005/2000/SQL Server CE 2.0


    If you like, rate this post

    Compact Framework for Beginners

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Re: Change "Null" to empty " " help needed

    Sorry strider,i'm already too far into my program to make the change that u suggested.I have no choice but to stick with the csv adaptor.can u offer me any other suggestion as to avoid having the "null" appear in my datagrid?Here is what happens when my form loads.....

    Private Sub Form1_Load(By Val sender as Object, ByVal e As System.EventsArgs)Handles MyBase.Load

    'load data
    ds = New Dataset("MechanismLifeTest")

    'get path to directory
    appath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase )

    'initialise adaptor
    da = New CSVDataAdaptor(appath + "\MechanismLifeTest.csv", True)

    'fill dataset from csv
    da.Fill(ds, "MechanismLifeTest")

    'display in grid
    DataGrid.DataSource = ds.Tables("MechanismLifeTest")

    End Sub

    can u help me to add in the code that will stop "null" from appearing in my grid?Pls help in anyway if u can.Thanks!!
    Last edited by Irkiere; Apr 24th, 2005 at 09:34 AM.

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Smile Re: Change "Null" to empty " " help needed

    I have solved this issue with some help.Will post asap when free to share with you guys.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2005
    Posts
    20

    Re: Change "Null" to empty " " help needed

    Private Sub SetNullEmpty(ByRef dt As DataTable, ByRef dg As DataGrid)

    Dim r As Integer
    Dim c As Integer
    For r = 0 To dt.Rows.Count - 1
    For c = 0 To dt.Columns.Count - 1
    With dt.Rows(r)
    .Item(c) = NZStr(.Item(c))
    End With
    Next
    Next
    dg.DataSource = dt
    End Sub

    Private Sub EditRecord_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'load data
    ds = New DataSet("MechanismLifeTest")
    Dim apppath As String
    'get path to application
    apppath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase )
    'initialise adapter
    da = New CSVDataAdapter(apppath + "\MechanismLifeTest.csv", True)
    'fill dataset from csv
    da.Fill(ds, "MechanismLifeTest")
    'display in grid
    'DataGrid1.DataSource = ds.Tables("MechanismLifeTest")
    Call SetNullEmpty(ds.Tables("MechanismLifeTest"), DataGrid1) End Sub

  7. #7
    Lively Member jberman's Avatar
    Join Date
    Nov 2004
    Location
    Hollywood, FL
    Posts
    103

    Re: Change "Null" to empty " " help needed

    I see you already have a solution, so you may not care, but in case you or anyone else does, the way to do it without a function that converts the null is to set the NullText property of the column. You mentioned it in your first post. To set this property for your columns in the form designer you need yo map the table and its columsn to the datagrid and set the columsn nulltext property to "". Go to the datagrid's properties and click the "TableStyles" property collection (Keep in mind that by adding mapping this way in the designer you have to know the names of the tables/columns beforehand, all of this can also be done programatically). In the tablestyles dialog you can add each table your dataset will hold. The mapped name for a default, unnamed table in a dataset is "Table" I beleive. Add the tables with the mapped names you need, then click the tables ColumnStyles collection property. Here you can map the columns in the dataset to the grid. Make sure the mapped names match the column names in the dataset's table or they will not display on the grid. Each column also has a nulltext property. Clear this property, which by default has "(null)" in it. You now have columns ready to hold your data that will display empty string where there are null values.

    I'm not 100% I wrote this all out perfect so feel free to correct any mistakes I have guys. I'm friggin tired and i'm going to sleep. night.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width