Thursday, August 14, 2025

System.InvalidOperationException: ExecuteScalar: Connection property has not been initialized.

 This error comes when you write below code:-


using (SqlCommand cmd=new SqlCommand("SELECT TOP 1 [FileID]  FROM [UploadFiles] order by Fileid desc"))


and not passed con as second parameter in SqlCommand. Use below code

using (SqlCommand cmd=new SqlCommand("SELECT TOP 1 [FileID]  FROM [UploadFiles] order by Fileid desc", con))


also you have to add below line

cmd.CommandType = System.Data.CommandType.Text;

No comments:

Post a Comment