Click here to Skip to main content
15,889,874 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am on a software where I need to display Data on labels.
I am using a Datagrid view which loads an Excel file using Data set and by using same dataset which is already filled with Data, One can type Column name in Textboxes for which user needs to display Data on Labels.
Everything Works fine but when the textbox is empty Code trows an error.
I also want this functionality that if user want to display like information from three columns only then he can. I need to implement same thing for 15 labels so that if someone needs to show information from only 5 or 10 labels he will enter corresponding columns name in textboxes and leave rest empty. I am unable to find any solution for this.

What I have tried:

For better understanding, below is the portion of code and the two ways to search data from Data set by typing Column name in runtime or by predefined typed column names in code. I want to implement the former approach of Runtime in my project.

label1.Text = ds1.Tables[0].Rows[i][@textBox1.Text.ToString()].ToString();

label1.Text = ds1.Tables[0].Rows[i]["Columnname1"].ToString();
Posted
Updated 16-Sep-17 1:06am

1 solution

Firstly you should check the values in the various textboxes to make sure they are valid. That will help you avoid the error you see.

Secondly, you cannot dynamically create a query in the way you are trying. You need to use the values from the textboxes to find the row and column indexes. You can then use those index values to reference the values from the dataset.

Thirdly, do not use default variable names like ds1, textBox1 etc. Use proper meaningful names so you, and others, will know what their purpose is.
 
Share this answer
 
Comments
himanil 16-Sep-17 9:26am    
hi thanks for reply, Actually I need to implement following things:
1. The user can load any Excel File from which he want to add data on fields like Labels.
2. The Excel will be loaded into Dataset and Dataset will be loaded in DataGridview. So he can see his Excel file.
3. User can then type the Column names in textboxes from which he needed Information to be added on Form, This will facilitate to add specific Columns information not all the colums because in one Excel sheet he can have 5 columns useful and in other Excel he might have 10 Columns useful only.
4. Whatever Column name he typed in Textboxes, The cells value will be added to corresponding Labels in Form. So this will like Choose which columns data you want to display and facilitate user in more friendly way.

I had implemented this process till Step 2 successfully but in next step if I try to define code for lets sake 15 Labels only. So what I did I wrote the above code 15 times changing only label and Textbox ids to map maximum 15 labels with 15 columns by 15 Textboxes.
But the problem I am facing is if I as a user at this time leaves any of the textbox as Blank which means I might have selected 9 columns then C# throws an error like, ' ' is not found in dataset.
I am figuring out how to solve this.

Hope this will make my situation clear and helps you or others to find a solution for this problem and I know Codeproject users can Code anything.

Thanks and Warm Regards
Richard MacCutchan 16-Sep-17 9:39am    
My first question is: why are you making this so complicated? Let the user have Excel and do it themselves.

However, if you are determined to do it this way, then I think you need to rethink your design. Creating 15 labels when the user may only use 9 is just wrong. And why are you getting the user to type the column names when they can just select the specific columns in the DataGridView? ...

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