0

这是我的代码。错误消息是

未提供参数 @event_image

如何将图像名称作为参数发送?如果我保留断点,它甚至不会进入If(fileupload1.HasFile).

我必须将图像存储在文件夹中,并且路径必须存储到 sql db

{
        conn.Open();
        string postdate = txtpostdate.Text;
        string unpostdate = txtunpostdate.Text;
        string name = txtname.Text;
        string description = txtdescription.Text;
        string country = dropcountry.SelectedItem.ToString();
        string multidate = txtstartdae.Text;
        string startend = dropstarttime.SelectedItem.ToString();
        string drop1m = dropti1m.SelectedItem.ToString();
        string dropme1h = droptimeend1h.SelectedItem.ToString();
        string drop2m = droptime2m.SelectedItem.ToString();
        string notes = txtnotes.Text;
        string prevlocation = droplocation.SelectedItem.ToString();
        string locationname = txtslocname.Text;
        string addres1 = txtsLocAddress1.Text;
        string addres2 = txtsLocAddress2.Text;
        string city = txtsLocCity.Text;
        string state = dropstate.SelectedItem.ToString();
        string zipcode = txtsLocZip.Text;
        string phonenumber = txtsLocPhone.Text;
        string faxnumber = txtsLocFax.Text;
        string notes2 = textnotes2.Text;
        SqlCommand cmd = new SqlCommand("InsertEvents", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        //SqlCommand cmd = new SqlCommand("insert into pa_events(event_postdate,event_unpostdate,event_canvisitorsregisters,event_eventname,event_description,event_image,event_multydateevent,event_startdate,event_enddate,event_start,event_end,event_notes,location_name,location_addres1,location_addres2,location_cites,location_state,location_zipcode,location_phonenumber,location_faxnumber,location_notes,event_country) values(@event_postdate,@event_unpostdate,@event_canvisitorsregisters,@event_eventname,@event_description,@event_image,@event_multydateevent,@event_startdate,@event_enddate,@event_start,@event_end,@event_notes,@location_name,@location_addres1,@location_addres2,@location_cites,@location_state,@location_zipcode,@location_phonenumber,@location_faxnumber,@location_notes,@event_country)", conn);

        if (fileupload1.HasFile)
        {
            imagename = fileupload1.FileName;

            int length = fileupload1.PostedFile.ContentLength;
            fileupload1.SaveAs(Server.MapPath("~\\images\\" + imagename));

             s = "~\\images\\" + imagename + "";
        }              


            if (radioyes.Checked == false && !radiono.Checked == false)
            {
                lblmsg.Text = "Please Select canve Register Yes! or No!";
            }

            if (radioyes.Checked == true)
            {
                cmd.Parameters.AddWithValue("@event_canvisitorsregisters", SqlDbType.VarChar).Value = "Y";
            }
            else
            {
                cmd.Parameters.AddWithValue("@event_canvisitorsregisters", SqlDbType.VarChar).Value = "N";
            }


            if (radiomultyyes.Checked == true)
            {
                cmd.Parameters.AddWithValue("@event_multydateevent", SqlDbType.VarChar).Value = "Y";
            }
            else
            {
                cmd.Parameters.AddWithValue("@event_multydateevent", SqlDbType.VarChar).Value = "N";
            }

            **cmd.Parameters.AddWithValue("@event_image", s);**
            cmd.Parameters.AddWithValue("@event_postdate", postdate);
            cmd.Parameters.AddWithValue("@event_unpostdate", unpostdate);
            cmd.Parameters.AddWithValue("@event_eventname", name);
            cmd.Parameters.AddWithValue("@event_description", description);
            cmd.Parameters.AddWithValue("@event_country", country);
            cmd.Parameters.AddWithValue("@event_startdate", startend);
            cmd.Parameters.AddWithValue("@event_enddate", drop1m);
            cmd.Parameters.AddWithValue("@event_start", dropme1h);
            cmd.Parameters.AddWithValue("@event_end", drop2m);
            cmd.Parameters.AddWithValue("@event_notes", notes);
            cmd.Parameters.AddWithValue("@location_name", locationname);
            cmd.Parameters.AddWithValue("@location_addres1", addres1);
            cmd.Parameters.AddWithValue("@location_addres2", addres2);
            cmd.Parameters.AddWithValue("@location_cites", city);
            cmd.Parameters.AddWithValue("@location_state", state);
            cmd.Parameters.AddWithValue("@location_zipcode", zipcode);
            cmd.Parameters.AddWithValue("@location_phonenumber", phonenumber);
            cmd.Parameters.AddWithValue("@location_faxnumber", faxnumber);
            cmd.Parameters.AddWithValue("@location_notes", notes2);
            //if (fileupload1.PostedFile != null && fileupload1.PostedFile.FileName != "")
            //
            int i = cmd.ExecuteNonQuery();
            if (i != 0)
            {
                lblmsg.Text = "record is inserted";
            }
            else
            {
                lblmsg.Text = "record is not inserted";
            }
            conn.Close();    
    }
4

2 回答 2

0

该错误与您的 SQL 插入无关,而是与您的文件上传设置有关。您收到该错误是因为,正如您所说,您甚至没有输入创建 SQL 参数的 if 语句。

你能发布与图片上传相关的代码吗?那是你的根本问题。

于 2013-09-06T14:46:18.827 回答
0

老实说,您的代码尚不清楚,但希望它适用于您:添加文件上传参数

if (fileupload1.HasFile)
    {
        imagename = fileupload1.FileName;

        int length = fileupload1.PostedFile.ContentLength;
        fileupload1.SaveAs(Server.MapPath("~\\images\\" + imagename));

         s = "~\\images\\" + imagename + "";
        cmd.Parameters.AddWithValue("@event_image", s);
   }  
于 2013-09-06T15:01:48.433 回答