Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
what is the problem with these codes please help

C#
string conStr = ConfigurationManager.ConnectionStrings["CStringCRM"].ToString();
            DataTable dt = new DataTable();
            SqlDataReader dr = null;
            using(SqlConnection conn = new SqlConnection(conStr))
            {
                string sql = string.Format(@"SELECT top 1 ID FROM User order by ID desc"); 
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = conn;
                cmd.CommandText = sql;
                cmd.CommandType = CommandType.Text;
                conn.Open();

                dr = cmd.ExecuteReader();

                dt.Load(dr);
                txtUID.Text = dr["ID"].ToString();
                conn.Close();
                cmd = null;

            }
Posted
Updated 31-Jul-15 2:53am
v2

Here's a fixed version of your code:
C#
string conStr = ConfigurationManager.ConnectionStrings["CStringCRM"].ToString();

using (SqlConnection conn = new SqlConnection(conStr))
using (SqlCommand cmd = new SqlCommand(@"SELECT top 1 ID FROM User order by ID desc", conn))
{
    conn.Open();
    using (SqlDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection))
    {
        if (dr.Read())
        {
            txtUID.Text = Convert.ToString(dr["ID"]);
        }
    }
}

  1. Consistent column name, as suggested by CPallini;
  2. Removed the un-used DataTable and dt.Load(dr), as suggested by CPallini and CodeMaster_Noob;
  3. Wrapped the SqlDataReader in a using block, and told it to close the connection when finished;
  4. Added the required dr.Read() call before attempting to access values from the SqlDataReader;
  5. Replaced dr["ID"].ToString() with Convert.ToString(dr["ID"]), in case the column value is null;
 
Share this answer
 
Comments
Altaful Haq 3-Aug-15 0:28am    
Thanks a lot Richard Deeming love your working with explanation...
Altaful Haq 3-Aug-15 0:49am    
Can I increment that value by if yes than help me..
Richard Deeming 3-Aug-15 7:56am    
Sorry, I don't understand your comment.
Altaful Haq 4-Aug-15 2:00am    
in the above solution we get a value and assign it into the textbox e.g the value is 7 now i want to make that value 8... how would i do so.
Richard Deeming 4-Aug-15 7:20am    
Assuming the ID column is an integer, something like this should work:
int id = Convert.ToInt32(dr["ID"]);
txtUID.Text = (id + 1).ToString();
Quote:
string sql = string.Format(@"SELECT top 1 ID FROM User order by ID desc");

Quote:
txtUID.Text = dr["UID"].ToString();

I see a mismatch...


Please note, the DataTable is useless in your code.
 
Share this answer
 
Comments
Altaful Haq 31-Jul-15 8:55am    
Still Not Working with txtUID.Text = dr["ID"].ToString();

#CPallini
CPallini 31-Jul-15 8:58am    
In order to ge better help you could report the exact error, if any ("not working" isn't much informative).
[no name] 31-Jul-15 9:07am    
Just comment the line " dt.Load(dr);" in your code and make sure that you have records in your database to show up in text box.

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