0

I pieced together this CSV parser, I chose oleDB as of all the ones I read it was the one I could make sense of. I have a client model I think I need to call it in the parser but I'm unsure. I get this error and think it may it that? How can I reference the model??

System.NullReferenceException: 'Object reference not set to an instance of an object.'

on this line

using (OleDbConnection cn = new OleDbConnection(csv.ConnectionString))

I don't want to rewrite the model again here though I'm unsure how to call it.

    public ActionResult CreateBulk(HttpPostedFileBase attachmentcsv)
    {
        ConnectionStringSettings csv = ConfigurationManager.ConnectionStrings["csv"];
        using (OleDbConnection cn = new OleDbConnection(csv.ConnectionString))
        {
            cn.Open();
            using (OleDbCommand cmd = cn.CreateCommand())
            {
                cmd.CommandText = "SELECT * FROM [attachmentcsv]";
                cmd.CommandType = CommandType.Text;
                using (OleDbDataReader reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
                {
                    int clientN = reader.GetOrdinal("ClientN");
                    int homePage = reader.GetOrdinal("homePage");
                    int clientEmail = reader.GetOrdinal("clientEmail");
                    int contName = reader.GetOrdinal("contName");
                    int monthlyQuota = reader.GetOrdinal("monthlyQuota");
                    int MJTopicsID = reader.GetOrdinal("MJTopicsID");

                        foreach (DbDataRecord record in reader)
                    {
                        String Strip = record.GetString(homePage).Replace("https://www.", "").Replace("http://www.", "").Replace("https://", "").Replace("http://", "").Replace("www.", "");

                        string[] URLtests = { "https://www." + Strip, "http://www." + Strip, "https://" + Strip, "http://" + Strip };
                            string[] Metric = MajesticFunctions.MajesticChecker(URLtests);

                        var newclient = new Client { clientN = record.GetString(clientN), homePage = Metric[0], clientEmail = record.GetString(clientEmail), contName = record.GetString(contName).First().ToString().ToUpper() + record.GetString(contName).Substring(1), monthlyQuota = record.GetInt32(monthlyQuota), TrustFlow = Int32.Parse(Metric[1]), CitationFlow = Int32.Parse(Metric[2]), RI = Int32.Parse(Metric[3]), MJTopicsID = record.GetInt32(contName), UserTableID = 1 };
                        db.Clients.Add(newclient);
                        db.SaveChanges();
                    }
                }
                  return Redirect("Index");
            }
        }
    }

Form View:

@using (Html.BeginForm("CreateBulk", "Clients", FormMethod.Post, new { enctype = "multipart/form-data" }))
{         
<div class="form-group">
<label for="attachment">Select a Csv File</label>
<label class="btn btn-default btn-file">   
<input type="file" name="attachmentcsv" id="attachmentcsv" hidden>
</label>
</div>
<button type="submit" class="btn btn-primary">Upload</button>
}
</div>

Client Model

namespace Linkofy.Models
{
public class Client
{
    public int ID { get; set; }

    [Required]
    [Display(Name = "Client")]
    public string clientN { get; set; }

    [Display(Name = "Website")]
    public string homePage{ get; set; }

    [EmailAddress]
    [Display(Name = "Contact Email")]
    public string clientEmail { get; set; }

    [Display(Name = "Contact Name")]
    public string contName { get; set; }

    [Display(Name = "Monthly")]
    public int monthlyQuota { get; set; }

    [Display(Name = "TF")]
    public int TrustFlow { get; set; }

    [Display(Name = "CF")]
    public int CitationFlow { get; set; }

    [Display(Name = "RIPs")]
    public int RI { get; set; }

    public int? MJTopicsID { get; set; }
    public virtual MJTopics MJTopics { get; set; }

    public int UserTableID { get; set; }
    public virtual UserTable UserTable { get; set; }

    public virtual ICollection<Link> Links { get; set; }
    public virtual ICollection<Status> Statuss { get; set; }
}
}
liamcook
  • 143
  • 2
  • 10
  • Your method is an `ActionResult` - you need to return something –  Jan 14 '18 at 21:16
  • @StephenMuecke quite right sorry, have added though still same result – liamcook Jan 14 '18 at 21:22
  • 2
    The `return Redirect("Index");` needs to be after the loop (otherwise you exit the loop on the first iteration) –  Jan 14 '18 at 21:25
  • There seem to be some stray brackets, e.g. `string[] URLtests = { "https://www." + Strip, "http://www." + Strip, "https://" + Strip, "http://" + Strip }; {` and then the closing bracket is the first of these two `return Redirect("Index"); } }` . What are they for? – ADyson Jan 14 '18 at 21:26
  • @ADyson ah sorry was when I was learning functions, quite right not needed have removed :) thanks – liamcook Jan 14 '18 at 21:33
  • 2
    Possible duplicate of [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – mason Jan 14 '18 at 21:47
  • On which line do you get the exception? – pmcilreavy Jan 14 '18 at 21:53
  • @pmcilreavy on this line using (OleDbConnection cn = new OleDbConnection(csv.ConnectionString)) sorry have added it now – liamcook Jan 14 '18 at 21:55
  • Ok so in your app.config or web.config file, do you have a connection string defined with the name of "csv" ? – pmcilreavy Jan 14 '18 at 22:24

1 Answers1

0

In your configuration file (usually App.Config or Web.config depending on the project type) you should have a connectionStrings section:

<configuration>  
    <connectionStrings>   
        <add name="Name" providerName="System.Data.ProviderName" connectionString="Valid Connection String;" />  
    </connectionStrings>  
</configuration> 

Make sure that the name of one of your connection strings is "csv". The null reference exception you are getting is because nothing is being found in the following line of code:

ConnectionStringSettings csv = ConfigurationManager.ConnectionStrings["csv"];

And so when you try to access a property of csv in:

csv.ConnectionString

the exception occurs.

SBFrancies
  • 3,987
  • 2
  • 14
  • 37
  • ahh I see, I have added this which I found where I was writing this from however wont this only take csvs from the c:/csvfolder how would I get it to use the one I have selected in the view :/? have I used the wrong reader? Thanks – liamcook Jan 15 '18 at 15:27