You must Sign In to post a response.
  • Category: ASP.NET

    Loading data to datatable

    hello
    When I load data from datareader to datatable,the duplicate Id rows are not loading I need to load all rows.Please help me.
  • #767087
    Duplicate rows should be load. Please provide example with code.

  • #767089
    Hi Vinod.

    In your query you might have used distinct keyword while showing records, so duplicates are not showed.
    remove that distinct keyword.
    If still problem exists kindly post the query you used in it.

    Sridhar Thota.
    Editor: DNS Forum.

  • #767091
    Can you share your code and SQL query?

    It may be in the issue in your SQL query. Do one thing try to run the SQL query in your query analyzer and get the result can fix it.

    If you are getting proper query result then try to fix in the loading part from data reader to data table. If you share you code that will be easy to identify the issue.

    By Nathan
    Direction is important than speed

  • #767096
    Query returns the all records from mysql. in while datareader.read we can see the records.But the datatable.load(datareader) loads only last record. The same code is working with mssql.

  • #767099
    Hi,

    As per my understanding about your previous comment, I suspect that the issue is in your datatable it "loads only last record" rather than load all the records right.

    Please ensure you r code looks like below to load all the records into datatable.


    public DataTable GetDataTable()
    {
    DataTable dataTable = new DataTable();
    using (SqlConnection conn = new SqlConnection("ConnectionString"))
    {
    SqlCommand cmd = conn.CreateCommand();
    cmd.CommandText = "procedure_name";
    cmd.CommandType = CommandType.StoredProcedure;

    if (conn.State != ConnectionState.Open)
    conn.Open();

    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

    dataTable.Load(dr);
    }
    return dataTable;
    }


    If still you face the problem then paste your piece of code so that we can help you better.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #767114
    you can load datatable using datareader, with following snippet

    conn = new SqlConnection(connString);
    string query = "SELECT * FROM Customers";
    SqlCommand cmd = new SqlCommand(query, conn);
    conn.Open();
    SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
    DataTable dt = new DataTable();
    dt.Load(dr);

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


Sign In to post your comments