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

  • Home
  • SEARCH
  • 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 999119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:18:02+00:00 2026-05-16T07:18:02+00:00

i need to get index of a value in DataTable. I’m trying to get

  • 0

i need to get index of a value in DataTable.

I’m trying to get like that.

 SqlDataAdapter da = new SqlDataAdapter("SELECT MessageID,SenderID,MessageContent FROM Messages WHERE ThreadID="+ThreadID, connectionString);
 //Get all messages in the Thread.
 DataTable dt = new DataTable();
 da.Fill(dt);

 da.SelectCommand.CommandText = "SELECT MessageID,SenderID,MessageContent FROM Messages WHERE MessageID="+MessageID;
 //Get the message which i need to get index.
 DataTable dtMsg = new DataTable();
 da.Fill(dtMsg);
 //Get index of dtMsg.Rows[0] in dt.
 int msgIndex = dt.Rows.IndexOf(dtMsg.Rows[0]);

I analized it when debugging values are same but its returning -1 everytime.
What i can do?

  • 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-16T07:18:03+00:00Added an answer on May 16, 2026 at 7:18 am

    You are looking for a row in datatable dt when the row is actually in datatable dtMsg….

    Try:

    int msgIndex = dtMsg.Rows.IndexOf(dtMsg.Rows[0]);
    

    Actually that is always going to return zero anyway as you are referencing the row by index anyway.

    If what you actually want is to find a row in dt based on a value in a dtMsg row you will need to use something like Find() or Select().

    Heres’s some sample code:

            // Create test data table with messageid as primary column
            DataTable dt = new DataTable();
            dt.Columns.Add("MessageID", typeof (int));
            dt.Columns.Add("SenderID", typeof(int));
            dt.Columns.Add("MessageContent", typeof(string));
            dt.PrimaryKey = new[] {dt.Columns["MessageID"]};
    
            // Add some data
            dt.Rows.Add(1, 10, "Message1");
            dt.Rows.Add(2, 11, "Message2");
            dt.Rows.Add(3, 12, "Message3");
            dt.Rows.Add(4, 13, "Message4");
    
            // Create second test data table with single row
            DataTable dtMsg = new DataTable();
            dtMsg.Columns.Add("MessageID", typeof(int));
            dtMsg.Columns.Add("SenderID", typeof(int));
            dtMsg.Columns.Add("MessageContent", typeof(string));
            dtMsg.PrimaryKey = new[] { dtMsg.Columns["MessageID"] };
    
            dtMsg.Rows.Add(3, 12, "Message3");
    
            // Not very elegant way of getting the message id from dtMsg. 
            int messageId = (int)dtMsg.Rows[0][0];
    
            int index = dt.Rows.IndexOf(dt.Rows.Find(messageId));
    
            // Result : index is 2
            Console.WriteLine(index);
    

    This assumes that MessageId is the primary index on the table.

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

Sidebar

Related Questions

How to get checked RadioButton Index using jQuery? I need to read it and
I have linq request. I need get item.Title in select. how do this? var
I have two tables with a weak relation. I need get a text value
I need to get key/value pairs from XML to populate member info on a
When I make my httppost call, i need to get the selected value and
I have a array in PHP (like example) and I need to get the
I need to get a record of value and timestamp by the max of
I don't need to sort listView I only need to get highest value from
I'm on RedHat, I need to get the value in the Release field. Take
I need get the id of an images which is place inside a datatemplate..

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.