convert sql bit into checkbox into desktop application using c# - winforms

I have a table like city and its have cityID primary int, cityName varchar(50), cityActive bit. I have directly bound into data grid using linq.
DGCustomers.DataSource = db.city.ToList();
But cityActive display into data grid as string always, so there is any direct method exists which convert its into checkbox. I know that we can do this using loop, and we can add column using form design or run time. but i want to confirm that if available any direct method. Please confirm us any one.

I have created a column with cActive and set the DatapropertyName = "cityActive" and set displayIndex accordilngly. In this case its directly bind the column from datasource. So no need to create extra columns, just need to map with DatapropertyName.

Related

Problem with Showing Query Results on a Form

I'm having a Projects lists Continuous form where the form's Record Source is based upon a table.
I have a requirement to display a field from another table which is linked back to original table using its primary key. The primary key is a AutoNumber field, but when displaying in the form I've used Input mask something like this "TMG/FEA/"0000.
So I made:
a unbound list box
and made the Row source as the query which displays the relevant information from second table
This query was created using primary key displayed in form (I mean the [Forms]![Form Name].[Field] ) as the where clause.
But the results returns blank. I'm Stuck here. I'm not sure if the query is not working due to the Input Mask or because of something else. Please help me. Thanks in advance
You should add to form field listbox, and set following values:
Data: primary key of your first table
After this, that field will duplicate ID value. Now you should transform RecordSource of this field in order to see contents from second table. So you should set:
SELECT [PrimaryForeignKeyID], [DetailedField] FROM tblSecond;. Actually I don't know the contents of your second table. Whereas PrimaryForeignKeyID is a field that links second table to first, so-called FK.
After this set following properties of list box:
ColumnCount = 2
ColumnWidth = 0;2
AllowEdits = False
Save form and open it for viewing.
In this case, your listbox will show the associated contents of second table on form.
So to my mind it is better for you to do such simple tables and forms, and you'll realize idea.

Look up information in onetable to populate values in a form bounded to another table

