Step 1: I have created a C# application called : Student details

Step 2: Added four TextBoxes and named them as :

Image below to refer:

Studentname.Text
StudentSurname.Text
StudentCity.Text
StudentState.Text

DATA INSIDE CSV FILE

vikas,gadhi,mumbai,maharashtra

prem,yogi,kolkata,maha

roja,goal,orissa,oya

ram,kala,goa,barka

Issue is How do I fetch all the data(surname,city,state) of user prem into above textboxes studentsurname,studentcity,studentstate from csv file when I search the name in textbox 1 => studentname.Text as prem

Below is the Code where I am stuck at return null and code inside Load_Script_Click

void Connection_fetch_details(String searchName)
{
    var strLines = File.ReadLines(filePath);
    foreach (var line in strLines)
    {
        if (line.Split(',')[0].Equals(searchName))
        {
            Connection_fetch_details cd = new Connection_fetch_details()
            {
                username = line.Split(',')[1]
            };
        }
    }
    return;
}

private void Load_Script_Click(object sender, EventArgs e)
{
    // load script is button 

    String con_env = textenv.Text.ToString();
    //Address Address = GetAddress("vikas");
    //textsurname.text = Address.Surname

    Connection_fetch_details cd = Connection_fetch_details(con_env);
    textusername.Text = cd.username;
}

==============================================================
Class file name : Address.class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DDL_SCRIPT_GENERATOR
{
    public class Connection_fetch_details
    {
        public string username { get; set; }
    }
}

2 Answers

1
Oliver On

To accomplish your goal you have to further separate your problem in more granular steps and also distinguish between what you show in your UI and what informations you hold in the background in which format.

  1. Create a class with the desired properties

    public class Student { public string Name { get; set; } ... }
    
  2. Learn how to read a csv file into such an object by using an existing library like CsvHelper or CsvReader.

  3. When you have something like List<Student> from this part. Learn how you can visualize such a thing by using some Binding (also depends on the visualization you use Winforms, WPF, etc.).
  4. Depending on the visualization component it already supports filtering or you need to filter by yourself by using e.g. LINQ to get the matching elements students.Where(student => student.Name.StartsWith(search)).

So far a lot of smaller problems which is simply to much to answer in a single one. Please try to break down your problems into smaller ones and search for their solutions. If you get stuck, ask a new question. That's all I can do for you now.

2
ADyson On

The main problem is that your method is void, which means it doesn't return any value. So even though you may be finding a match, and creating a Connection_fetch_details object, you aren't returning that result back to the calling method.

This will fix that problem:

Connection_fetch_details Connection_fetch_details(String searchName)
{
    var strLines = File.ReadLines(filePath);
    foreach (var line in strLines)
    {
        if (line.Split(',')[0].Equals(searchName))
        {
            Connection_fetch_details cd = new Connection_fetch_details()
            {
                username = line.Split(',')[1]
            };
            return cd; //return the object containing the matched username
        }
    }
    return null;
}

Now it will return a Connection_fetch_details object if there is a match, or null if there is no match.


Next, you asked about returning all the fields, not just one. For that you would need to

a) add more properties to your object

b) add more code to populate those properties from the CSV

c) add code to populate the textboxes with the results from the object.

I'm also going to rename "username" to something more relevant, since none of the field names you described in the question match that. I'm also going to rename your class to "Student", and rename your search method, for the same reason.

Here's an example:

Student searchStudent(String searchName)
{
    var strLines = File.ReadLines(filePath);
    foreach (var line in strLines)
    {
        var split = line.Split(',');
        if (split[0].Equals(searchName))
        {
            Student s = new Student()
            {
                firstname = searchName,
                surname = split[1],
                city = split[2],
                state = split[3]
            };
            return s; //return the object containing the matched name
        }
    }
    return null;
}

private void Load_Script_Click(object sender, EventArgs e)
{
    // load script is button 

    String con_env = textenv.Text.ToString();
    //Address Address = GetAddress("vikas");
    //textsurname.text = Address.Surname

    Student st = searchStudent(con_env);
    textsurname.Text = st.surname;
    txtcity.Text = st.city;
    txtstate.Text = st.state;
}

namespace DDL_SCRIPT_GENERATOR
{
    public class Student
    {
        public string firstname { get; set; }
        public string surname { get; set; }
        public string city { get; set; }
        public string state { get; set; }
    }
}