Specified cast is not valid. i can't understand error
-
public void FillCompVouType()
{
try
{
this.Cursor = Cursors.WaitCursor;
con = new SqlConnection(cs.sourceConn1);
con.Open();
scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
SqlDataReader DR = default(SqlDataReader);vochcombox.Items.Clear(); DR = scmd.ExecuteReader(); foreach (int VouType in DR) { vochcombox.Items.Add(VouType); } while (DR.Read()) { this.vochcombox.Items.Add(DR.GetOrdinal("VouType")); } DR.Close(); con.Close(); this.Cursor = Cursors.Default; } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); if (con.State == ConnectionState.Open) { con.Close(); } } }
-
public void FillCompVouType()
{
try
{
this.Cursor = Cursors.WaitCursor;
con = new SqlConnection(cs.sourceConn1);
con.Open();
scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
SqlDataReader DR = default(SqlDataReader);vochcombox.Items.Clear(); DR = scmd.ExecuteReader(); foreach (int VouType in DR) { vochcombox.Items.Add(VouType); } while (DR.Read()) { this.vochcombox.Items.Add(DR.GetOrdinal("VouType")); } DR.Close(); con.Close(); this.Cursor = Cursors.Default; } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); if (con.State == ConnectionState.Open) { con.Close(); } } }
Which line is it complaining about? I see no explicit casts in there.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
-
Which line is it complaining about? I see no explicit casts in there.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
foreach (int VouType in DR)
-
public void FillCompVouType()
{
try
{
this.Cursor = Cursors.WaitCursor;
con = new SqlConnection(cs.sourceConn1);
con.Open();
scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
SqlDataReader DR = default(SqlDataReader);vochcombox.Items.Clear(); DR = scmd.ExecuteReader(); foreach (int VouType in DR) { vochcombox.Items.Add(VouType); } while (DR.Read()) { this.vochcombox.Items.Add(DR.GetOrdinal("VouType")); } DR.Close(); con.Close(); this.Cursor = Cursors.Default; } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); if (con.State == ConnectionState.Open) { con.Close(); } } }
DR = scmd.ExecuteReader();
foreach (int VouType in DR)Are you sure that VouType is an Integer ?
-
foreach (int VouType in DR)
see my answer (under this) with it's question ...
-
DR = scmd.ExecuteReader();
foreach (int VouType in DR)Are you sure that VouType is an Integer ?
yes i'm its int
-
public void FillCompVouType()
{
try
{
this.Cursor = Cursors.WaitCursor;
con = new SqlConnection(cs.sourceConn1);
con.Open();
scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
SqlDataReader DR = default(SqlDataReader);vochcombox.Items.Clear(); DR = scmd.ExecuteReader(); foreach (int VouType in DR) { vochcombox.Items.Add(VouType); } while (DR.Read()) { this.vochcombox.Items.Add(DR.GetOrdinal("VouType")); } DR.Close(); con.Close(); this.Cursor = Cursors.Default; } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); if (con.State == ConnectionState.Open) { con.Close(); } } }
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 theGetInt32
method to retrieve the value of the field as anint
. You should also wrap your connection, command, and data-reader objects inusing
blocks.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
-
public void FillCompVouType()
{
try
{
this.Cursor = Cursors.WaitCursor;
con = new SqlConnection(cs.sourceConn1);
con.Open();
scmd = new SqlCommand("Select Distinct VouType From Table_name Where Colum=1 order by VouType", con);
SqlDataReader DR = default(SqlDataReader);vochcombox.Items.Clear(); DR = scmd.ExecuteReader(); foreach (int VouType in DR) { vochcombox.Items.Add(VouType); } while (DR.Read()) { this.vochcombox.Items.Add(DR.GetOrdinal("VouType")); } DR.Close(); con.Close(); this.Cursor = Cursors.Default; } catch (Exception ex) { this.Cursor = Cursors.Default; MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); if (con.State == ConnectionState.Open) { con.Close(); } } }
That error is telling you that he cannot convert DR object type to int. DR type is SQLDataReader, you cannot convert data reader to int. Why fill "vochcombox" twice? While loop should work, but foreach doesn't for sure.