Click here to Skip to main content
15,888,202 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello,

I want to bind my all billno with initial value "--Select--" but its getting error with initial value using c# .net windows application.

"Error:-
Input array is longer than the number of columns in this table.

Ho can we resolve this issue.
Please help me.

Thanks in Advance.

Ankit Agarwal
Software Engineer

What I have tried:

public DataTable GetAllBillNo()
       {
           DataTable dt = new DataTable();
           SqlCommand cmd = new SqlCommand("spGetAllBillNo", GetConnection());
           cmd.CommandType = CommandType.StoredProcedure;
           SqlDataAdapter sda = new SqlDataAdapter(cmd);
           sda.Fill(dt);
           DataRow dr;
           dr = dt.NewRow();
           dr.ItemArray = new object[] { 0, "--Select--" };//Error in this line.
           dt.Rows.InsertAt(dr, 0);
           return dt;
       }

       public void GetAllBillNo()
       {
           cmbBillNo.ValueMember = "OrderID";
           cmbBillNo.DisplayMember = "OrderID";
           cmbBillNo.DataSource = connClass.GetAllBillNo();
       }
Posted
Updated 10-Oct-17 17:51pm
v2

Simple: look at the error message:
Input array is longer than the number of columns in this table.
It's pretty clear.
You are trying to add two columns of data to a table that has - at most - one column. Since the system doesn't know what to do with the rest of it, it throws it away.

Use the debugger: put a breakpoint on the line
dr.ItemArray = new object[] { 0, "--Select--" };
And look at dt carefully. How many columns does it have? What are they? What do they contain?

Probably, you are calling the wrong SP, or it doesn't return what you think it does - but I'd suspect that "Get all bill numbers" would return a single column of data, myself.
 
Share this answer
 
Comments
Agarwal1984 10-Oct-17 3:27am    
I am calling correct SP.
Only one column in my dt.
Only OrderID.

when i am not using this code:-
DataRow dr;
dr = dt.NewRow();
dr.ItemArray = new object[] { 0, "--Select--" };//Error in this line.
dt.Rows.InsertAt(dr, 0);
So, value are displaying in my combobox but i want to initial value at 0 position like "--Select--"

DataTable is returning in Single column:-
select OrderID from Orders (this query written in SP).
OriginalGriff 10-Oct-17 3:39am    
One column == one data. You are trying to add two items: a zero, and a string. Rightly, the system complains.

Instead of creating an array with two items, remove the zero from the array and leave the InsertAt code the same.
Agarwal1984 10-Oct-17 3:45am    
Now its getting error:-
Input string was not in a correct format.Couldn't store <--Select--> in OrderID Column. Expected type is Int32.

I changed my code:-

public DataTable GetAllBillNo()
{
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand("spGetAllBillNo", GetConnection());
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
DataRow dr;
dr = dt.NewRow();
dr.ItemArray = new object[] {"--Select--"};
dt.Rows.InsertAt(dr,0);
return dt;
}

if i convert.int so then another error is giving:- input string was not in a correct format.
OriginalGriff 10-Oct-17 4:08am    
So...you have an column that holds numbers, and you want to put a string into it?
And these error messages aren't ones you expected?

I'm not trying to be awkward here, but remember that I have no idea what your data is - I only get exactly what you type to work with.

If you want to mix types, you are going to have to change what you are doing: you can't "automate" the process of creating the table like that because if you provide integers it creates an integer column. Instead, convert your values from SQL from integer to string and add your "select" at the top. Then create your data table from that. Or create a class which contains the integer, and overrides ToString to display it - and the ToString implementation returns "Select" for a negative number. That way, your items can remain in numerical order without leading zeros being needed.

The later is the way I'd go.
Agarwal1984 10-Oct-17 4:16am    
my requirement is:-

--Select--
1
2
3
4

i want to fill combobox with --Select-- but my data successfully filled without select.
try

sda.Fill(dt);
           DataRow dr;
           dr = dt.NewRow();
           dr["OrderID"] = "--Select--";
           dt.Rows.InsertAt(dr, 0);
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900