How to Update database from UI which has many textboxes and CheckBox in asp.net and SQL Server? [closed] - c#

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am new to ASP.net. I have a webform where I have 40 text box 18 drop downs and 29 checkbox. I need to load the all data from these controls to SQL server database table using stored procedure. I dont want to pass all the parameters one by one and update the table.
Is there any other short way to do it? Please help

I am not sure I follow your question here?
The simplest way, would be to do a model object. Create a method that accepts the model object that includes all the data from your form. This methods feeds the stored procedure call.
This is the common way to do it. And the fastest. You need to assign the data to data-fields. There is no way around it really. But of course it can be done in many different ways.
So you want a more specific answer, you will have to elaborate what you mean by:
"I dont want to pass all the parameters one by one and update the
table."
I can't come up with a scenario where it would be desireable to pass each parameter one by one into an update statement...
but you will have to mention each field in your SQL stored procedure, that you want to update.
if that is your only question:
UPDATE table-name
SET column-name = value, column-name = value, ...
WHERE condition

If you have used a model in your view and used Razor syntax to render the relevant controls than if you have a form defined, posting back to the controller that accepts the same model as it's parameter will handle the binding of all the properties back to the model.
You then either parse the model or use the properties and send them down to entity framework, or any other ORM that you might be using, and have it handle the update operation.
[HttpPost]
public ActionResult YourPostMethod(YourModelUsedInView model)
{
// process your model here and send it down to the DB.
}

Related

I'm trying to create a Multiple choice quiz in c#/winforms, using sql to store retrieve the questions [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I wish to create a simple Winforms app that can access questions from a SQL table called "questions table" as well as table called "answersTable". I would then like to assign the question result to a single textbox on screen and have the 3 "answersTable" results be mapped to 3 individual buttons, I have already made all the forms and achieved this using a JOIN query to join my tables and then mapping the controls (by mapping the controls via the dataReader,
i.e
while data.read()
{
button1.text = datareader.getValue(0)
};
my Winforms which I wish to populate from SQL tables
My code to populate the Winforms
This is what I would like to make
However this only works for the last question and answer in the table and gives me no control of displaying the previous or next entry in the questions and answers tables.
My next thought was to try and take the data from the SQL tables add them to a list box and then use the list box to map the questions and answers to my forms controls but am just unsure if this is the best way to go about it.
Any help would be much appreciated.
There is a lot going on with this code. Let me start with your problem and continue with the rest.
The load method. My guess is what you call in the on initialization of the form. You are reading until the last value from the reader and never look back. What you could do instead.
Create a new class QuizQuestion which will depict your database return structure.
pubic class QuizQuestion {
public string Question { get; set; }
public string AnswerA { get; set; }
// continue for the rest
}
Now in your class, create a member
List<QuizQuestion> quizQuestions;
And last in your while datareader clause, create one QuizQuestion object for every row and add it to your list.
This way you will have all the values saved. Now create a function that will get the last index and show the right list item.
Also create an event for each button to use the previous function to go to the next answer.
Don't forget to close the connection object you are creating. Check the keyword using

C# / SQL Server : MVVM insert data into multiple tables [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I work on a small project just to get to understand the mvvm model in C# better.
I work with a Microsoft SQL Server database with three tables: customer, location and address.
Every customer can have one or more locations, and every location has a specific address.
My current thought how to accomplish this:
First insert the customer.
To insert the location, get the highest customer_id and insert the location with the max(customer_id)
Then, to insert the address, get the max(location_id) and insert the address, with the location id
Is there a better way to do this?
I haven't found any tutorial with an example of inserting data into more than one table, especially not using SQL Server.
And my next problem is: what should I bind to my TextBoxes, so that I can insert the content of it?
I thought about having a save button. This button would then execute a method, where I insert the data from the bound TextBoxes. Should I do this with commands, instead of writing a method?
Thanks already!
Sql server has a OUTPUT clause which you can use
something like
INSERT INTO MyTable VALUES({CustomerName})
OUTPUT INSERTED.ID
then you can store the inserted customer's actual ID and do the rest inserts in separate queries.
As for your second question yes you should do it with Command binding to a method in your View model
The most advisable for your situation is to use a transaction.
Example.- https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqltransaction(v=vs.110).aspx

