简体   繁体   English

“参数无效。”

[英]'Parameter is not valid.'

I am working on WinForms using C#. I made a class video that has method yourvideos() .我正在使用 C# 处理 WinForms。我制作了一个 class 视频,其方法yourvideos() This method reads data from SQL database and add it in imagelist and listview .此方法从 SQL 数据库中读取数据并将其添加到imagelistlistview中。

First I have uploaded the image and then take the location of image using:首先我上传了图像,然后使用以下方法获取图像的位置:

var=open.FileName.ToString();

Then in a function uploadvideo() , I converted image to bytearray and inserted it to database:然后在 function uploadvideo()中,我将图像转换为 bytearray 并将其插入到数据库中:

public void uploadvideo(string url, string path, string name,string title, DateTime date, string imgloc, string user)
{
    con.Open();
    FileStream file = new FileStream(imgloc,FileMode.Open,FileAccess.Read);
        BinaryReader br = new BinaryReader(file);
        byte[] img = br.ReadBytes((int)file.Length);
   cmd = new SqlCommand($"insert into RecipeInfo (RecipeName, [Recipe Link],Thumbnail,Username, Date, Title) values (@Name,@Path,@img,@Username,@Date,@Title)", con);
                cmd.Parameters.AddWithValue("Name", name);
                cmd.Parameters.AddWithValue("Path", path);
                cmd.Parameters.AddWithValue("Img", img);
                cmd.Parameters.AddWithValue("Username", user);
                cmd.Parameters.AddWithValue("Date", date);
                cmd.Parameters.AddWithValue("Title", title);
                cmd.ExecuteNonQuery();
}

and then the function yourvideos() where I am retrieving data comes.然后是我正在检索数据的 function yourvideos()

public void yourvideos(string user, ImageList imglist, ListView list, Label l)
{
     cmd = new SqlCommand("select Title, Thumbnail from RecipeInfo where Username=@username", con);
            cmd.Parameters.AddWithValue("username", user);
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    if (reader.HasRows)
    {
        while (reader.Read())
        {
            byte[] img = (byte[])(reader["Thumbnail"]);
            MemoryStream ms = new MemoryStream(img);
            imglist.Images.Add(Image.FromStream(ms));
            list.Items.Add(reader["Title"].ToString());
            //MessageBox.Show("Data presesnt");
        }
    }
    else
    {

        l.Visible = true;
        l.Text = "Upload videos and share your recipes with others";
    }
    reader.Close();
    con.Close();
}

This function is called in the Form under the button click event这个function在按钮点击事件下的Form中调用

v1.yourvideos(Form2.setname, imageList1, yourvideoslistview, messagelabel);

It is giving me the error when I call yourvideos() that 'parameter is not valid' error details are:当我调用 yourvideos() 时出现“参数无效”错误详细信息是:

System.ArgumentException
  HResult=0x80070057
  Message=Parameter is not valid.
  Source=System.Drawing
  StackTrace:
   at System.Drawing.Image.FromStream(Stream stream, Boolean useEmbeddedColorManagement, Boolean validateImageData)
   at System.Drawing.Image.FromStream(Stream stream)
   at loginpage.video.yourvideos(String user, ImageList imglist, ListView list, Label l) in C:\Users\maha javed\source\repos\loginpage\Recipe.cs:line 115
   at loginpage.Form3.xuiSuperButton2_Click(Object sender, EventArgs e) in C:\Users\maha javed\source\repos\loginpage\Form3.cs:line 118
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at loginpage.Program.Main() in C:\Users\maha javed\source\repos\loginpage\Program.cs:line 19

  This exception was originally thrown at this call stack:
    [External Code]
    loginpage.video.yourvideos(string, System.Windows.Forms.ImageList, System.Windows.Forms.ListView, System.Windows.Forms.Label) in Recipe.cs
    loginpage.Form3.xuiSuperButton2_Click(object, System.EventArgs) in Form3.cs
    [External Code]
    loginpage.Program.Main() in Program.cs

From documentation on [this page][1] .来自[this page][1]上的文档。 This is example of how you should add parameters to SqlCommand object:这是应如何向SqlCommand object 添加参数的示例:

string demoXml, string connectionString)
{
    // Update the demographics for a store, which is stored
    // in an xml column.
    string commandText = "UPDATE Sales.Store SET Demographics = @demographics "
        + "WHERE CustomerID = @ID;";
using (SqlConnection connection = new SqlConnection(connectionString))
{
    SqlCommand command = new SqlCommand(commandText, connection);
    command.Parameters.Add("@ID", SqlDbType.Int);
    command.Parameters["@ID"].Value = customerID;

    // Use AddWithValue to assign Demographics.
    // SQL Server will implicitly convert strings into XML.
    command.Parameters.AddWithValue("@demographics", demoXml);

    try
    {
        connection.Open();
        Int32 rowsAffected = command.ExecuteNonQuery();
        Console.WriteLine("RowsAffected: {0}", rowsAffected);
    }
    catch (Exception ex)
    {
        Console.WriteLine(ex.Message);
    }
}

} }

In the while loop you need to call在 while 循环中你需要调用

while(reader.read()){
  .....
}

I'm suggesting using block to dispose your connection, command and reader object.我建议使用块来处理您的连接、命令和阅读器 object。

Also you can see some valuable input from the below link, if the size of the image data is big.如果图像数据很大,您还可以从下面的链接中看到一些有价值的输入。

What 'length' parameter should I pass to SqlDataReader.GetBytes() 我应该将什么“长度”参数传递给 SqlDataReader.GetBytes()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM