I have a Code where i am getting data from csv file on name search if i type name in textbox1 then deatails related to that person are displayed in the remaining textboxes like surname , city , state
error :error screenshot 1: https://i.stack.imgur.com/VdVEZ.png
Download link of my project :
Link 1: https://www.sendspace.com/file/76vdv5
Code i have written
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace studentdetails
{
public partial class Form1 : Form
{
String filePath = "C:\\Users\\vikas\\Desktop\\d.csv";
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
String con_env = textName.Text.ToString();
UserDetails ud = SearchFor(con_env);
textSurname.Text = ud.surname;
textCity.Text = ud.city;
textState.Text = ud.state;
}
UserDetails SearchFor(String searchName)
{
var strLines = File.ReadLines(filePath);
foreach (var line in strLines)
{
var bits = line.Split(',');
if (bits[0].Equals(searchName))
{
return new UserDetails()
{
surname = bits[1],
city = bits[2],
state = bits[3],
};
}
}
return null;
}
}
}
Userdetails Clas
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace studentdetails
{
class UserDetails
{
public string firstname { get; set; }
public string surname { get; set; }
public string city { get; set; }
public string state { get; set; }
}
}
Can anyone figure out why this error occurring
Object reference not set to an instance of an object.