how to view/display data from database using different parameter - c#

Could you support me to display data from database using different parameter. Just try to explain I have
=> Five========= **From [] to []** and one another drop down.
=> I need to have accept all drop down values from the user and doing searching and displaying the result on the same page to searching interface. HOW CAN I DO THIS ?
PLEASE HELP ME and may I get source code please ?

Hi Have a look at stored Procedures -
call a stored procedure, and pass in the parameters to get the result.
http://technet.microsoft.com/en-us/library/aa174792(v=sql.80).aspx

Related

Rethinkdb .net update value

I'm looking at updating stored values in a RethinkDB using the C# RethinkDB.Driver library and I'm just not getting it right.
I can achieve an update by getting the result, altering that object then making a separate call to update with that object. When there are many calls to a record to update like this, the value being updated elsewhere whilst the application is working with the record.
TestingObject record = r.Db("test").Table("learning").Get("c8c54346-e35f-4025-8641-7117f12ebc5b").Run(_conn);
record.fieldNameIntValue = record.fieldNameIntValue + 1;
var result = r.Db("test").Table("learning").Get("c8c54346-e35f-4025-8641-7117f12ebc5b").Update(record).Run(_conn);
I've been trying something along these lines :
var result = r.Db("test").Table("learning").Get("c8c54346-e35f-4025-8641-7117f12ebc5b").Update(row => row["fieldNameIntValue"].Add(1)).Run(_conn);
but the result errors with Inserted value must be an OBJECT (got NUMBER):101 which suggests this is only passing the field value back instead of updating the object.
Ideally I'd like to update multiple columns at once, any advice is appreciated :)
This is an example that works in the ReQL data explorer. You can chain as may filters before the update as you want. I assume this will translate to the C# Driver, but I dont have any experience with that.
r.db('database').table('tablename').update({clicks: r.row("clicks").add(1)}).run().then(function(result){ ...
Thanks T Resudek your answer and a clearer head helped emphasised the need to map the calculation to the property.
Looking at the javadocs for update it has HashMap method which I followed with the c# library and it works.
var result = r.Db("test").Table("learning").Get("c8c54346-e35f-4025-8641-7117f12ebc5b").Update(row => r.HashMap("fieldNameIntValue",row["fieldNameIntValue"].Add(1))).Run(_conn);
I'd be interested to know if this is the right way or was a better way.

Pass parameter to return complex result of stored procedure

I am successfully able to add my stored procedure via the Update Model from Database wizard. For a stored procedure that does not require any input parameters, I can easily retrieve complex queries.
However, I cannot return the results of my stored procedure which requires an input parameter. I try passing it in my C# class and storing it in "result" as follows:
//20 is the value I am passing into my stored procedure
IEnumerable<MyClass> result = _db.MyStoredProcedure(20).Select(x => new MyClass(){....});
return result;
but I am unable to retrieve the results, as 'int' does not contain a definition for 'Select'.
I believe this is because I need to actually have a result type in my Complex Types in the Model Browser. I can create custom ones, however, I cannot get the return values from my stored procedure into these values as there is no way for me to set that parameter.
I do notice in my stored procedure that it has the variable that needs to be initialized in the "Stored Procedures / Functions" folder under the Database.Store location. Is there any way I can set that to be the value that I want to pass? (I checked properties in the model browser for that specific ID)
Or, maybe I am thinking about this the wrong way.
Any help would be great.
Thanks in advance.
My solution for this problem is simple really.
So passing a parameter works as it should. I should actually be passing 20 into the aforementioned example in my question. The problem lies in with the stored procedure itself.
Because the ADO doesn't have a built-in way to set/pass parameters, you need to manually modify your SQL query, to do what the stored procedure is supposed to do. So I modified my stored procedure to print out the contents of what it's supposed to execute, using
print result
Then copied and pasted the result of your stored procedure from the "messages" (so my select with the parameter I would normally pass). Then I re-edit the function import in my Model Browser, and hit "Get Column Information" and get the complex result type that I specifically want.
It works!

Stored procedure validation best approach

Alright so I have a user table that I would like to check against. This user table has a username, email, and accountnumber. Would I would like to do is check if anyone of those has been taken and if it has return if it has been taken.
I was thinking about doing an array example
CHECK AGAINST TABLE
IF USERNAME MATCHES INSERT #ARRAY "TRUE"
IF EMAIL MATCHES INSERT #ARRAY "TRUE"
ETC.
Then on the C# side I will call the array and check by index
If registrationValidationArray[0] = "true"
{
ViewBag.UserNameTaken = "True"
return view("Registration")
// On cshtml post error next to username that it has been taken
}
So my question is does this approach sound logical and sound like it will work or is there another approach that might help me here procedure wise. Another developer suggested an incremental for the procedure so if username is taken +1 and then on the C# side display according to the numeric value but I couldn't wrap my head around that one. Anyone know of a better way or see a flaw in my logic?
If you are using the Telerik OpenAccess ORM and you have a stored procedure returning the details from the check I would suggest you to map this stored procedure to a Domain Method, then OpenAccess will return you an object with nicely named properties. You can use the returned object instead of the array. This will make you code more readable and easier to maintain. Now you should remember what means index 0 and index 1, even if you define the indexes as constants it will not be an eye-catching code.

Can I pass a .net Object via querystring?

I stucked at a condition , where i need to share values between the pages. I want to share value from Codebehind via little or no javascript. I already have a question here on SO , but using JS. Still did'nt got any result so another approach i am asking.
So I want to know can i pass any .net object in query string. SO that i can unbox it on other end conveniently.
Update
Or is there any JavaScript approach, by passing it to windows modal dialog. or something like that.
What I am doing
What i was doing is that on my parent page load. I am extracting the properties from my class that has values fetched from db. and put it in a Session["mySession"]. Some thing like this.
Session["mySession"] = myClass.myStatus which is List<int>;
Now on one my event that checkbox click event from client side, i am opening a popup. and on its page load, extracting the list and filling the checkbox list on the child page.
Now from here user can modify its selection and close this page. Close is done via a button called save , on which i am iterating through the checked items and again sending it in Session["mySession"].
But the problem is here , when ever i again click on radio button to view the updated values , it displays the previous one. That is , If my total count of list is 3 from the db, and after modification it is 1. After reopening it still displays 3 instead of 1.
Yes, you could but you would have to serialize that value so that it could be encoded as a string. I think a much better approach would be to put the object in session rather than on the URL.
I would so something like this.
var stringNumbers = intNumbers.Select(i => i.ToString()).ToArray();
var qsValue = string.Join(",", stringNumbers);
Request.Redirect("Page.aspx?numbers=" + sqValue);
Keep in mind that if there are too many numbers the query string is not the best option. Also remember that anyone can see the query string so if this data needs to be secure do not use the query string. Keep in mind the suggestions of other posters.
Note
If you are using .NET 4 you can simplify the above code:
var qsValue = string.Join(",", intNumbers);
Make the object serializable and store it in an out-of-process session.
All pages on your web application will then be able to access the object.
you could serialize it and make it printable but you shouldn't
really, you shouldn't
The specification does not dictate a minimum or maximum URL length, but implementation varies by browser and version. For example, Internet Explorer does not support URLs that have more than 2083 characters.[6][7] There is no limit on the number of parameters in a URL; only the raw (as opposed to URL encoded) character length of the URL matters. Web servers may also impose limits on the length of the query string, depending on how the URL and query string is stored. If the URL is too long, the web server fails with the 414 Request-URI Too Long HTTP status code.
I would probably use a cookie to store the object.

Best Practice - Handling multiple fields, user roles, and one stored procedure

I have multiple fields both asp:DropDownList's and asp:TextBox's. I also have a number of user roles that change the Visible property of certain controls so the user cannot edit them. All of this data is saved with a stored procedure call on PostBack. The problem is when I send in the parameters and the control was not on the page obviously there wasn't a value for it, so in the stored procedure I have the parameters initialized to null. However, then the previous value that was in the database that I didn't want changed is overwritten with null.
This seems to be a pretty common problem, but I didn't have a good way of explaining it. So my question is, how should I go about keeping some fields from being on the page but also keeping the values in the database all with one stored procedure?
Apply the same logic when chosing what data to update as the logic you're actually using when chosing what data (and its associated UI) to render.
I think the problem is you want to do the update of all fields in a single SQL update, regardless of their value.
I think you should do some sanity check of your input before your update, even if that implies doing individual updates for certain parameters.
Without an example, it is a little difficult to know your exact circumstances, but here is a fictitious statement that will hopefully give you some ideas. It is using t-sql (MS SQL Server) since you did not mention a specific version of SQL:
UPDATE SomeImaginaryTable
SET FakeMoneyColumn = COALESCE(#FakeMoneyValue, FakeMoneyColumn)
WHERE FakeRowID = #FakeRowID
This basically updates a column to the parameter value, unless the parameter is null, in which case it uses the columns existing value.
Generally to overcome this in my update function
I would load the current values for the user
Replacing any loaded values with the newly changed values from the form
Update in db.
This way I have all the current plus everything that has been changed will get changed.
This logic will also work for an add form because all the fields would be null then get replaced with a new value before being sent to the db. You would of course just have to check whether to do an insert or update.

Categories

Resources