Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7690461
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:24:09+00:00 2026-05-31T20:24:09+00:00

I am a beginner and writing code to display data in a Gridview using

  • 0

I am a beginner and writing code to display data in a Gridview using Asp.Net and c#. I want to know if the approach I followed is correct or not. I want suggestions on standards and architectural issues, best practices with my code so that I can modify my code accordingly. I appreciate your great suggestions and code additions.

Connection Code:

    public class DemoProjConnectionClass
    {
        public SqlConnection DemoProjConnection()
        {
            SqlConnection con = new SqlConnection("Data Source=Localhost;Initial Catalog=master;Integrated Security=True");
            return con;
        }
    }

Domain Code(gets & sets):

public class DemoProjDomainClass
{

public int EmpId { get; set; } 
public string EmpName { get; set; } 
public int Salary { get; set; } 

 } 

Class Library Code:

public class DemoProjServiceClass
{
    public IList<DemoProjDomainClass> getDemoProjList()
    {
        string sqlDemoProjList;
        sqlDemoProjList = "SELECT EmpId,EmpName,Salary from Employee";
        DemoProjConnectionClass x = new DemoProjConnectionClass();
        SqlConnection con = x.DemoProjConnection();
        con.Open();
        SqlCommand cmd = new SqlCommand(sqlDemoProjList, con);
        cmd.CommandType = CommandType.Text;
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds, "tempTable1");



        IList<DemoProjDomainClass> DemoProjList = new List<DemoProjDomainClass>();
        for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
        {
            DemoProjDomainClass _obj = new DemoProjDomainClass();

            _obj.EmpId = Convert.ToInt16(ds.Tables[0].Rows[i][0]);_obj.EmpName = ds.Tables[0].Rows[i][1].ToString();_obj.Salary = Convert.ToInt16(ds.Tables[0].Rows[i][2]);DemoProjList.Add(_obj);
        }
        return DemoProjList;
    }
} 

UI Code

protected void Page_Load(object sender, EventArgs e)
{
   DemoProjServiceClass ob=new DemoProjServiceClass();
    GridView1.DataSource = ob.getDemoProjList();
    GridView1.DataBind();

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T20:24:10+00:00Added an answer on May 31, 2026 at 8:24 pm

    Connection Code:

    I will not hardcode the connectionstring like that. I will keep that in a config file (web.config or so..) and read from there so that i can change my connection string any time if needed with a recompilation.

    Class Library Code

    Your getDemoProjList method dont have any exception handling. I will wrap that code with using statement so that i dont need to worry abour closing my connection

    UI

    I dont think you should load data in the PageLoad without checking whether it is a postback or not. So will use a isPostBAck property checkk. I would also do a null check before binding it as the data source of the grid.

    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack)
      {
        DemoProjServiceClass ob=new DemoProjServiceClass();
        List<DemoProjDomainClass> objList=ob.getDemoProjList();
        if(objList!=null)
        {
           GridView1.DataSource = objList;
           GridView1.DataBind();
        }
       }
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am a beginner at programming. I am writing code using class inheritance. Here
I have a small-scale WPF application using VB.net as the code behind and I
I am writing a test code (I'm just a beginner) where I need to
I'm a beginner in powershell and know C# moderately well. Recently I was writing
Well, now i have a new problem. Im writing code in C# I want
i am writing javascript code on nodepad++,i am at beginner level and write just
I'm not a beginner at C# but I really need to increase my understanding,
I am writing some C code which is expected to compile on multiple compilers
I'm writing a twitter bot using tweepy that will search for mentions to it
I'm a beginner in powershell and know C# pretty well. I have this command

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.