-3

Project - add delete edit and search in local data base. Everything works perfectly apart from search.

All file names are the same just so confused. Don't really know anything about C# so probably something really daft.

Any help would be hugely appreciated.

The error

Cannot convert 'string' to 'int'

shows at the commented lines.

private void button4Search_Click(object sender, EventArgs e)
{
    SqlDataReader mdr;
    string select = "SELECT * FROM users WHERE id = " + textBoxID.Text;
    command = new SqlCommand(select, connection);
    openConnection();
    mdr = command.ExecuteReader();

    if (mdr.Read())
    {
        // ERRORS HERE
        textBoxFileName.Text = mdr.GetString("FileName");
        textBoxFilePath.Text = mdr.GetString("FilePAth");
        textBoxMediaFileType.Text = mdr.GetString("MediaFileType");
        textBoxComments.Text = mdr.GetString("Comments");
    }
    else
    {
        MessageBox.Show("User Not Found");
    }

    closeConnection();
}
Magnetron
  • 7,495
  • 1
  • 25
  • 41
  • 5
    Welcome to Stack Overflow! Please DON'T post images of your code. Instead, [edit] your question to show the code AS TEXT. Also the error message, AS TEXT. Try to create a [mcve], with that information, maybe we can help you. – S.L. Barth is on codidact.com Mar 27 '19 at 11:15
  • 1
    Firstly Gemma, welcome to StackOverflow. When it comes to posting questions please provide your code, expected outcome, and your actual outcome so that we can better help you. –  Mar 27 '19 at 11:16
  • 1
    the reader in your code expects an `int` type not string or field/column name https://learn.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqldatareader.getstring?view=netframework-4.7.2 – Aarif Mar 27 '19 at 11:16

3 Answers3

3

At first two important hints:

  1. Your code is open to SQL injection attacks. This is dangerous. Please use parameterized queries.
  2. Don't use SELECT * but state explicitly which columns to return. This way you avoid unnecessary traffic and can be sure to get the columns in the order you expect.

To your question:

GetString() takes an index as argument. Not a column name. You can use GetOrdinal() to get the index of a column by name:

textBoxFileName.Text = mdr.GetString(mdr.GetOrdinal("FileName"));
René Vogt
  • 43,056
  • 14
  • 77
  • 99
  • Thank you, Your thing worked but now its throwing this at me, I honestly don't have a clue about c# ill up load a photo now – Gemma Belshaw Mar 27 '19 at 11:27
  • 2
    @GemmaBelshaw No, don't load up another photo! Please post code and error messages as text! And if you have a different question now, ask a new question. And if you really don't have a clue of c# at all, i'd suggest to use a book instead of posting every little compiler error on SO. – René Vogt Mar 27 '19 at 11:29
0

First of all please provide the code here in the question. Try:

mdr.GetValue(read.GetOrdinal("de_DE")).ToString();
René Vogt
  • 43,056
  • 14
  • 77
  • 99
cr4nk89
  • 19
  • 4
  • 2
    Hello, when answering a question, is nice to use the same variables that OP used. In your answer you're mixing OP variables (`mdr`) with your own (`read`, `"de_DE"`). Also, always format your code as code by adding 4 spaces before each line – Magnetron Mar 27 '19 at 11:26
0

Correct the code:

mder = command.ExecuteReader();

into:

mdr= command.ExecuteReader();

And edit this: textBoxFileName.Text = mdr.GetString(mdr.GetOrdinal("FileName")); similarly all textboxes