Click here to Skip to main content
15,894,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
DataTable dt3 = new DataTable();
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
            using (SqlConnection con = new SqlConnection(CS))
            {
                string sqlstr2 = "select max(catId) from tblcatProducts";
                using (SqlDataAdapter dr2 = new SqlDataAdapter(sqlstr2, CS))
                {

                    dr2.Fill(dt3);
                    txtCatIdProducts.Text = dt3.ToString();
Posted

1 solution

This:
C#
dt3.ToString();

will just give you the String-representation of the whole DataTable, something like "System.Data.DataTable". Not any of the values therein.

Since you're querying for a single value, it will be in row 0, column 0:
C#
txtCatIdProducts.Text = dt3.Rows[0][0].ToString();

Whenever querying for single values you should use ExecuteScalar[^] which is meant for exactly that, instead of an SqlDataAdapter.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Mar-15 0:34am    
5ed.
—SA
[no name] 21-Mar-15 6:11am    
Thank you, Sergey!

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