连接时出现填充错误

时间:2015-11-22 09:28:29

标签: c#

我真的尝试了很多,知道下面代码中的错误在哪里向我显示错误消息:

  

“fill: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();


        }


    }

1 个答案:

答案 0 :(得分:0)

除了评论之外,根据您发布的例外截图,我认为问题源于以下事件:

beravoSV.User_panel.adNewdadsbtn_Click(...)

因此在此事件中(包括从此事件调用的方法)检查​​DataAdapter相关代码。例如:

SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

并确保将正确的connection传递给dataadapter实例。

我认为应该解决这个问题。