-2

I want to retrieve various information from my DB. But during testing my code I got the message:

Data is null. This method or property cannot be called on null values

There are nullable columns in my dB and I think those are causing the problem. Please someone help me to solve this problem. Here is my code:

using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Text;
using WpfDropDownNavigate.Models;

namespace WpfDropDownNavigate.Business
{
    public class MusteriBusiness
    {
        public static List<Musteriler> MusterileriGetir()
        {
            string sqltrtr = "select * from Customers";
            SqlDataReader reader = Helper.ExecuteReader(sqltrtr, CommandType.Text);
            List<Musteriler> musteriler = new List<Musteriler>();
            while (reader.Read())
            {
                musteriler.Add(new Musteriler
                {
                    MusteriID =reader.GetString(0),
                    FirmaAdi = reader.GetString(1),
                    SorumluKisi = reader.GetString(2),
                    Yetkisi = reader.GetString(3),
                    Adres = reader.GetString(4),
                    Sehir = reader.GetString(5),
                    Ulke = reader.GetString(6),
                    TelefonNo = reader.GetString(7),
                }) ;
            }
            reader.Close();
            return musteriler;
        }

        public static int MusteriGuncelle(string firmaAdi, string sorumluKisi, string yetkisi, string adres, string sehir, string ulke, string telefonNo,string musteriId)
        {
            string sqltrt = string.Format("update Customers set CompanyName='{0}',ContactName='{1}',ContactTitle='{2}',Address='{3}',City='{4}',Country='{5}',Phone='{6}' where CustomerID='{7}'", firmaAdi, sorumluKisi, yetkisi, adres, sehir, ulke, telefonNo, musteriId);
            return Helper.ExecuteNonQuery(sqltrt, CommandType.Text);
        }  
    }
}
using System;
using System.Collections.Generic;
using System.Text;

namespace WpfDropDownNavigate.Models
{
   public class Musteriler
   {
        public string FirmaAdi { get; set; }
        public string SorumluKisi { get; set; }
        public string Yetkisi { get; set; }
        public string Adres { get; set; }
        public string Sehir { get; set; }
        //public string Bolge { get; set; }
        //public string PostaKodu { get; set; }
        public string Ulke { get; set; }
        public string TelefonNo { get; set; }
        //public string FaxNo { get; set; }

        public string MusteriID { get; set; }
    }
}
ASh
  • 34,632
  • 9
  • 60
  • 82
Mustafasan
  • 11
  • 1
  • 4
  • 1
    Your `UPDATE` statement in your `MusteriGuncelle` method is vulnerable to SQL Injection (SQLi), it is a security vulnerability you should avoid using parametrized queries. Check this website for an explanation and coding guidelines, including c# [Bobby tables](https://bobby-tables.com/) – Cleptus Jan 29 '22 at 16:03
  • 2
    The `SELECT *` is something you may want to avoid, because if later you add/remove columns to the table your `reader.GetString(x)` may not work as intended. If possible I would suggest doing `SELECT column1, column2, etc FROM tableName` – Cleptus Jan 29 '22 at 16:06
  • I bet that error message came with a line number (if you don't see it, double-click the error message and the editor will jump to the offending line). When you report a problem include that information (not as a line number, do it with a comment). Please don't make us guess – Flydog57 Jan 29 '22 at 17:34

1 Answers1

0

Nullable columns must be tested for null. E.g. Assuming that Adres can be null

Adres = reader.IsDBNull(4) ? null : reader.GetString(4),

This is not necessary for columns declared as NOT NULL in the database.

See also:

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188