Click here to Skip to main content
15,904,500 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
we have to insert data using asp.net and c# in this format. How can we do this as our table structure is different which has code, city and item in columns, while we require entry in below format. we also don't know how many items are there to be entered.

Cities  	Item 1	Item 2	Item 3
City 1			
City 2			
City 3


What I have tried:

I have tried multiple solutions to achieve this.
Posted
Updated 13-Aug-18 3:01am
v4
Comments
OriginalGriff 10-Aug-18 1:58am    
Show us sample data - input and output - and explain what you have tried. This is a complex area, and without knowing exactly what you are doing - and exactly what DB you are using - it is difficult to help you much. Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with. And that there are as many different ways to organise your data as there are people to organise it ...
Use the "Improve question" widget to edit your question and provide better information.

My quick'n dirty solution.
Can easily be changed to work the DataTable / Rows.
If your data come from SQL server this can be done in SQL server directly (search for TSQL-STUFF function)
Output
cities1	cities2	cities3
city1	city2	city3


Code
private void Convert()
    {
        var csvData = new List<string>() { "cities", "city1", "city2", "city3" };
        var csvOut = new List<string>();
        var lstTemp = new List<string>();

        //create header columns
        for (int i = 1; i < csvData.Count; i++)
            lstTemp.Add(csvData[0] + i.ToString());

        csvOut.Add(string.Join("\t", lstTemp.ToArray()));

        //convert first columns to one row
        lstTemp.Clear();
        for (int i = 1; i < csvData.Count; i++)
            lstTemp.Add(csvData[i].ToString());

        csvOut.Add(string.Join("\t", lstTemp.ToArray()));

        //output csv
        Response.Write(string.Join("\r\n", csvOut.ToArray()));
    }
 
Share this answer
 
v2
created a table with the following fields

itemcode
item
city
sales
date

also, created a form for data entry and taking input with no issue, each time user selects date, selects item from dropdownlist and add it's sales. However, i want to create a data entry form using below format where unknown items would be in columns and not in rows. we can enter 1 day data for each time and dropdownlist would not be required.

Cities  	Item 1	Item 2	Item 3
City 1			
City 2			
City 3


but, how can we take input in the following format for 1 day.
 
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