I have an excel sheet with information about each employee. I keep getting new updated spreadsheet every month. I have to create a database managing cases related to the employees. I have a database and the bounded form already created for the cases which also contain emp info fields. What I am trying to do is to only type in the emp id in the form and want the form to look up in the spreadsheet(which can be a table in the cases db) and populate other fields in the form and that information can go into the cases db. Can this be done?
Assuming the Employee information is available within the current database, perhaps in a linked (Excel), table there are a number of ways to approach this, one of which is:
Create a form based on the Employees table, showing the fields that you are interested in auto-populating
Delete the RecordSource of the form
Delete the ControlSource for each of the controls on the form. You need to do this otherwise they will all initially display with the error #Name?
Set the Locked or Enabled property of these controls to Yes or No respectively, so that the information they will display will not be editable
Add a, for example, Combo Box to the form; you can accept the third option in the Controls Wizard to help you populate this. You need the EmployeedID as the first column, but can add additional columns
Delete the Embedded Macro that Access creates (or Macro for Access 2003 or earlier) for the AfterUpdate event
Click the build button for this event (...) and create some code.
Here is some code I used with my sample Staff Database:
Private Sub cboStaff_AfterUpdate()
Me.RecordSource = "SELECT StaffID, Title, FirstName, Surname FROM " _
& "tblStaff WHERE StaffID = " & Me.cboStaff
With Me
.txtStaffID.ControlSource = "StaffID"
.txtTitle.ControlSource = "Title"
.txtFirstName.ControlSource = "FirstName"
.txtSurname.ControlSource = "Surname"
End With
End Sub
Whenever the user selects a staff (or employee) member from the combobox this will retrieve the data from the table and populate the various controls on the form. These controls will not be editable as they will be locked or not-enabled. (You can also set the Allow Additions and Allow Deletions properties of the form to No, but Allow Edits needs to remain as Yes, otherwise the combobox won't work.)
This code can be improved. In particular, to only set the RecordSource and ControlSource s once.
Obviously I am not aware of the specifics of your database, and there are other ways to approach this.
Added If, however, the RecordSource for the form is some other table that you are hoping to populate with some details from the Employees table then, instead of changing the RecordSource and ControlSource as indicated, you could use ADO (in the AfterUpdate event of the combobox) to create a RecordSet containing a single row (the chosen employee's details) and set the values (the Text) of controls on the form to the values from this recordset. As I say, there are a number of ways to approach this.
Added In response to:
"The changes you requested to the table were not successful because
they would create duplicate values in the index, primary key or
relationship...."
I don't know your precise set-up but I can tell you why this is happening. The default behaviour in Access is that, for a bound form, if a change is made to any one of the bound fields, then an attempt to navigate away from the record will cause Access to save the record.
Is the form bound when it doesn't need to be? Or have you set the ControlSource of a control to a field when it doesn't need to be? If this is not the case then:
In the BeforeUpdate event of the form you can set the Cancel argument to True to prevent the update (or insert). However, this will prevent ANY new record from being inserted. You can either have a Button that the user needs to click to explicitly save the record, or, in the BeforeUpdate event, test some condition with If to decide whether you will allow the insert (or update) or to stop the record from being saved (by setting Cancel = True).

How do I tell Entity Framework to allow SQL Server to provide a defined default value for a field?

I used the following SQL script to enable setting current time in a field, when a new row is added to a table:
ALTER TABLE [Items] ADD CONSTRAINT DF_Items DEFAULT GETDATE() FOR [CreationDate]
Now I am using Entity Framework to work with this table, and add new rows to it. What I want to do is allow that specific column to receive its value from SQL Server itself, and not have to provide the value myself.
Setting that specific column's value to Nothing in Visual Basic fills the field with DateTime.MinValue, which is not what I want (and SQL Server doesn't support, by the way).
What changes do I have to make to make this work?
You must set StoreGeneratedPattern in EDMX designer (or DatabaseGeneratedOption in code first) to Identity for that date property. EF always sends .NET default value for not filled property which is not store generated. Setting the pattern to Identity will tell EF that value is generated in DB during insert and it will requery its value. If you change the pattern from default value you will not be able to set the property in your application.

Access 2007 - Query displays Combobox Primary ID rather than Value

I am writing a query based on a Table which is populated via a Form with several combo boxes. The information is stored in the Table as the Primary Key of the Combobox value which I understand to be correct. However, when I run a query or report based on the Table, I would like to display the Combo box Value rather than the Primary Key ID. Is there a way to do this other than using a Lookup to the combobox in the Table?
Thanks in advance for your help.
JB
It depends on how you have your combo boxes set up. Are they populated from a list in the field or from another table?
If from a list you entered in the table field where the value is stored, you should just store the value.
If they are from a separate table, then set the relationship from the combo lookup table's primary key/ID to a field in the main table that stores the combo selection which should be a number type. In a query, add both your main table and the other table where values for the combo are stored and add the value to your results from the combo value table along with the data from your main table.
It's a little confusing to say you want to query a combobox value, essentially you need to look at the SQL source of the combobox first in order to see where it draws it's values from. You then need to link this back to your query that you are running, as long as there is a common PK/FK between these then you can just perform an INNER JOIN on that.

Database Design for Filtering Database

I'm new to complex database design. I'm currently into a project where the user should be able to retrieve Instructions based on a combination of 18 columns. So my parameter Table has the following columns
Job
State
Manager
ProcessCode
ProcessType
(rest of the columns truncated).
InstructionID (FK of Instruction Table)
When adding / Modifying the instruction, he can choose multiple options in each of the above parameters. The Stored Procedure will store data in all combinations possible, in order facilitate easy retrieval, as during search (retrieval) only one option will be chosen in each of the columns.
There can be multiple instructions for same combination and the same instruction can apply to multiple combinations.
I have somehow created the SP for adding instruction, but am now struck with modification. When my Webpage passes the new combination to SP, what is the best way to update the Table?
I could delete all existing rows and create new rows for new combination, but I wanted to maintain the created date and created user columns. Further, there is a requirement to maintain history of these in a separate history table.
Sorry for the length of the question... And, Thank you for help.
If you're trying to retrieve data based on a combination of parameters then you can set the parameters to have the default value of NULL e.g.
CREATE PROC spProcName
#FieldName INT = NULL
The only other thing to do is set the WHERE section of the statement to look at the parameter values and compare them to see if they or null or not e.g.
WHERE ((FieldName = #FieldName) OR (#FieldName IS NULL))
Use this for querying the tables and use standard update queries in a similar fashion using the default parameter value of null but setting the value like this:
FieldName = ISNULL(#FieldName, FieldName)
Which lets you update only given parameters.
Hope this is something you are after, I can give a full example if needed.
What you have is many-to-many relationship, so I would suggest you use:

Resources