Word Automation: deleteing table column by name - c#

Does anyone know how i can delete a column of a table in word from c#? I basically search a word doc for tables and for each table ive found, i want to delete the entire column with the name "Student ID" from that table. I know you can delete by column index using code like:
wordTable.Columns[0].Delete();
But i need a way to delete the column based on the name of the column since each table in the doc may have the student id column in different indices.
Thanks

Tables don't have names as such - what you probably mean is the column whose top cell has "Student ID" in it. You're going to have to walk the columns and look at the top cell in each (presumably you can at least guarantee that has the lowest cell index) to find if it has the text you're looking for.
wordTable.Columns[n].Cells[0].Range.Text

Related

How do I read/write record in Access Database that has table relationships?

I am creating an Access Database, that has the following tables and field
TableMain TableCustomer Record in TableCustomer
ID Auto
fDate(Date/time) CID (Auto) 4
fCustomer -------fCustomerName (short) John Smith
the relationship is Field fCustom in TableMain to field fCustomerName in TableCustomer
How do I go about creating a new record in TableMain, referencing John Smith in TableCustomer (ID=4)
````command.CommandText="INSERT INTO TableMain (fDate,fCustomer) VALUES (#dt,#cust)";
````command.AddParameterWithValue( "dt", #10/15/2023#);
````command.AddParameterWithValue( "cust", ???); <- what do I put for the value
do I put "John Smith" or the number 4 (ID for John Smith)?
2) How can I select a record that will have the related data returned to me,
is it just SELECT * FROM TableMain, or does it need joins?
fcustomer column should be a plain jane long data type.
So, yes, you insert 4 into that record.
In any report, or maybe even a query? Then you simply have table main, and then join on the customer table based on ID, and then you can enjoy/have/use/display/see the full name by using that sql join.
On a form to edit tblmain, you could say have a combobox (drop down list), and it would have two columns, the first column (hidden) would be "ID" from tblcustomer, and the 2nd column (for easy user display) would of course show/display the customer name. But, that "value" of the combo box will be "id". Then when you save tblmain record, you save the "ID" value into a column called fcustomer.
Probably better to adopt a naming convention for those columns, and hence
tblCustomer_ID would be a better and more descriptive name in tblmain.
So yes, while you the developer have to save 4 in this example to that tblmain, the user would of course never have to type in 4, but you would provide some kind of drop down (combbox) or some such to allow the user to select the customer in a nice user-friendly approach.
So that form to edit tblmain is ONE record, and when you save, you save the columns of data for that form.
However, while the code you write works this way, you provide a UI on that form that would let the user select/see/have/enjoy the selecting of a customer by name, but your code behind would of course work with the customer ID value, and save that number into the tblmain record.
This means that your form to "edit" and "save" tblmain records still is only ever saving one record, despite the fact of having controls on that form that can pull + display that information from the customer table.
So, in your code behind?
As a general rule you still ONLY updating a single record for that tblmain record, and it will be based on that table, and not some type of SQL join.
You only need that sql join for a grid like display, or perhaps a report, but for a form that edits such data? No that form will work on the one record, and some controls like a listbox or combo box (drop down list) are used to pull + display that related data.

RDLC showing additional Rows in a table

I am creating a Report on RDLC. Lets say i have 2 tables in database where im importing data from users and expenses.
Each user can have multiple Expenses. Expenses contain Details and amount just 2 columns that i have to include in the report.
Ive created a stored procedure but its duplicating rows because of having same UserId in expenses. So far im able to achieve this.
See This Picture
What i wanted is that One row with column bulty id and other details with multiple sub rows of expenses.
How can i achieve this. I've tried hide duplicate property in RDLC but its showing duplicate column fields
This is what i wanted to achieve
See image
in this case, I use matrix tool in RDL file.
you should set expenses the ColumnGroup of the matrix
Solved it by doing the Master/Detail Approach. Put 2 tablix in the list. on the first tablix set user details and made a row group so that the values dont repeat and then on the second tablix used the additional data fields.
Then dragged this additional data tablix into the row of first tablix and everything seems to work just fine.
For serial numbering i used Counting distinct values according to my userid

On Transact-SQL: possible to build a statement to create a new column while using the data of another column all from the same table.

On Transact-SQL: Would like to create a statement that adds a column to my current active table, the newly added column would take data from another column of the same table. It will add the data on each row and sum it up on the new column.
To be more specific,
I work for a company that produces Sweet Potato fries, we use labels to describe the content of each box.
I have a table that gets updated and populated when a label gets printed, the label goes on each box; So number of labels == number of boxes we produce. The table has a column that describes the weight of each box; the column being named [CaseWeight].
I want to take this CaseWeight column do a Sum([CaseWeight]) on a new column and call it total pounds. While still been able to see the rest of the columns in the table.
You can use a view with a window function:
create view v_t as
select t.*, sum(weight) over () as total_pounds
from t;
You cannot put a window function in as a generated column, so this is probably the best approach.
To add a column to a table, you may need to drop the table, depending on the server settings. What I do in that case is back the data of the table up into another table (SELECT * INTO MyTableBackup FROM MyTable). Then I drop the table and recreate it with the new column (NOTE: When dropping and recreating the tables, don't forget to recreate the indexes and keys as well!). Then I do an insert from the backup table to the new one. You would do the Group By on the insert when summing. So if you wanted the sum of the weight of each package in the order, you would do:
SELECT SUM(CaseWeight) AS TotalPounds FROM MyTableBackup GROUP BY OrderNumber
You can put that in the middle of the select like this:
INSERT INTO MyTable
SELECT
bak.OrderNumber
,bak.NumberOfCases
,bak.CaseWeight
,(SELECT SUM(CaseWeight) FROM MyTableBackup b WHERE b.OrderNumber = bak.OrderNumber GROUP BY OrderNumber) AS TotalPounds
FROM MyTableBackup bak
Just make sure you test the select part of your query before doing the DROP and INSERT.

ItextSharp set field of a table from template pdf using c#

I have PDF template.
In PDF template there is a table with the list of people with the columns name (name_dic) and surname (surname_dic). The field names are the same for all row.
With
stamper.AcroFields.SetField("NAME_DIC", "Lucas");
stamper.AcroFields.SetField("SURNAME_DIC", "Brown");
I write in the first line
How do I write in the second row?
Thank you
I have solution.
first row
Stati_Famiglia[0].#subform[0].details[0].detail[0].NAME_DIC[0]
second row
Stati_Famiglia[0].#subform[0].details[0].detail[1].NAME_DIC[0]

Dynamically bound DataGridView shows -1 value on auto-increment column?

I've dynamically bind datagridview with my database table and ContactId column is autoincrement in my database table when i click on last row in gridview to insert data it shows me -1 then on next -2 when i save the contents and then again open it shows properly why such a behavior?
The negative values are temporary keys. Needed to make proper foreign key relations etc. And indeed replaced when the Db generates the real keys.

Categories