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; }
}
}