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 7839531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:28:02+00:00 2026-06-02T15:28:02+00:00

I want to ask more to show data from SQL Server to WinForm using

  • 0

I want to ask more to show data from SQL Server to WinForm using a datagrid.
I’ve been creating a datagrid and the stored procedure to show data is

ALTER PROC [dbo].[SP_GetData]
AS
  SELECT nama , nim
  FROM tabledata

and I’ve created the function to access the database and the stored procedure in C#

string Sp_Name = "dbo.SP_GetData";

SqlConnection SqlCon = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DBMahasiswa;Data Source=.");
SqlCon.Open();

SqlCommand SqlCom = new SqlCommand(Sp_Name , SqlCon);
SqlCom.CommandType = CommandType.StoredProcedure;

List<mahasiswaData> listMahasiswa = new List<mahasiswaData>();

using (SqlDataReader sqlDataReader = SqlCom.ExecuteReader())
{
   if (sqlDataReader.HasRows)
   {
      while (sqlDataReader.Read())
      {
         mahasiswaData DataMhs = new mahasiswaData();
         DataMhs.Nama = sqlDataReader["Name"].ToString();
         DataMhs.Umur = Convert.ToInt32(sqlDataReader["Age"]);
         listMahasiswa.Add(DataMhs);
      }
   }
}

SqlCon.Close();
return listMahasiswa;

and finally, in the show button I add this code

dgvmahasiswa.DataSource = new MahasiswaDB().LoadMahasiswa();

Could somebody tell me where the fault is or the alternatives one?

Thank You So Much! 😀

  • 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-06-02T15:28:03+00:00Added an answer on June 2, 2026 at 3:28 pm

    Some things to think about:

    1. At the moment, if your code runs into exceptions, you’ll leave a
      SqlConnection hanging around; you’ve used the using pattern for your
      SqlDataReader; you should extend it to all of your disposable
      objects.

    2. You are swallowing exceptions; if your query fails, the connection
      cannot be made, or something else happens, you’ll never really know – your function will just return null.

    3. Is it possible for name or age to be null? Age to be non-numeric?
      There’s no test for any unexpected values, which you’ll also never
      know about.

    4. If you don’t have any records, you’ll return an empty list. Is this
      desired? Or would you prefer to know there were no records?

    You might prefer to look at something like this:

    public List<mahasiswaData> GetData(){
    
    
        List<mahasiswaData> gridData = new List<mahasiswaData>();
    
    
        try{
    
            using(SqlConnection conn = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=DBMahasiswa;Data Source=."))
            {
                using(SqlCommand command = new SqlCommand())
                {
                    command.Connection = conn;
                    command.CommandType = CommandType.StoredProcedure;
                    command.Text = "dbo.SP_GetData";
    
                    using(SqlDataReader reader = command.ExecuteReader())
                    {
                        if(reader.HasRows){
                            while(reader.Read())
                            {
                               object rawName = reader.GetValue(reader.GetOrdinal("Name"));
                               object rawAge = reader.GetValue(reader.GetOrdinal("Age"));
    
                               if(rawName == DBNull.Value || rawAge == DBNull.Value)
                               {
                                   //Use logging to indicate name or age is null and continue onto the next record
                                   continue;
                               }
                               //Use the object intializer syntax to create a mahasiswaData object inline for simplicity
                               gridData.Add(new mahasiswaData()
                                                       {
                                    Nama = Convert.ToString(rawName),
                                                            Umur = Convert.ToInt32(rawAge) 
                                                       });
    
    
                            }
                        }
                        else{
                            //Use logging or similar to record that there are no rows. You may also want to raise an exception if this is important.
                        }
    
                    }
    
                }
    
    
            }
    
    
        }
        catch(Exception e)
        {
           //Use your favourite logging implementation here to record the error. Many projects use log4Net
           throw; //Throw the error - display and explain to the end user or caller that something has gone wrong!
        }
    
    
        return gridData;
    
    
    }
    

    Note that if you are sure that age or name will never be null then you can simplify the middle section:

    while (reader.Read())
    {
        //Use the object intializer syntax to create a mahasiswaData object inline for simplicity
        gridData.Add(new mahasiswaData()
        {
            Nama = reader.GetString(reader.GetOrdinal("Name")),
            Umur = reader.GetInt32(reader.GetOrdinal("Age"))
        });
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In my app from applicationDidEnterBackground i want to ask the application for more time
Possible Duplicate: T-SQL Pivot? Possibility of creating table columns from row values I've been
I want to ask that i am using core image and applying some filter
I'm creating a graph in php using the google graphing library to show StarCraft
I want to ask if is it possible to declare more than one background
I've been using Entity Framework Profiler to test my data access in an MVC
I want to ask how other programmers are producing Dynamic SQL strings for execution
I want to auto-generate a HTML table from some custom data. One of the
want to ask user to input something but not want to wait forever. There
I want to ask how can someone read keystrokes while in a html page

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.