'在不存在数据的情况下,无效的读取尝试. [英] 'Invalid attempt to read when no data is present.'

查看:44
本文介绍了'在不存在数据的情况下,无效的读取尝试.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#开发WinForms.我制作了具有方法 yourvideos()的课程视频.此方法从SQL数据库读取数据,并将其添加到 imagelist listview 中.

I am working on WinForms using C#. I made a class video that has method yourvideos(). This method reads data from SQL database and add it in imagelist and listview.

首先,我上传了图片,然后使用以下方法获取图片的位置:

First I have uploaded the image and then take the location of image using:

var=open.FileName.ToString();

然后在函数 uploadvideo()中,我将图像转换为字节数组并将其插入数据库:

Then in a function uploadvideo(), I converted image to bytearray and inserted it to database:

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','{user}','{date}','{title}')", con);
                        cmd.ExecuteNonQuery();
        }

,然后是我要检索数据的函数 yourvideos().

and then the function yourvideos() where I am retrieving data comes.

public void yourvideos(string user, ImageList imglist, ListView list, Label l)
{
    cmd = new SqlCommand($"select Title, Thumbnail from RecipeInfo where username='{user}'", con);
    con.Open();
    SqlDataReader reader = cmd.ExecuteReader();
    if (reader.HasRows)
    {
        while (reader.HasRows)
        {
            byte[] img = (byte[])(reader[2]);
            MemoryStream ms = new MemoryStream(img);
            imglist.Images.Add(Image.FromStream(ms));
            list.Items.Add(reader[5].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

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

现在,问题是如果语句表示存在数据但该控件不读取数据并显示错误,则该控件将输入:

Now, the issue is that the control enter in if statement means data is present but then it does not read data and display error:

在没有数据的情况下无效的读取尝试.

Invalid attempt to read when no data is present.

如何解决此问题?我显示了一个消息框,以确认if语句是否正常工作,但未检索到数据.

How can I solve this issue? I showed a messagebox in order to confirm that the if statement is working and it worked, but data is not being retrieved.

推荐答案

首先,不希望在获取数据时修正您的方法,因为这里存在许多问题,但是您可以在其他SO Q中获得帮助.

Firstly, not looking to fix your method in obtaining your data, as there are a number of issues here but you can get help on these in other SO Q's.

仅选择2列数据时,无法在位置5返回索引.

You can't return an index in position 5 when you are only selecting 2 columns of data.

list.Items.Add(reader[5].ToString());

将此更改为:

list.Items.Add(reader["Title"].ToString());

在您的SELECT语句中用标题"代替其他任何内容.

Substitute 'Title' for anything else in your SELECT statement.

我也会更改:

while (reader.HasRows)

收件人

while(reader.Read())

编辑为了修复无法使用的SQL语句的一部分,我建议使用SQL Command Parameter.首先,更新查询的这一部分.

EDIT In order to fix up part of your SQL statement which will not work, I would suggest using a SQL Command Parameter. First, update this part of your Query.

username='{user}'

对此

用户名= @用户名

接下来,您需要通过在con.Open()之前的行中添加以下内容来将其设置为命令参数.

Next, you will need to set this as a command parameter by adding the following on the line before con.Open().

cmd.Parameters.AddWithValue("@username", user);

这篇关于'在不存在数据的情况下,无效的读取尝试.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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