-1
填寫錯誤我真的努力了很多知道哪裏是錯這個下面的代碼,展示給我的錯誤信息:經與連接
「填寫:selectcommand.connection屬性尚未初始化」
click to see the error image 但我不能真正得到錯誤變成下面的代碼,請你能不能幫我解決這個代碼,並避免出現此錯誤消息
protected void addadsbtn_Click(object sender, EventArgs e)
{
HttpCookie cookie = Request.Cookies.Get("Location");
string Location = string.Empty;
var user = Session["UsrNme"];
Location = cookie.Value;
string FileExtentio = System.IO.Path.GetExtension(FileUploadImg1.FileName);
string FileExtentio2 = System.IO.Path.GetExtension(FileUploadImg2.FileName);
string makervalue = string.Empty;
string Gearvalue = string.Empty;
string NyAdsDesc = TextBox2.Text;
SqlConnection addadscon = new SqlConnection(sc);
var UsrNme = Session["UsrNme"];
if (Session["UsrNme"] != null && cookie != null)
{
}
if (FileUploadImg1.HasFile || FileUploadImg2.HasFile || FileUploadImg3.HasFile || FileUploadImg4.HasFile || FileUploadImg5.HasFile)
{
var filess = new[] { FileExtentio, FileExtentio2, FileExtentio3, FileExtentio4, FileExtentio5 };
filess = filess.Where(s => !string.IsNullOrEmpty(s)).ToArray();
var extensions = new[] { ".jpg", ".png" };
if ((filess.Except(extensions).Count()) <= 0)
{
if (DropDownList3.SelectedValue == "no")
{
AdsWrngPanel.Visible = true;
adsstutslbel.Text = "- Please select ads status";
}
else
{
}
if (DropDownList2.SelectedValue == "no")
{
AdsWrngPanel.Visible = true;
adscondlbel.Text = "- Please select ads condition";
}
else
{
}
SqlCommand cmd = new SqlCommand(@"INSERT INTO [ads] ([Section], [Category], [UID], [AdsTit], [AdsDesc], [Country], [State],[AdsDate],
[City], [AdsPrice], [Img1], [img2], [img3], [img4],[img5], [Wtags], [Address],[Condition], [Status],[Maker],[Year],[Gear],[RoomNo],
[Space],[Shower],[Garage],[Currency])
VALUES (@Section, @Category, @UID, @AdsTit, @AdsDesc, @Country, @State,@adsDate, @City, @AdsPrice, @Img1, @img2, @img3,
@img4, @img5, @Wtags, @Address,@Condition, @Status,@Maker,@Year,@Gear,@RoomNo,@Space,@Shower,@Garage,@Currency)", addadscon);
cmd.Parameters.AddWithValue("@Section", Secdrdoads.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Maker", makervalue);
cmd.Parameters.AddWithValue("@Year", Yearvalue);
string imgnouser = "/images/general/nouser.jpg";
if (FileUploadImg1.HasFile)
{
cmd.Parameters.AddWithValue("@Img1", FileUploadImg1.FileName);
FileUploadImg1.SaveAs(Server.MapPath("~/images/AdsImgs/" + FileUploadImg1.FileName));
}
else
{
cmd.Parameters.AddWithValue("@Img1", imgnouser);
}
addadscon.Open();
cmd.ExecuteNonQuery();
addadscon.Close();
}
else
{
AddNwAddsWrngFrmtLbl.Text = "Error: The file should have .png or .jpg format only";
AddNwAddsWrngFrmtLbl.ForeColor = System.Drawing.Color.Red;
}
}
else
{
SqlCommand cmd3 = new SqlCommand(@"INSERT INTO [ads] ([Section], [Category], [UID], [AdsTit], [AdsDesc], [Country], [State],[AdsDate],
[City], [AdsPrice], [Wtags], [Address],[Condition], [Status],[Maker],[Year],[Gear],[RoomNo], [Space],[Shower],[Garage])
VALUES (@Section, @Category, @UID, @AdsTit, @AdsDesc, @Country, @State,@adsDate, @City, @AdsPrice, @Wtags, @Address,@Condition, @Status,@Maker,@Year,@Gear,@RoomNo,@Space,@Shower,@Garage)", addadscon);
cmd3.Parameters.AddWithValue("@Wtags", addadswtagtxtbtn.Text);
cmd3.Parameters.AddWithValue("@Condition", DropDownList2.SelectedItem.Text);
cmd3.Parameters.AddWithValue("@Status", DropDownList3.SelectedItem.Text);
cmd3.Parameters.AddWithValue("@adsDate", DateTime.Now);
addadscon.Open();
cmd3.ExecuteNonQuery();
addadscon.Close();
}
MultiView1.ActiveViewIndex = 3;
ViwMyAdsPanel.Visible = true;
CheckUsrAds();
}
}
您需要告訴命令對象使用哪個連接。試試這個:'cmd.Connection = addadscon;' –
@ LasseV.Karlsen它已被告知作爲第二個參數在'new SqlCommand(...'部分實際上。 –
在哪條線上你完全得到這個錯誤?我覺得你像因爲在你的代碼中沒有「selectcommand」部分,所以不要顯示相關的代碼 –