Click here to Skip to main content
14,579,188 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hi how can i insert null cells in datagridview when inserting access.mdb


i tried these codes but i'm geting "nothing" error

What I have tried:

Dim D_TBRM = .Rows(ROW).Cells(C_TBRM).Value
                     If D_TBRM = "" Then D_TBRM = DBNull.Value.ToString
                     Dim D_BARKOD As String = .Rows(ROW).Cells(C_BARKOD).Value.ToString
                     If D_BARKOD = "" Then D_BARKOD = DBNull.Value.ToString



Comm.Parameters.AddWithValue("@10", D_TBRM)
                       Comm.Parameters.AddWithValue("@11", .Rows(ROW).Cells(C_MIKTAR).Value * 1)
                       Comm.Parameters.AddWithValue("@12", D_BARKOD)
Posted
Updated 29-Jun-20 21:11pm
v2
Comments
Richard MacCutchan 30-Jun-20 4:22am
   
The NullReferenceException means .Rows(ROW).Cells(C_BARKOD) does not contain any data.
Member 14588284 30-Jun-20 5:33am
   
yes its true
when datagrid cell is empty i send this access.mdb nullvalue
Richard MacCutchan 30-Jun-20 5:40am
   
If the cell is empty you cannot refer to its Value since that does not exist. Test the cell first before you try to use its value.
Member 14588284 30-Jun-20 5:48am
   
But i know cell is empty
Richard MacCutchan 30-Jun-20 5:54am
   
Then do not try to access its value. How many times does that need repeating?
Member 14588284 30-Jun-20 5:58am
   
not continuous but sometimes this will happen
Richard MacCutchan 30-Jun-20 6:03am
   
If .Rows(ROW).Cells(C_BARKOD).Value == null
    D_BARKOD = DBNull.Value.ToString
Else
    D_BARKOD = .Rows(ROW).Cells(C_BARKOD).Value.ToString
End If
Member 14588284 30-Jun-20 6:30am
   
== null

in here "null is not declared" error



i changed like this
i'm trying others i will return
 If .Rows(ROW).Cells(C_BARKOD).Value = "" Then
                            D_BARKOD = DBNull.Value.ToString
                        Else
                            D_BARKOD = .Rows(ROW).Cells(C_BARKOD).Value.ToString
                        End If
Richard MacCutchan 30-Jun-20 6:33am
   
Use the VB.NET value IsNothing.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

You really need to be clearer when you describe a problem - copy and paste of the error message is the best way, 'geting "nothing" error" isn't much help, particularly when you don't tell us where you get it, or when.

But I'll assume that the error was a "null reference exception" since you are talking about NULL values and databases.

At a guess, the Value of .Rows(ROW).Cells(C_BARKOD) is returning a null value ("Nothing" in VB), so the call to ToString is - rightly - throwing an exception because you are trying to call a method - Tostring - on a non-existent object instance - null.
If so, then check for it before you try to call anything!

We can' check for you - we can't run your code and we don't have any of your data - so, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
   
Comments
Member 14588284 30-Jun-20 3:54am
   
https://yadi.sk/i/_yV7a1UNSBnDwg
error message


