Click here to Skip to main content
16,004,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
dear frnds.
i have datatable and i am fill this datatable with the value of database table which have already a row in it but when i m load datatable using connected mode it give not me 1 row data to me, why


suggest me
my code is this



string sql = "select * from " + checkedListBox2.SelectedItem.ToString() + "";
Npgsql.NpgsqlCommand cmd = new Npgsql.NpgsqlCommand(sql, con);
C#
using (Npgsql.NpgsqlDataReader rdr = cmd.ExecuteReader())
                            {
                                while (rdr.Read())
                                {
                                   
                                    dbdt.Load(rdr)
}
 for (int b = 0; b < dbdt.Rows.Count; )
                                    {
 string str = dbdt.Rows[b][chklistindex2].ToString();

                                        if (strg == str)
                                        {
                                            MessageBox.Show("find");
                                        }
                                        else if (strg!=str)
                                            {
                                                b++;
 
                                            }                                        
                                        else
                                        {
                                            using (var conn = new Npgsql.NpgsqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["test"].ToString()))
                                            using (var cmd1 = conn.CreateCommand())
                                            {
                                                conn.Open();
                                                cmd1.CommandText = "insert into " + checkedListBox2.SelectedItem.ToString() + " ( " + checkedListBox2.SelectedItem.ToString() + " ) values('" + strg + "')";
                                                var result = cmd1.ExecuteNonQuery();
                                            }
                                        }
                                    }
                         


this is my code and my database table (f_name)


it have 2 column id and f_name
and value in it is 1 and kavita respectively on first index

but my above code dose not load this data into datatable


why if i am add a new row in database then it load the data form second row in datatable


pls help me
Posted

1 solution

Simple: You read (and thus discard) the first record in the reader when you call Read:
C#
while (rdr.Read())
{
    dbdt.Load(rdr);

Read reads a row, and removes it from the reader: so when you call Load the row is no longer available.
Try
C#
if (rdr.HasRows)
   {
   dbtb.Load(rdr);

But please, don't do things like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900