You must Sign In to post a response.
  • Category: .NET

    Creating a datatable with autoincrement column

    The following is the code i have created for creating a datatable with an autoincrement column.

    Code: (button1_Click)
    -----

    DataTable dt = new DataTable("CarsIndia");
    DataColumn dc = dt.Columns.Add("CarId", typeof(int));
    dc.AutoIncrement = true;
    dc.AutoIncrementSeed = 000;
    dc.AutoIncrementStep= 1;
    dt.Columns.Add("Carmake", typeof(string));
    dt.Columns.Add("CarModel", typeof(string));
    dt.Columns.Add("Variant", typeof(string));


    dt.Rows.Add("Toyota","Qualis", "VXI");
    dt.Rows.Add("Hyundai", "i10", "Sports");
    dt.Rows.Add("Honda", "City", "VXI");

    When upon running the above code i am facing an error saying " Input string was not in a correct format.Couldn't store <Toyota> in CarId Column. Expected type is Int32."

    Any valuable suggestion is expected.

    Thanks
  • #681029
    Hi,
    CarId must be of integer type and you are trying to insert string value in to it. Eventhough CarId is autoincrement column , while inserting records it is taking Toyota as input for CarId field which is wrong hence you are getting error message.

    -Sarang

  • #681032
    Try this code

    DataTable dt = new DataTable("CarsIndia");
    DataColumn dc = dt.Columns.Add("CarId", typeof(int));
    dc.AutoIncrement = true;
    dc.AutoIncrementSeed = 000;
    dc.AutoIncrementStep = 1;
    dt.PrimaryKey = new DataColumn[] { dt.Columns["CarId"] };
    dt.Columns.Add("Carmake", typeof(string));
    dt.Columns.Add("CarModel", typeof(string));
    dt.Columns.Add("Variant", typeof(string));

    string carmakers = "Toyota,Hyundai,Honda";
    string cars = "Qualis,i10,City";
    string Model = "VXI,Sports,VXI";

    string[] carmake = carmakers.Split(',');
    string[] car = cars.Split(',');
    string[] Models = Model.Split(',');

    DataRow dr = null;
    for (int i = 0; i < 3; i++)
    {
    dr = dt.NewRow();
    dr["CarID"] = i + 1;
    dr["Carmake"] = carmake[i].ToString();
    dr["CarModel"] = car[i].ToString();
    dr["Variant"] = Models[i].ToString();
    dt.Rows.Add(dr);
    }


    In this code i have taken some static variables so please change it as per your requirements.

    Hope this will help you.

  • #681059
    You can try like this,



    DataTable dt = new DataTable("CarsIndia");
    DataColumn dc = dt.Columns.Add("CarId", typeof(int));
    dc.AutoIncrement = true;
    dc.AutoIncrementSeed = 000;
    dc.AutoIncrementStep= 1;
    dt.Columns.Add("Carmake", typeof(string));
    dt.Columns.Add("CarModel", typeof(string));
    dt.Columns.Add("Variant", typeof(string));


    var nr = dt.NewRow();
    nr["Carmake"] = "Toyota";
    nr["CarModel"] = "Qualis";
    nr["Variant"] = "VXI";

    dt.Rows.Add(nr);

    nr = dt.NewRow();
    nr["Carmake"] = "Hyundai";
    nr["CarModel"] = "i10";
    nr["Variant"] = "Sports";

    dt.Rows.Add(nr);



    nr = dt.NewRow();
    nr["Carmake"] = "Honda";
    nr["CarModel"] = "City";
    nr["Variant"] = "VXI";

    dt.Rows.Add(nr);

    SQL Server Programmers and Consultants

  • #681097
    Please check the following code:


    protected void Button1_Click(object sender, EventArgs e)
    {
    DataTable dt = new DataTable("CarsIndia");
    DataColumn dc = new DataColumn("CarId", typeof(int));
    dt.Columns.Add(dc);
    dc.AutoIncrement = true;
    dc.AutoIncrementSeed = 1000;
    dc.AutoIncrementStep = 1;
    dc.ReadOnly = true;
    dt.Columns.Add("Carmake", typeof(string));
    dt.Columns.Add("CarModel", typeof(string));
    dt.Columns.Add("Variant", typeof(string));


    DataRow dr = dt.NewRow();
    dr["Carmake"] = "Toyota";
    dr["CarModel"] = "Qualis";
    dr["Variant"] = "VXI";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["Carmake"] = "Hyundai";
    dr["CarModel"] = "i10";
    dr["Variant"] = "Sports";
    dt.Rows.Add(dr);

    dr = dt.NewRow();
    dr["Carmake"] = "Honda";
    dr["CarModel"] = "City";
    dr["Variant"] = "VXI";
    dt.Rows.Add(dr);

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


    Thanks & Regards
    Paritosh Mohapatra
    Microsoft MVP (ASP.Net/IIS)
    DotNetSpider MVM

  • #681169
    provide Autoincrement seed and autoincrement step before adding autoincrement column


    DataColumn AutoInc_col= new DataColumn();
    AutoInc_col.DataType = System.type.GetType("System.Int32");

    AutoInc_col.AutoIncrement = true;
    AutoInc_col.AutoIncrementSeed = 1000;
    AutoInc_col.AutoIncrementStep = 10;

    //Add the column to a new DataTable.
    DataTable dtTable = new DataTable();
    dtTable.Columns.Add(AutoInc_col)k


    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.