Hi
I have upload my file and i am facing this error message when i try to add any new record with below code the error message is :
selectcommand.connection property has not been initialized
<pre>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 makervalue = string.Empty;
string Yearvalue = string.Empty;
string NyAdsDesc = TextBox2.Text;
SqlConnection cn = new SqlConnection(sc);
SqlCommand cmd = new SqlCommand();
var UsrNme = Session["UsrNme"];
if (Session["UsrNme"] != null && cookie != null)
{
cn.Open();
switch (Catedrdoads.SelectedItem.Text)
{
case "Cars":
makervalue = barndcardrlst1.SelectedValue;
Yearvalue = CarYearfrmDrDw.SelectedValue;
Gearvalue = DropDownList4.SelectedValue;
break;
case "Trucks":
makervalue = DropDownList9.SelectedValue;
Yearvalue = DropDownList12.SelectedValue;
Gearvalue = DropDownList10.SelectedValue;
break;
default:
break;
}
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)
{
string sqlstatment = @"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)";
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstatment;
cmd.Parameters.AddWithValue("@Section", Secdrdoads.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Maker", makervalue);
cmd.Parameters.AddWithValue("@Year", Yearvalue);
cmd.Parameters.AddWithValue("@Gear", Gearvalue);
cmd.Parameters.AddWithValue("@RoomNo", RoomNum);
cmd.Parameters.AddWithValue("@Space", areasize);
cmd.Parameters.AddWithValue("@Category", Catedrdoads.SelectedItem.Text);
string imgnouser = "/images/general/nouser.jpg";
if (FileUploadImg5.HasFile)
{
cmd.Parameters.AddWithValue("@Img5", FileUploadImg5.FileName);
FileUploadImg5.SaveAs(Server.MapPath("~/images/AdsImgs/" + FileUploadImg5.FileName));
}
else
{
cmd.Parameters.AddWithValue("@Img5", imgnouser);
}
}
else
{
AddNwAddsWrngFrmtLbl.Text = "Error: The file should have .png or .jpg format only";
AddNwAddsWrngFrmtLbl.ForeColor = System.Drawing.Color.Red;
}
}
else
{
string sqlstatment = @"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)";
cmd = new SqlCommand(sqlstatment);
cmd.Connection = cn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstatment;
cmd.Parameters.AddWithValue("@Section", Secdrdoads.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Maker", makervalue);
cmd.Parameters.AddWithValue("@Year", Yearvalue);
cmd.Parameters.AddWithValue("@Gear", Gearvalue);
DropDownList2.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Status", DropDownList3.SelectedItem.Text);
cmd.Parameters.AddWithValue("@adsDate", DateTime.Now);
}
SqlDataAdapter ad = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
ad.SelectCommand = cmd;
ad.Fill(ds);
MultiView1.ActiveViewIndex = 3;
ViwMyAdsPanel.Visible = true;
CheckUsrAds();
}
}