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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T01:17:20+00:00 2026-06-11T01:17:20+00:00

I tried to save a record from a query into a DataRow , but

  • 0

I tried to save a record from a query into a DataRow, but did not make it. I could need some help. This my code:

private DataSet CreateDataSet()
{
    DataSet ds = new DataSet();

    // create Countries table and columns
    DataTable dtApplications = ds.Tables.Add("Applications");
    DataColumn dtApplicationID = dtApplications.Columns.Add("ApplicationID", typeof(int));
    DataColumn dtApplicationName= dtApplications.Columns.Add("ApplicationName", typeof(string));

    DataTable dt = new DataTable();
    SqlConnection con = new SqlConnection(MoahannedConnectionString);
    SqlCommand cmd1 = new SqlCommand("select AppID,AppName from Application", con);
    SqlDataAdapter da = new SqlDataAdapter();
    SqlDataReader sdr = null;
    dt.TableName = "Application";
    con.Open();
    sdr = cmd1.ExecuteReader();
    while (sdr.Read())
    {
       AddDataToTable(dtApplicationID, dtApplicationName, dtApplications, ds);
    }
}

private void AddDataToTable(DataColumn ID, DataColumn Name, DataTable myTable,DataSet ds)
{
    DataRow row;
    row = myTable.NewRow();
    row["ApplicationID"] = ID;
    row["ApplicationName"] = Name;
    myTable.Rows.Add(row);
}
  • 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-11T01:17:21+00:00Added an answer on June 11, 2026 at 1:17 am

    You can use the DataAdapter directly to load a DataTable:

    using(var da = new SqlDataAdapter("select AppID,AppName from Application", con))
    {
        da.Fill(dt);
    }
    

    The rest of the code above is redundant in my opinion.

    Side-note: You’re getting the exception because you want to pass a field of a record to a DataRow’s field. But actually you’re passing the DataColumn.

    This would work but is unnecessary:

    row["ApplicationID"] = sdr.GetInt32(sdr.GetOrdinal("ApplicationID"));
    row["ApplicationName"] = sdr.GetString(sdr.GetOrdinal("ApplicationName"));
    

    Where sdr is your DataReader.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

tried uncommenting pam_limits.so from the pam.d directory but no luck. Basic PAM seems to
I'm trying to use NAudio to record from the micrphone and save tthat data
I need to generate unique record id for the given unique string. I tried
I've been trying to figure this problem out all morning. Tried some things from
I tried everything to get this code working, and I hope someone will save
Tried searching the site, but cannot find an answer to my problem: Lets say
Tried a bunch of things but I can't get it to work consistently amid
tried to find the answer by googling and in MSDN but with no luck.
I am trying to use do the most simple update query but Doctrine issues
I have input from Form Name,Branch (text) x,y and Total are (number) fields Record

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.