PDA

View Full Version : خطای ExecuteScalar: CommandText property has not been initialized



دلتنگ اسمان
یک شنبه 22 تیر 1393, 19:03 عصر
سلام
میخوام ماکزیمم عدد موجود در ستونی رو بخونم با این کد:sql2008 ,vs2008

SqlConnection con = new SqlConnection();
SqlCommand com = new SqlCommand();
con.ConnectionString = "server=localhost;Integrated security=true;database=matab";
com.Connection = con;
con.Open();

string sql = "select max(numfile) from personal";
numfileLbl.Text = (((int)com.ExecuteScalar() + 1).ToString());

com.CommandText = sql;
com.Connection = con;


ولی خطا میده. توی اکسس از این کد استفاده میکنم و مشکلی هم ندارم .

fakhravari
یک شنبه 22 تیر 1393, 21:43 عصر
cmd.CommandText="SELECT COUNT(*) FROM (SELECT * FROM (SELECT * FROM EMPLOYEE ) as T1) as T2";
Records=int.Parse(cmd.ExecuteScalar().ToString());

s.karim
دوشنبه 23 تیر 1393, 12:12 عصر
به جای دستور select ات که نوشتی اینو بنویس شاید جواب داد:


SELECT
TOP 1 ISNULL(numfile,0) + 1
FROM
personal
ORDER BY
numfile DESC