连接出现填充错误

本文关键字:错误 填充 连接 | 更新日期: 2023-09-27 18:23:46

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

"fill:selectcommand.connection属性尚未初始化"

单击查看错误图像但我真的无法将错误写入下面的代码中,请你帮我修复这个代码并避免这个错误消息

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();

        }

    }

连接出现填充错误

根据您发布的异常屏幕截图,我认为问题源于以下事件:

beravoSV.User_panel.adNewdadsbtn_Click(...)

因此,在该事件(包括从该事件调用的方法)内部,检查与DataAdapter相关的代码。例如:

SqlDataAdapter adapter = new SqlDataAdapter(queryString, connection);

并确保将正确的CCD_ 4传递给数据适配器实例。

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