my all codes like this.
Shortly somtimes my datagridview cells can be is null
Dim STNNAME = "SNO,CARKOD,TARIH,BELGETIP,BELGENO,BA,ACIKLAMA,MUH_ACIKLAMA,STKKOD,TBRM,MIKTAR,BARKOD,USTBRM,MIKTAR2,BRMFIYAT,TUTAR,KDV,KDVTUTAR,ISK,ISKTUTAR,NETTUTAR,KDVDAHIL,KAYITNO,STOKLU,ISLTARIH,GIDKALEM,GIDYER,BIRIM,MUHKOD"        Dim STR1 = "INSERT INTO CARTH002(" & STNNAME & ") VALUES(@1,@2,@3,@4,@5,@6,@7,@8,@9,@10,@11,@12,@13,@14,@15,@16,@17,@18,@19,@20,@21,@22,@23,@24,@25,@26,@27,@28,@29) "        Dim GUN As DateTime        ' MsgBox(.Cells(0).Value)        With FAT_TABLO            For ROW = 0 To .Rows.Count - 1                If IsDBNull(.Rows(ROW).Cells(1).Value) = False Then                    If .Rows(ROW).Cells(1).Value.ToString <> "" Then                        ' SNO,CARKOD,TARIH,BELGETIP,BELGENO,BA,ACIKLAMA,MUH_ACIKLAMA,STKKOD,                        ' TBRM,MIKTAR,BARKOD,USTBRM,MIKTAR2,BRMFIYAT,TUTAR,KDV,KDVTUTAR,ISK,ISKTUTAR,                        ' NETTUTAR, KDVDAHIL, KAYITNO, STOKLU, ISLTARIH, GIDKALEM, GIDYER                        Dim D_ACIKLAMA = .Rows(ROW).Cells(C_ACIKLAMA).Value                        If D_ACIKLAMA = "" Then D_ACIKLAMA = DBNull.Value.ToString                        Dim D_STKKOD As String = .Rows(ROW).Cells(C_STKKOD).Value.ToString                        If D_STKKOD = "" Then D_STKKOD = DBNull.Value.ToString                        Dim D_TBRM As String = .Rows(ROW).Cells(C_TBRM).Value                        If D_TBRM = "" Then D_TBRM = DBNull.Value.ToString                        Dim D_BARKOD As String = .Rows(ROW).Cells(C_BARKOD).Value.ToString                        If D_BARKOD = "" Then D_BARKOD = DBNull.Value.ToString                        Dim D_USTBRM As String = .Rows(ROW).Cells(C_USTBRM).Value.ToString                        If D_USTBRM = "" Then D_USTBRM = DBNull.Value.ToString                        Dim D_MIKTAR2 As String = .Rows(ROW).Cells(C_MIKTAR2).Value.ToString                        If D_MIKTAR2 = "" Then D_MIKTAR2 = 0                        Dim D_GIDKALEM As String = .Rows(ROW).Cells(C_GIDKALEM).Value.ToString                        If D_GIDKALEM = "" Then D_GIDKALEM = DBNull.Value.ToString                        Dim D_GIDYER As String = .Rows(ROW).Cells(C_GIDYER).Value.ToString                        If D_GIDYER = "" Then D_GIDYER = DBNull.Value.ToString                        Dim D_MUHKOD = .Rows(ROW).Cells(C_MUHKOD).Value                        If D_MUHKOD = "" Then D_MUHKOD = DBNull.Value                        '  MsgBox(.Rows(ROW).Cells(C_TARIH).Value)                        GUN = .Rows(ROW).Cells(C_TARIH).Value                        Dim Comm As New OleDbCommand(STR1, cn)                        Comm.Parameters.AddWithValue("@1", ROW + 1)                        Comm.Parameters.AddWithValue("@2", CARKOD.Text)                        Comm.Parameters.AddWithValue("@3", GUN)                        Comm.Parameters.AddWithValue("@4", MODUL.Text)                        Comm.Parameters.AddWithValue("@5", BELGENO.Text)                        Comm.Parameters.AddWithValue("@6", BA.Text)                        Comm.Parameters.AddWithValue("@7", D_ACIKLAMA)                        Comm.Parameters.AddWithValue("@8", "")                        Comm.Parameters.AddWithValue("@9", D_STKKOD)                        If D_TBRM <> "" Then                            Comm.Parameters.AddWithValue("@10", D_TBRM)                        End If                        Comm.Parameters.AddWithValue("@11", .Rows(ROW).Cells(C_MIKTAR).Value * 1)                        Comm.Parameters.AddWithValue("@12", D_BARKOD)                        Comm.Parameters.AddWithValue("@13", D_USTBRM)                        Comm.Parameters.AddWithValue("@14", D_MIKTAR2)                        Comm.Parameters.AddWithValue("@15", .Rows(ROW).Cells(C_BRMFIYAT).Value * 1)
OriginalGriff 30-Jun-20 4:29am
   
So check for nulls ...
Member 14588284 30-Jun-20 5:38am
   
I think I couldn't explain
i know datagrid cell is null (empty)
but when its null i must send it to access.mdb a null value

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100