fill:selectcommand.connection属性尚未初始化 [英] fill: selectcommand.connection property has not been initialized

查看:134
本文介绍了fill:selectcommand.connection属性尚未初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我上传了我的文件,当我尝试使用以下代码添加任何新记录时,我遇到此错误消息错误消息为:

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();
                // Set your values based on your selection
                switch (Catedrdoads.SelectedItem.Text)
                {
                    case "Cars":
                        // Set your values
                        makervalue = barndcardrlst1.SelectedValue;
                        Yearvalue = CarYearfrmDrDw.SelectedValue;
                        Gearvalue = DropDownList4.SelectedValue;
                        break;
                    case "Trucks":
                        // Set your values
                        makervalue = DropDownList9.SelectedValue;
                        Yearvalue = DropDownList12.SelectedValue;
                        Gearvalue = DropDownList10.SelectedValue;
                        break;


                    default:
                        // Consider throwing an error here, none of your options were selected
                        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();

            }


        }

推荐答案

cmd.Connection = cn;

添加此行。



-KR
cmd.Connection = cn;
Add this line.

-KR


这篇关于fill:selectcommand.connection属性尚未初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