Click here to Skip to main content
15,883,705 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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




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

            }


        }
Posted

1 solution

cmd.Connection = cn;
Add this line.

-KR
 
Share this answer
 
Comments
Member 10690878 15-Nov-15 6:52am    
Hi @KrunalRohit but i have already added this one into code you can check it again after each string sqlstatment
Krunal Rohit 15-Nov-15 10:16am    
Do this way -
SqlCommand cmd = new SqlCommand(sqlstatment, cn);

-KR
Member 10690878 15-Nov-15 11:36am    
I do as you mention like :

SqlCommand cmd = new SqlCommand(sqlstatment, cn);
cmd.CommandType = CommandType.Text;
cmd.CommandText = sqlstatment;

cmd.Parameters.AddWithValue("@Section", Secdrdoads.SelectedItem.Text);
cmd.Parameters.AddWithValue("@Maker", makervalue);

but i get error message: Severity Code Description Project File Line
A local or parameter named 'cmd' cannot be declared in this scope because that name is used in an enclosing local scope to define a local or parameter

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900