Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
      protected void Add_Click(object sender, EventArgs e)
{
    GridView1.Visible = true;
    //createnewrow();
    ds.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);

    TextBox1.Text = "";
    TextBox2.Text = "";
    TextBox3.Text = "";

    GridView1.DataSource = ds;
    GridView1.DataBind();
    GridView1.DataSource = null;

}


What I have tried:

I try to add data to the table. It display an error message Input array is longer than the number of columns
Posted
Updated 30-Jan-18 22:08pm

Quote:
Input array is longer than the number of columns
You are trying to add more number of column values to the dataRow,
ds.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);

check the columns count and add correct number of column values
int colsCount =  dt.Columns.Count;


refer this example:
DataTable dt = new DataTable();
       dt.Columns.Add("Col1");
       dt.Rows.Add("Col1 Data"); // Valid
       dt.Rows.Add("Col1 Data", "Col2 Data"); // invalid ,  Input array is longer than the number of columns
 
Share this answer
 
v2
Comments
Member 13650918 31-Jan-18 4:14am    
It works. bt not display the grid view after putting this code. my code is to add values in text field and display it in a grid formate. what to do?
Karthik_Mahalingam 31-Jan-18 4:22am    
show your code.
Member 13650918 31-Jan-18 4:55am    
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace EmpGrid
{
public partial class WebForm1 : System.Web.UI.Page
{
DataTable dt = new DataTable();
private SqlConnection conn = new SqlConnection(@"Data Source=ADMIN-PC\SQLEXPRESS;Initial Catalog=task;User ID=sa;Password=ups");

protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// if (!ds.Columns.Contains("id"))


dt.Columns.Add("id");
// if (!ds.Columns.Contains("name"))
dt.Columns.Add("name");
// if (!ds.Columns.Contains("salary"))
dt.Columns.Add("salary");




GridView1.DataSource = dt;
GridView1.DataBind();

//gvbind();

}

}
protected void gvbind() {
conn.Open();
SqlCommand cmd = new SqlCommand("Select * from empdet", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
if (ds.Tables[0].Rows.Count > 0) {
GridView1.DataSource = ds;
GridView1.DataBind();
} else {
//ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
GridView1.DataSource = new String[] { };
GridView1.DataBind();


}
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lbldeleteid = (Label)row.FindControl("lblID");
conn.Open();
SqlCommand cmd = new SqlCommand("delete FROM empdet where id='" + Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString()) + "'", conn);
cmd.ExecuteNonQuery();
conn.Close();
gvbind();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{

GridView1.EditIndex = e.NewEditIndex;
gvbind();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value);

GridViewRow row = (GridViewRow)GridView1.Rows[e.RowIndex];
Label lblID = (Label)row.FindControl("lblID");

TextBox textName = (TextBox)row.Cells[1].Controls[0];
TextBox textadd = (TextBox)row.Cells[2].Controls[0];
// TextBox textc = (TextBox)row.Cells[2].Controls[0];



GridView1.EditIndex = -1;
GridView1.DataBind();
conn.Open();

SqlCommand cmd = new SqlCommand("update empdet set name='" + textName.Text + "',salary='" + textadd.Text + "'where id='" + id + "'", conn);


cmd.ExecuteNonQuery();

conn.Close();
gvbind();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
gvbind();
}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
gvbind();



}

protected void Add_Click(object sender, EventArgs e)
{
GridView1.Visible = true;

//ds.Columns.Add(new DataColumn("id", typeof(int)));
/
Hi, you are trying to add a new row which has 3 columns while you have zero(0) columns in your datatable ds. Solution: add columns to datatable.
Note: if you have added any databound column in your gridview1, make sure the column names of your dataset/datatable ds have the same names as in the DataField of your gridview Databound columns.

Try this code:

protected void Add_Click(object sender, EventArgs e)
        {
            DataTable ds = new DataTable();

            ds.Columns.Add(new DataColumn("Col1", typeof(string)));
            ds.Columns.Add(new DataColumn("Col2", typeof(string)));
            ds.Columns.Add(new DataColumn("Col3", typeof(string)));
            GridView1.Visible = true;
            //createnewrow();
            
            ds.Rows.Add(TextBox1.Text, TextBox2.Text, TextBox3.Text);

            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";

            GridView1.DataSource = ds;
            GridView1.DataBind();
            GridView1.DataSource = null;
        }
 
Share this answer
 
v2

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