Click here to Skip to main content
15,888,351 members
Home / Discussions / C#
   

C#

 
GeneralRe: C# PictureBox remove image Pin
Ralf Meier21-Dec-16 22:58
mveRalf Meier21-Dec-16 22:58 
QuestionSpecified cast is not valid. i can't understand error Pin
Member 1289974620-Dec-16 2:07
Member 1289974620-Dec-16 2:07 
AnswerRe: Specified cast is not valid. i can't understand error Pin
OriginalGriff20-Dec-16 2:30
mveOriginalGriff20-Dec-16 2:30 
GeneralRe: Specified cast is not valid. i can't understand error Pin
Member 1289974620-Dec-16 2:34
Member 1289974620-Dec-16 2:34 
GeneralRe: Specified cast is not valid. i can't understand error Pin
Ralf Meier20-Dec-16 2:38
mveRalf Meier20-Dec-16 2:38 
AnswerRe: Specified cast is not valid. i can't understand error Pin
Ralf Meier20-Dec-16 2:36
mveRalf Meier20-Dec-16 2:36 
GeneralRe: Specified cast is not valid. i can't understand error Pin
Member 1289974620-Dec-16 2:44
Member 1289974620-Dec-16 2:44 
AnswerRe: Specified cast is not valid. i can't understand error Pin
Richard Deeming20-Dec-16 2:45
mveRichard Deeming20-Dec-16 2:45 
Member 12899746 wrote:
foreach (int VouType in DR)
{
   vochcombox.Items.Add(VouType);
}

You can't use a SqlDataReader like that. Each record could contain multiple fields, and it would have no way of knowing which field you want to convert to an integer.

Member 12899746 wrote:
while (DR.Read())
{
   this.vochcombox.Items.Add(DR.GetOrdinal("VouType"));
}

That's closer, but still not right. GetOrdinal returns the index of the specified field, not the value of that field.

You need to use the GetInt32 method to retrieve the value of the field as an int.

You should also wrap your connection, command, and data-reader objects in using blocks.

C#
public void FillCompVouType()
{
    Cursor = Cursors.WaitCursor;
    try
    {
        using (var connection = new SqlConnection(cs.sourceConn1))
        using (var command = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", connection))
        {
            vochcombox.Items.Clear();
            
            connection.Open();
            using (var dr = command.ExecuteReader())
            {
                while (dr.Read())
                {
                    vochcombox.Items.Add(dr.GetInt32("VouType"));
                }
            }
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    finally
    {
        Cursor = Cursors.Default;
    }
}




"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


AnswerRe: Specified cast is not valid. i can't understand error Pin
Nelson Costa Inácio21-Dec-16 5:54
Nelson Costa Inácio21-Dec-16 5:54 
QuestionIs there anything wrong with passing state via argument? Pin
TheOnlyRealTodd20-Dec-16 0:12
professionalTheOnlyRealTodd20-Dec-16 0:12 
AnswerRe: Is there anything wrong with passing state via argument? Pin
Afzaal Ahmad Zeeshan20-Dec-16 0:39
professionalAfzaal Ahmad Zeeshan20-Dec-16 0:39 
GeneralRe: Is there anything wrong with passing state via argument? Pin
TheOnlyRealTodd20-Dec-16 17:56
professionalTheOnlyRealTodd20-Dec-16 17:56 
Questioni get an error (non-invocable member-- can't be used like a method) any help? Pin
Member 1289974619-Dec-16 23:21
Member 1289974619-Dec-16 23:21 
AnswerRe: i get an error (non-invocable member-- can't be used like a method) any help? Pin
Afzaal Ahmad Zeeshan19-Dec-16 23:32
professionalAfzaal Ahmad Zeeshan19-Dec-16 23:32 
GeneralRe: i get an error (non-invocable member-- can't be used like a method) any help? Pin
Member 1289974619-Dec-16 23:42
Member 1289974619-Dec-16 23:42 
GeneralRe: i get an error (non-invocable member-- can't be used like a method) any help? Pin
Afzaal Ahmad Zeeshan19-Dec-16 23:45
professionalAfzaal Ahmad Zeeshan19-Dec-16 23:45 
GeneralRe: i get an error (non-invocable member-- can't be used like a method) any help? Pin
OriginalGriff19-Dec-16 23:55
mveOriginalGriff19-Dec-16 23:55 
GeneralRe: i get an error (non-invocable member-- can't be used like a method) any help? Pin
Afzaal Ahmad Zeeshan20-Dec-16 0:00
professionalAfzaal Ahmad Zeeshan20-Dec-16 0:00 
GeneralRe: i get an error (non-invocable member-- can't be used like a method) any help? Pin
Member 1289974620-Dec-16 2:01
Member 1289974620-Dec-16 2:01 
QuestionUnable to copy file "obj\x86\Debug\**** Pin
Rebyc Rebyc19-Dec-16 5:40
Rebyc Rebyc19-Dec-16 5:40 
AnswerRe: Unable to copy file "obj\x86\Debug\**** Pin
Richard Deeming19-Dec-16 5:57
mveRichard Deeming19-Dec-16 5:57 
AnswerRe: Unable to copy file "obj\x86\Debug\**** Pin
Afzaal Ahmad Zeeshan19-Dec-16 23:39
professionalAfzaal Ahmad Zeeshan19-Dec-16 23:39 
QuestionC# Textbox number and symbols Pin
Pavlex418-Dec-16 6:12
Pavlex418-Dec-16 6:12 
AnswerRe: C# Textbox number and symbols Pin
Pavlex418-Dec-16 6:52
Pavlex418-Dec-16 6:52 
GeneralRe: C# Textbox number and symbols Pin
Pete O'Hanlon18-Dec-16 9:55
mvePete O'Hanlon18-Dec-16 9:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.