C# LINQ-to-SQL - not updating table with stored procedure - c#

SOLVED
I have problems with Linq to SQL inserting/updating/deleting dataset from table with stored procedures.
I have checked data connections, points on the same table that has been created when adding connection
I'm using Linq-to-Sql classes designer for managing behavior of sprocs for my tables (insert, update, delete)
I have same app created with different code style and everything works just fine but i can't include location change (when someone else installs app) in my .cs files, so I want to do it on "right way" and make it work for everyone.
using sql server 2012 and vs2010
Here is my problem.
this is included in form as reference for DataContext
private ZavrsniradDataContext poziv = new ZavrsniradDataContext();
//I've tried without private, and including this into methods where I'm using it and nothing.
after I declare some variables for sending it to sproc I call sproc for altering the table..
poziv.p_IzmjenaMjesta(#ID, #Naziv, #PP, #IDZupanije);
poziv.SubmitChanges();
note: I'm sending ID number of the dataset I want to change, for comparison..
Alter proc [dbo].[p_IzmjenaMjesta]
#ID int,
#Naziv varchar(30),
#PP int,
#IDZupanije int
.
.
.
Update Mjesto
SET Naziv= #Naziv, PostanskiPretinac = #PP, IDZupanije = #IDZupanije
WHERE ID = #ID;
After I call the sproc, my table is being refreshed, and all changes are visible. But after a while, all changes dissapear. I believe all my sprocs and functions are working properly, with my table, but somehow I'm doing something wrong. everything works perfecrly when I run sprocs in Management studio.
My other app contains this definition in beggining of the form, and everything works perfectly. updateing, inserting, deleting! But i don't want that solution because then I have to create fixed location for my database on every PC. I just want to install it and run it :)
public partial class Mjesta : Form
{
ZavrsniRad poziv;
public Mjesta()
{
InitializeComponent();
poziv = new ZavrsniRad(#"c:\zavrsni_rad\Zavrsni rad.mdf");
}
NOTE:
I'm using exact copy of both databases on different locations with same sprocs/functions/dataset for different apps, and one is working, the other one is not working.
and that grinds my gears :(

I have solved it.
app is running and working properly...
I forgot on that little thing :(
Problem solver, add all rights on modifying your database to every account you have.
That also includes rights to log file.
(i dont have rep points for posting pics)
http://www.pohrani.com/f/v/AQ/2bPLiVcP/slika.jpg

Related

SQL Server prevent only specific table from updating based on parameter passed from C# controller

In our .net application, we have a tool that allows you to type SQL in a browser and submit it, for testing. In this context, though, I need to be able to prevent testers from writing to specific tables. So, based on the parameter passed from the controller (InSupportTool = true, or something), I need to know if SQL Server is allowed to make updates or inserts to, say, an accounts table.
Things I've tried so far:
I have tried looking into triggers, but there is no before trigger available, and I've heard people don't recommend using them if you can help it.
Parsing the passed SQL string to look for references to inserting or updating on that table. This is even more fragile and has countless ways, I'm sure, of getting around it if someone wanted to.
Check constraint, which is the closest I feel I've gotten but I can't quite put it together.
For check constraints, I have this:
ALTER TABLE Accounts WITH NOCHECK
ADD CONSTRAINT chk_read_only_accounts CHECK(*somehow this needs to be dynamic based on parameters passed from C# controller*)
The above works to prevent updates to that table, but only if I put a check like 1 = 0. I've seen a post where people said you could use a function as the check, and pass parameters that way, but I'm at the limit of my familiarity with SQL/.net.
Given what I'm looking to do, does anyone have experience with something like this? Thanks!
Since the application is running under a different account than the end user, you could specify your application name in the connection string (e.g. Application Name=SupportTool) and check that in an after trigger, rolling back the transaction as needed:
CREATE TABLE dbo.example(
col1 int
);
GO
CREATE TRIGGER tr_example
ON dbo.example
AFTER INSERT, UPDATE, DELETE
AS
IF APP_NAME() = N'SupportTool'
BEGIN
ROLLBACK;
THROW 50000, 'This update is not allowed using the support tool', 1;
END;
GO
INSERT INTO dbo.example VALUES(1);
GO

Automatically inserting value from another table in lightswitch

I am creating a desktop, C# application in Lightswitch. I have a DB file with a table called Reports and another table called StatusList. There are going to be 3 options in StatusList table - Unresolved, In Progress and Resolved. The Report table has a couple of not important fields and a field "Status", which is supposed to hold one of the three values from StatusList table
Now, when the user will create a new Report, I need the application to automatically insert the "Unresolved" value as the "Status" without user being able to change it.
I have tried using this method
partial void Reports_Created()
{
this.Status = "Unresolved";
}
but it is not working. I guess it is because I am trying to assign a String value to the field which is populated from another table.
I am using Visual Studio 2013 and this is my first Lightswitch app as well.
Thank you in advance for any help.
It sounds like your trying to pass parameters between screens. Beth Massi does a good job explaining how to achieve that in this video. And here is another MSDN link to a similar question
Shouldn't your code before like:
This.report.status = "Unresolved"
I.e. The report entity is not referenced by "this" (that refers to the screen).

Resizing column size of a table in DB and effecting with the dataTableAdapter on a Dataset.xsd

I'm asking about a problem with C# and Visual Studio 2012; I'm trying to resize a column of a table in my database, effect with the dataTableAdapter on a Dataset.xsd
I'm using DataTableAdapter from a stored procedure with a SELECT statement to populate a DataGridView, reports and many more.
I created the table long time ago, but now there is an a problem with it.
I had to increase the length of a column and I changed the appropriate column length of the DataTable also. But it didn't give me the solution. still whenever I Fill or Get data through that DataTableAdapter it response with the previous (original) size of the column.
But when I create a new DataTable and redirect my code to the new DataTableAdapter, it works.
Why is this happening ?
Because redirecting code to the new DataTableAdapter is little bit difficult because I don't know all the places it use in the entire solution.
And also if can please tell me how to add new column to the table and deal with the DataTableAdapter with it also.
Thanks and waiting for your reply.
after doing small research with my friends , i got an proper way to fix this error.
not even re-sizing, but also any other change with the database Table or storedprocedure you have to reconfigure the dataTableAdaptor, unless it just work as , when it was created.
it will continue with major errors or sometimes, it will function incorrectly, even you cant figure out there is an error.
so whenever you do any change with the database Table or storedprocedure go to the dataSet.xsd , where the dataTable locate and right on the dataTable , then configure it again.
this saved me and worked.

TableAdapter Insert not persisting data

I created a service-based local db via Visual Studio 2013 Express edition.. The connection string, dataset and TableAdapter were added automatically.
On click of a button, I am trying to insert some data by calling TableAdapter.Insert
As it is I already have a dataGridView's datasource bound to the dataset, So I immediately see that the data was inserted in table properly at run time, But when I close the application, The actual DB dosn't contain the data. Therefore, The data isn't actually inserted in DB.
According to http://msdn.microsoft.com/en-us/library/ms233812%28v=vs.110%29.aspx
With insert, you have to only call insert, yet I am calling Update and AcceptChanges on table, for safety, well I tried the first way shown in link (i.e. creating a row and adding it to dataset then calling update) as well, but it seems the data isn't being inserted in DB at all.
Finally, the insert code, rds is DataSet and ta is TableAdapter
private void AddBtn_Click(object sender, EventArgs e)
{
ta.Insert("foo", "bar", 2, "zing", "tada");
ta.Fill(rds.reminders);
rds.reminders.AcceptChanges();
ta.Update(rds.reminders);
}
It turned out that, as I was using the VS compiled application each time the mdf database was being overwritten, hence the changes I made were completely erased,
The possible solution could be one of following
1)Change the connectionstring to point to database that is in Debug folder, which wont be overwritten each time you compile and run the application
2)You could simply let the connectionstring be as it is and just test it through detached compiler mode.
I was able to figure this out due to the following stackoverflow link I suddenly sumbled upon after 2 days.
Database changes do not persist after ObjectContext.SaveChanges() is called
ClearBeforeFill Property
http://msdn.microsoft.com/en-us/library/bz9tthwx.aspx
"By default, every time you execute a query to fill a TableAdapter's
data table, the data is cleared and only the results of the query are
loaded into the table."

Database changes do not persist after ObjectContext.SaveChanges() is called

I am trying to insert a record into a table using an Entity Framework. I made my database (mdf file), made a model from the database (edmx), and now I am trying to insert a record.
Users Brad = new Users
{
UserInitials="BCH",
UserName="Brad"
};
using (WeighProgramDatabaseEntities wDB = new WeighProgramDatabaseEntities())
{
wDB.Users.AddObject(Brad);
int res = wDB.SaveChanges(System.Data.Objects.SaveOptions.AcceptAllChangesAfterSave);
List<Users> lwt = wDB.Users.ToList(); //call to look at the inserted data
}
When I follow this procedure it appears everything works fine. My wDB.Users.ToList(); returns one record which is the one I just inserted. However, if I run my code again, removing the insert stuff and just call the query (leaving just) List<Users> lwt = wDB.Users.ToList(); my query returns no records. What should I be doing differently to get my changes to persist? Why did my records "go away"?
WeighProgramDatabaseEntities inherits from ObjectContext.
This is my first attempt at using EF stuff so some of my verbiage might not be correct and I might not have included something important - let me know.
Edit:
I think my problem is that my Insert/Update/Delete functions are not mapped. But I'm not sure how to create a stored procedure for them to map to...
Edit2:
Ok, phew, I figured out the stored procedure mapping. and now that I have an INSERT mapped, and I call AddObject it will actually be inserted.
A little more complicated than I would have expected. For example if I right click on a table in my Database Explorer and select New Query it brings me to a great query designer window which let's me create Inserts, Selects, Updates... but there is no Save button! It's grey out. I need to write them from another route with does not let me generate Inserts/Deletes/Updates via a GUI. Which is ok I guess because they are not much text, but still a bit of a hassle.
I think that your ConnectionString points to the mdf in the Debug/Release folder of your solution, and that file is probably overwritten each time you build your application that's why you think your changes are not persisted. Try setting the ConnectionString to point to the mdf in the project folder (the one that is actually reference by the project).
You can also set the "Copy to output directory" file property to "Copy if newer" rather than "Copy always". If you update the schema of the database in your project then it becomes newer than the database file in the debug directory and it will be overwritten on compile.

Categories

Resources