Update a record in ASP .Net MVC [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Today in the interview I faced a question tat I had no clue about. Being a newbie into ASP .Net MVC I had no other way but to ask the experts about it.
"In your MVC Application ,data are being stored into a RDBMS System. In a .cshtml page ,there is a button to update the existing records into DB.
Now suppose two users from different parts of the Globe are at the same time are trying to update a common record. But by the the time the second user hits the submit button, you already submitted the update. In that case the second user , when would be pressing the submit won't be able to submit. Instead the page would reloaded for the second user with the updated info, discarding the changes.And then only he would be able to go on with the update"
How could you achieve that in ASP .Net MVC?
I though thought it might be something from the DB Side coding also , but I have no clue how to achieve the same.
You need to implement a strategy for managing concurrency. How you do this depends largely on the business rules of the application and the type of conflict that arises when two or more people attempt to change the same record.
Most often, you will add a column called RowVersion which will be a timestamp type. Whenever you display records to be updated, you also select the current RowVersion value and usually store it in a hidden field. The update operation will include the RowVersion field, which gets a new value, but before you commit an update operation, you compare the RowVersion value you have with the current one in the database. If they are different, someone else has updated the row during the time that it took you to get the record to be updated and then tried to change it. You determine how to proceed based on the application's business rules.
Asynchronous Tasks do the database operations , so even if another user tries to update , the first process runs in the background asynchronously

ViewData, ViewBag and TempData violates MVC? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 7 years ago.
Improve this question
If we all look at the mvc diagram, the view shouldn't know the models the controller gives out to (according to msdn mvc overview https://msdn.microsoft.com/en-us/library/dd381412(v=vs.108).aspx). To me, the ViewData, ViewBag, and TempData violates this and I have been scratching my head at why these three concepts was introduced.
Maybe I am getting it wrong? I can easily pass data to the view using the models instead of these three.
Would like to know other people's opinion.
If you down vote please comment why.
thank you
I can easily pass data to the view using the models instead of these
three.
Yes definitely you can and in such case your view becomes a strongly typed view and you can see starting of your view has a line
#Model entityName
What if you don't want (for some reason) your view to be strongly typed at all. How do you think in such case you are going to pass the data to view.
That's the purpose of ViewData or ViewBaG or TempData.
the view shouldn't know the models the controller gives out to
I think you misinterpreted this line which can't be agreed upon. If your view needs data to be displayed in controls (may be an edit view), then from where it's going to get the data if it doesn't know from where to read the data.
View someway or other way must know the model data (either in form of directly passing the Model object or using any of those 3 technique).
EDIT:
Per your last comment:
can i not use this?
public class TestModel
{
public dynamic testproperty { get; set; }
}
Yes, probably you can but that's not a proper way. Try creating a strongly typed view with model as TestModel and use any scaffold template and see if scaffolding can generate a proper template.
It will not, since scaffolding internally uses reflection to go through your model properties and accordingly generate controls for those properties/members. Since you have a dynamic property, it will not be able to reflect that property. change the type to a static type like String and see it will generate a #Html.DisplayFor(modelItem => item.testproperty). That's the issue.
Even if you don't use any scaffolding template and generate controls by your hand; what type of control will you generate for your property? since it's tped as dynamic it could be anything (string, int, datetime, email...).
ViewBag or ViewData are state management technique to pass on small amount of data b/w the views or controller to views. You should always pass your data as Model object to your view as correctly stated by #SteveHarris.
Hope this makes t clear to some sense.

Sort a gridview without call database [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to sort a gridview without call the database. The goal is to stay on the same page and sort datas with buttons up/down at the right of the gridview ( in two templatefields) and to do only one call to the database a the end (submit button) . It will be and update with stored procedure.
It is possible to do that with the Gridview's properties ? Someone say to me to use a datatable to sort, but i can't transfer gridwiew's data to a table it don't work, i've ever try with differents ways. i think to an other solution, it's to put gridview's data in a XML file and modify it with XSLT. What's yours ideas to implement that ?
Cordialy
Julien
According to this:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.enablesortingandpagingcallbacks(v=vs.110).aspx
you cannot handle client-side sorting or paging if you are using template fields.
You would be better off not using an asp.net server control.
If you own or can purchase Kendo UI, or if the free GPLv3 license is appropriate for your project, you can use the Kendo UI grid. It provides easy client-side sorting filtering and paging.
http://demos.kendoui.com/web/grid/index.html
Of course, googling will turn up many other vendors with similar offerings.
Finnaly i got the solution. I pass Gridview's datas to a DataTable, then I rebind Gridview with the DataTable. The Datas are temporary stocked with this manner. Then i compare the first DataTable with the last, and take only the modified lines to submit to server. It's work good !
Thanks for you anwser !

Categories