SQL Server error: ExecuteNonQuery: Connection property has not been initialized

淺唱寂寞╮ 提交于 2019-12-09 04:38:41

You haven't associated your command cmd with your SqlConnection, that is why you are getting the error.

You need to specify:

cmd.Connection = con;

in your submitdata() method.

Since SqlCommand implements IDisposable, its better if you use it within using block like:

using (SqlCommand cmd = new SqlCommand())
{

    cmd.Parameters.Clear();
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "insertdata";
    cmd.Connection = con;
    .... your code

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!