Update specific rows in LINQ to SQL result set - vb.net

I have a page with a form on it and needs a range of dates. Thus I've placed a number of textboxes on the page into which users can type dates. When the user clicks the save button I want to trigger a LINQ update to the SQL Server...all the rows already exist, so I'm just updating existing data. How can I do this?
For example, lets say my table looks like this:
Column Names: Description dateValue
Column Values:
Birthdate 1/1/1990
Anniversary 1/10/1992
Death 1/1/1993
I want to do something like this:
hupdate.Description("Birthdate").dateValue = TextBox1.Text
hupdate.Description("Anniversary").dateValue = TextBox2.Text
hupdate.Description("Death").dateValue = TextBox3.Text
hconfig.SubmitChanges()
Is there a way to do this with LINQ?

I don't think there is a way to do that with only LINQ. However, there are other ways to achieve that like:
if you use Entity Framework you can use LINQ to query the data, then change the entities (within your c# code) and update the DB via them.
Create a stored procedure that updates the data, according to your description this stored procedure doesn't seem like a complicated one.

Related

Filtring/Searching from a DataGridView

I have a DataGridView in a form which displays a client list with the columns ID, First Name, Last Name, Address. I also included a TextBox to perform the search query. I want to filter my DataGridView based from the given columns using a single TextBox (like a multi data filter/search where I can select from those four columns by typing on a single TextBox).
The question is: am I required to create a binding source for here (I populate my DataGridView using sql database) or is there a way to create filters without having to add a binding source?
You don't populate your grid using a sql database. You populate your grid from a SQL Server database suing something else in between, e.g. a SqlDataAdapter and a DataTable.
A BindingSource is supposed to be a one-stop shop for working with bound data so, while you don't have to use one, I would recommend doing so. Whether it can help you filter your data depends on what it's bound to. The BindingSource doesn't actually do the work of filtering itself but rather passes the work on to the underlying IBindingListView implementation if there is one. For instance, if the underlying data source is a DataTable then the RowFilter of its DefaultView will be used. You could set the DefaultView.RowFilter yourself.
If you want to do all the heavy lifting yourself then you could also search the grid yourself and then hide the rows that don't match. Those rows would still exist though, so you'd have to take that into account when using the data in code.
You have to create a query to retrive a data using like keyword by passing parameter value in form
Example:
pass the parameter from
form(cmd.parameters.add(nvarchar, 20).value = "textbox.text")
create a stored procedure
CREATE PROCEDURE procedure_name
#searchvalue varchar(20)
AS
BEGIN
SELECT
ID, FirstName, LastName, Address
FROM
tablename
WHERE
FirstName LIKE '%#SearchTerm%'
END
This above stored procedure will retrieve all the rows where the FirstName column value corresponds to that name
Then pass the result to gridview

selecting a column values from a Table using LINQ (Entity Framework)

Im trying to display a dropdown with values from table 'Events'. I have created a Model.edmx which has the structure of the database. Now i have to just write LINQ code to display one column values. I am kind of new to LINQ.
Dim Events=" LINQ select statement part???"
ddlEvent.DataSource = Events
ddlEvent.DataBind()
ddlEvent.Items.Insert(0, New ListItem("-Type-", ""))
DropDownLists work with 2 fields:
the DataTextField represents the field holding the text of the options that will be displayed to the user
the DataValueField represents the field hoding the value associated with each option
So in your case, you could get it to work with something like:
Dim events As List(Of Event) = yourDbContext.Events.ToList()
ddlEvent.DataSource = events
ddlEvent.DataTextField = "NameOfThePropertyYouWantDisplayed"
ddlEvent.DataValueField = "NameOfThePropertyYouWantAsValue"
ddlEvent.DataBind()
Note, however, that the query will retrieve every property from the database. This can hurt performance if your Event class has a lot of properties.
To avoid this, you could use the LINQ Select operator, used for projection. This could be used to retrieve only the necesseray data from the database. You can have a look on the MSDN here
Context.Events.ToList();
Where, Context is your EF db context. Update your filter condition in where clause
ddlEvent.DataSource = from x in Context.Events select x.column;
here 'x' is a variable and 'column' in 'x.column' is required column you want to fetch.
Where, Context is your EF db context.

query a table not in normal 3rd form

Hi I have a table which was designed by a lazy developer who did not created it in 3rd normal form. He saved the arrays in the table instead of using MM relation . And the application is running so I can not change the database schema.
I need to query the table like this:
SELECT * FROM myTable
WHERE usergroup = 20
where usergroup field contains data like this : 17,19,20 or it could be also only 20 or only 19.
I could search with like:
SELECT * FROM myTable
WHERE usergroup LIKE 20
but in this case it would match also for field which contain 200 e.g.
Anybody any idea?
thanx
Fix the bad database design.
A short-term fix is to add a related table for the correct structure. Add a trigger to parse the info in the old field to the related table on insert and update. Then write a script to [parse out existing data. Now you can porperly query but you haven't broken any of the old code. THen you can search for the old code and fix. Once you have done that then just change how code is inserted or udated inthe orginal table to add the new table and drop the old column.
Write a table-valued user-defined function (UDF in SQL Server, I am sure it will have a different name in other RDBMS) to parse the values of the column containing the list which is stored as a string. For each item in the comma-delimited list, your function should return a row in the table result. When you are using a query like this, query against the results returned from the UDF.
Write a function to convert a comma delimited list to a table. Should be pretty simple. Then you can use IN().

MS Acess 2003 - VBA for Update SQL query?

hey guys, could someone show me the simple update query through vb? I need to add new fields to the table (just 3) and add a couple text boxes on a form so that users can add some additional data relative to the record (which is already what this form is based on).
So the first form I have is a form that populates a list, when the user double clicks on a selection from that list, it opens a new form, so that the ID of the the table that is tied to this form that I need to add the these text boxes on (all the combo boxes and text boxes relative to one record are tied to the active form at this point, however there are all unbound. On a button click there is already vb that saves the information to the table). I did not create this however, it was built by someone who is not there anymore, and apparently is better than I at this stuff. My problem is that there is soooo much vb that checks for records, and various sql statements based on case, that I cannot decipher it to its simplest form.
So I was looking for a simple example of an update sql statement in vb so I can try to break this apart.
I need it to update the record based on the ID: sql WHERE RecordID = me.RecordID
I actually thought I knew how to do this based on examples, however every time I try, then try to run on button click, I get a run-time error of SYNTAX error, and the debug just highlights the db.execute(sql) part. So I tried to get the resulting immediate window of the sql statement, and it looks fine to me:
UPDATE tblMain
SET [Name] = "John Doe",
[DATE] = #9/30/2009#,
[TYPE] = "TypeA",
WHERE RecordID = 958;
Can I update a table without accounting for every field in the table (because this one has about 15 plus the new 3, so I am ignoring about 14 fields here, but I do not want to alter those anyway???
So as always, I appreciate the help yall!! Thanks!
EDIT:
Sorry I always forget this....I was actaully trying it DAO....
Dim db as DAO.Database
Dim sql as String
set db = CurrentDb
etc
You were thaaat close! You have a simple extra comma after your last column. Get rid of it and it works fine.
UPDATE tblMain SET
[Name] = "John Doe",
[DATE] = #9/30/2009#,
[TYPE] = "TypeA"
WHERE RecordID = 958;
Yes, you can absolutely update only a few columns rather than all of them. That is a best practice, BTW.
Finally, It's considered bad practice to name your columns after reserved words like "Name" and "Date", but I know you inherited this.
You were wise to include Debug.Print sql in your code. bpayne already pointed out the extra comma in your SQL statement.
I want to point out another trouble shooting technique you may find useful to debug SQL statement problems.
Copy the statement from the Immediate Window, and paste it into the SQL View of a new query. Modify the query in the query designer until you can get it working, then revise your VBA code to generate a matching SQL statement.
In this case you might not have noticed the extra comma. However, you could create another new query and build the UPDATE statement from scratch in the query designer. After getting that one working, you could compare its SQL View to the failing query.

Access sharepoint list data in SQL

I am new to SharePoint development. I have a list, this list has a column that is called Todaysdate. This column needs to be updated daily to today's actual date and since it contains ~20,000 rows I am NOT going to update it manually everyday. Because it's used in a calculation row.
My question is can I just use SQL and update the rows in the UserData table that correspond to the datetime column that I need?
I can query the list of rows by something similar to
Select * from UserData where tp_ListID = 'GUID'
but the data contained in the column datetime3 is not just the Todaysdate information. How do I return just the Todaysdate info?
You really should not query let alone update the SharePoint content database directly using SQL. It is totally unsupported, so if you break something you are left alone, and the database schema may change with future service packs / releases.
Also as noesgard mentioned it in his comment you do not need it to use today's date in a calculated field, see this blog entry on how you can do that.