0
    string j = "Data Source=FUJITSU-PC\\SQLEXPRESS;Initial Catalog=attendance_system;Integrated Security=True";
    DataTable dt = new DataTable();
    using (SqlConnection cn = new SqlConnection(j))
    {
        SqlCommand cmd = new SqlCommand("select dbo.faculty_status.username from dbo.faculty_status", cn);
        cn.Open();
        SqlDataReader dr = cmd.ExecuteReader();
        dr.Read();
        dt.Load(dr);
        cn.Close();
    }
    int x = dt.Rows.Count;
    foreach (DataRow row in dt.Rows)
    {
        foreach (var item in row.ItemArray)
        {
            string xx = item.ToString();
        }
    }

MySQL server database table

I used this code it wors but it does not show me my desire first row value it givs me out put like----

13-24516-2

6
  • what is the error? Commented May 9, 2016 at 9:59
  • what are you doing with xx? Since it's local variable, it could be getting overwritten with the second row? Commented May 9, 2016 at 10:02
  • i wants to print my value using xx variable Commented May 9, 2016 at 10:07
  • I don't see that code here. Can you edit the post and give that code? Commented May 9, 2016 at 10:10
  • @KrishnaChaithanyaMuthyala ....my code---- Commented May 9, 2016 at 10:27

3 Answers 3

1

I think the problem is the way you are reading. Try this:

    string j = "Data Source=FUJITSU-PC\\SQLEXPRESS;Initial Catalog=attendance_system;Integrated Security=True";
    using (SqlConnection cn = new SqlConnection(j))
    {
        SqlCommand cmd = new SqlCommand("select dbo.faculty_status.username from dbo.faculty_status", cn);
        cn.Open();
        using (var reader = cmd.ExecuteReader())
        {
            while(reader.Read())
            {
                Console.WriteLine(reader["username"]);
            }
        }
    }

Let me know if it works!

Sign up to request clarification or add additional context in comments.

1 Comment

good to know...and please try to use stored procedures instead of direct commands like these...
0

It gives you the last row. the XX string is first loaded with the first results, afterwards on the second row it overwrites the first row with the second row, this goes on as long as there is rows.

if you want the first rows the be displayed you could use somthing like:

    DataTable dt = new DataTable();
    string xx = dt.Rows[0].ToString();

or

foreach (DataRow row in dt.Rows)
{
        foreach (var item in row.ItemArray)
        {
            Console.WriteLine(item.ToString());
        }
}

dt.Rows[the row you want to retrieve]

1 Comment

its not work.still it gives me second row..after second it works fine..it give me 3,4,5 and so on
0

Try this code example:-

SqlCommand cmd = new SqlCommand("select dbo.faculty_status.username from dbo.faculty_status", cn);
DataTable dt = new DataTable();
SqlDataAdapter mda = new SqlDataAdapter(cmd);
mda.Fill(dt);

Let me know if it works!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.