我的连接出现错误
本文关键字:错误 连接 我的 | 更新日期: 2023-09-27 18:14:38
我知道这个问题已经重复了很多,但我真诚地试图修复它并找到错误所在,但我找不到它并解决它,所以这就是为什么我在这里发布我的问题。这个代码的问题是它给我错误信息:
ExecuteNonQuery: Connection属性未初始化
在代码的末尾。有人能帮我一下吗?
protected void editHPPersInfobtn_Click(object sender, EventArgs e)
{
SqlConnection EdPersInfoCon = new SqlConnection(sc);
SqlCommand EditUsrInfoCMD = new SqlCommand();
var UsrNme = Session["UsrNme"];
string FileExtentiona = System.IO.Path.GetExtension(EditImgFUP.FileName);
string FileExtentiona2 = System.IO.Path.GetExtension(EditPersInfologoFileUp.FileName);
string FileExtentiona3 = System.IO.Path.GetExtension(EditPesInfoBizImg1FU.FileName);
string FileExtentiona4 = System.IO.Path.GetExtension(EditPesInfoBizImg2FU.FileName);
string FileExtentiona5 = System.IO.Path.GetExtension(EditPesInfoBizImg3FU.FileName);
if (Session["UsrNme"] != null)
{
if (EditImgFUP.HasFile || EditPersInfologoFileUp.HasFile || EditPesInfoBizImg1FU.HasFile || EditPesInfoBizImg2FU.HasFile || EditPesInfoBizImg3FU.HasFile)
{
var files = new[] { FileExtentiona, FileExtentiona2, FileExtentiona3, FileExtentiona4, FileExtentiona5 };
files = files.Where(s => !string.IsNullOrEmpty(s)).ToArray();
var extensions = new[] { ".jpg", ".png" };
if ((files.Except(extensions).Count()) <= 0)
{
if (EditImgFUP.PostedFile.ContentType.ToLower().StartsWith("image/") ||
EditPersInfologoFileUp.PostedFile.ContentType.ToLower().StartsWith("image/") ||
EditPesInfoBizImg1FU.PostedFile.ContentType.ToLower().StartsWith("image/") ||
EditPesInfoBizImg2FU.PostedFile.ContentType.ToLower().StartsWith("image/") ||
EditPesInfoBizImg3FU.PostedFile.ContentType.ToLower().StartsWith("image/"))
{
string EditUsrInfoSQL = @"Update UserInfo SET FN=@FN, LN=@LN, Password=@Password, RePass=@RePass, Website=@Website, Post=@Post, Email=@Email, Address=@Address, TeleNum=@TeleNum, Facebook=@Facebook, GooglePlus=@GooglePlus, Twitter=@Twitter, Img=@Img, Logo=@Logo, image1=@image1, image2=@image2, image3=@image3 Where UID=@UID";
EditUsrInfoCMD.Connection = EdPersInfoCon;
EditUsrInfoCMD.CommandType = CommandType.Text;
EditUsrInfoCMD.CommandText = EditUsrInfoSQL;
EditUsrInfoCMD.Parameters.AddWithValue("@UID", UsrNme);
EditUsrInfoCMD.Parameters.AddWithValue("@FN", FNEDTTXTBX.Text);
//...
string imgnouser = "/images/general/nouser.jpg";
string[] imagesUI = GetImagess(Convert.ToString(UsrNme));
if (EditImgFUP.HasFile)
{
EditUsrInfoCMD.Parameters.AddWithValue("@Img", EditImgFUP.FileName);
EditImgFUP.SaveAs(Server.MapPath("~/images/users/" + EditImgFUP.FileName));
}
else
{
if (string.IsNullOrEmpty(imagesUI[0]))
{
EditUsrInfoCMD.Parameters.AddWithValue("@Img", imgnouser);
}
else
{
EditUsrInfoCMD.Parameters.AddWithValue("@Img", imagesUI[0]);
}
}
}
}
else
{
EditPersInfoImgFrmtWarnLbl.Text = "Error: The file should have .png or .jpg format only";
EditPersInfoImgFrmtWarnLbl.ForeColor = System.Drawing.Color.Red;
EditUsrPan.Visible = true;
}
}
else
{
string EditUsrInfoSQL = @"Update UserInfo SET FN=@FN, LN=@LN, Password=@Password, RePass=@RePass, Website=@Website, Post=@Post, Email=@Email, Address=@Address, TeleNum=@TeleNum, Facebook=@Facebook, GooglePlus=@GooglePlus, Twitter=@Twitter Where UID=@UID";
EditUsrInfoCMD.Connection = EdPersInfoCon;
EditUsrInfoCMD.CommandType = CommandType.Text;
EditUsrInfoCMD.CommandText = EditUsrInfoSQL;
EditUsrInfoCMD.Parameters.AddWithValue("@UID", UsrNme);
EditUsrInfoCMD.Parameters.AddWithValue("@FN", FNEDTTXTBX.Text);
EditUsrInfoCMD.Parameters.AddWithValue("@LN", LNEDTTXTBX.Text);
EditUsrInfoCMD.Parameters.AddWithValue("@Password", PASSEDTTXTBX.Text);
//...
}
EdPersInfoCon.Open();
int result = EditUsrInfoCMD.ExecuteNonQuery();
if (result > 0)
{
// Updated successfully;
}
EditUsrPan.Visible = false;
ReHPPersInfo();
showusrinfo.Visible = true;
}
}
在您的if...elses
中有两个分支,您不将连接分配给命令:
if (Session["UsrNme"] != null)
{
if (EditImgFUP.HasFile || EditPersInfologoFileUp.HasFile || EditPesInfoBizImg1FU.HasFile || EditPesInfoBizImg2FU.HasFile || EditPesInfoBizImg3FU.HasFile)
{
if ((files.Except(extensions).Count()) <= 0)
{
if (....)
{
EditUsrInfoCMD.Connection = EdPersInfoCon;
EditUsrInfoCMD.CommandType = CommandType.Text;
EditUsrInfoCMD.CommandText = EditUsrInfoSQL;
EditUsrInfoCMD.Parameters.AddWithValue("@UID", UsrNme);
EditUsrInfoCMD.Parameters.AddWithValue("@FN", FNEDTTXTBX.Text);
}
// !!! HERE !!!
}
else
{
// !!! HERE !!!
EditPersInfoImgFrmtWarnLbl.Text = "Error: The file should have .png or .jpg format only";
EditPersInfoImgFrmtWarnLbl.ForeColor = System.Drawing.Color.Red;
EditUsrPan.Visible = true;
}
}
else
{
string EditUsrInfoSQL = @"Update UserInfo SET FN=@FN, LN=@LN, Password=@Password, RePass=@RePass, Website=@Website, Post=@Post, Email=@Email, Address=@Address, TeleNum=@TeleNum, Facebook=@Facebook, GooglePlus=@GooglePlus, Twitter=@Twitter Where UID=@UID";
EditUsrInfoCMD.Connection = EdPersInfoCon;
EditUsrInfoCMD.CommandType = CommandType.Text;
EditUsrInfoCMD.CommandText = EditUsrInfoSQL;
}
为什么不在初始化SqlCommand
的地方赋值呢?
SqlConnection EdPersInfoCon = new SqlConnection(sc);
SqlCommand EditUsrInfoCMD = new SqlCommand();
EditUsrInfoCMD.Connection = EdPersInfoCon;