DataSource Controls :: Return Number Of Rows Affected For Multiple Queries In One SQLCommand?
Feb 22, 2010
I am accepting query/queries from user (our support team) in a text box where user can enter only one query or multiple queries. I need to display result of all queries entered in the textbox. If it is SELECT statement then result of that statement in grid which is done. If UPDATE/INSERT/DELETE then total number of rows affected which is also done but if only one UPDATE/INSERT/DELETE statement is entered in the textbox.
If user enters 2 UPDATE statements and then a SELECT statement then how can I get number of rows affected for individual UPDATE statements just like SQL Server Qury Analyzer displays messages in its result pane.
e.g.
(6 row(s) affected) -- first UPDATE statement
(4 row(s) affected) -- second UPDATE statement
(16 row(s) affected) -- for SELECT statement (grid will also be displayed along with these messages).
I tried almost everything, SqlDataSource (returns count of first statement only), SqlCommand.ExecuteNonQuery and DataAdapter.Fill (returns count of last statement only).
I am working on asp.net 3.5 and in MVC using Linq to SQL classes. In my MVC project i am calling stored procedure using linq to sql that conatin query joining two tables.Below is my code in Partial class
[ Function(Name="dbo.GetUserWiseThreadDetail_SP")] [ResultType(typeof(Thread))] [ResultType(typeof(Reply))] public IMultipleResults GetUserWiseThreadDetail_SP([Parameter(DbType="VarChar(30)")] string userID) { ExecuteResult result = this.ExecuteMethodCall(this, (MethodInfo)(MethodInfo.GetCurrentMethod())), userID); return ((IMultipleResults)(result.ReturnValue)); }
In one of the controller i have made one more class
public class MypostIndexData { public IEnumerable<Thread> Thrds { get; set; } public IEnumerable<Reply> Repls { get; set; } public MypostIndexData(IEnumerable<Thread> thrds,IEnumerable<Reply> repls) { this.Thrds=thrds;this.Repls=repls; } }
finally coding of controller action method
ForumDataContext pmforum = new PMForumDataContext(); public ActionResult MyPosts(string userId) { IEnumerable<Thread>thrds; IEnumerable<Reply>repls; IMultipleResults result = pmforum.GetUserWiseThreadDetail_SP(userId); (this satement doesnot give any result,if i execute stored proc in sql it give the results) thrds = result.GetResult<Thread>(); List<Thread> thrdlist = new List<Thread>(thrds); repls = result.GetResult<Reply>(); List<Reply> replylist; if (repls != null) { replylist = new List<Reply>(repls); } return View(new MypostIndexData(thrds,repls));
I am unable to understand why i am not getting any results?
I am working on a small project. I ran the query below to get the maximum userid and increase it by 1 to create a new userid. However, when I run the query, it does not display the values that I enter into the database using insert statement. For example, if I open the table called login, I can see there are three records. If Irun the select statement, it only return one record which is the one that I enter manually when I first created the table. If I run the same select statement and give it a user id that I created, it returns the correct result.
for some SQL queries I know the expected number of rows that must be affected by UPDATE, DELETE or INSERT statements. It could be a number such as 1 or a range such as 0-1 or 1-2 etc.
What is the best way to enforce that this is the number of affected rows and that otherwise an exception is thrown?
Also, can I limit the number of rows affected for UPDATE/DELETE/INSERT to say 1 row and otherwise get an exception?
In general terms, which way will require more server processing power and which way will return faster queries... if there is even a difference. The client's database is still on the dev MS SQL server but is essentially but the client has decided they want to collect two more fields of information. Both fields are going to be collection text data and probably varchar(300) will be sufficient for the column data types. Would it ultimately be more efficient to
Add two additional columns in the main table so the data is entered directly into each record, or Add an additional table (or two) with a foriegn key to the primary table that holds the info for each new field The SELECT statement for the main table could be modified to either add the new columns (a.NewColumn1 and a.NewColumn2) or it could contain nested SELECT statements that pull in the data from the other tables. Custom daisy-chained Insert or Update statements would be no problem and a cascade Delete relationship would take care of any deletions. I'm just trying to figure out which would be better from a performance standpoint?
I'm using a table adapter to perform my queries. I'm sure I'm missing something really easy, but I can't figure out how to return the number of rows returned from a query to a dataset used for a GridView.Basically, I want to display a message "No records exist for your selection" by testing for ReturnedRows = 0.
Excerpt of C# code:
handoutDSTableAdapters.handoutTableAdapter handoutsAdapter = new handoutDSTableAdapters.handoutTableAdapter(); gv_Handout.DataSource = handoutsAdapter.GetDataByDoctor(Int32.Parse(ddl_MD.SelectedValue)); gv_Handout.DataBind();
I have the below c# method to populate an asp.net frontend control from a linq query. The only valid way I seem to be able to reference the query variable "result" is if I use the attribute "SingleOrDefault" but unfortunately this forces my query to return just the one single set of data for every unique primary key UserId value from my query, where I require the unique data attached to each different UserId value to be returned, how can I achieve this using linq? Note, I am passing the UserId value into my method from a public variable which is also below.
On one line the Code may be OX01 and shape Hexagon for example
Then the next line the code is still OX01 but the shape is Triangle.
What I want to do is be able to pull back data for every line that is equal to a unique Code column. in the above example OX01 Hexagon would be returned but not OX01 Triangle.
I've tried using SELECT DISTINCT but it brings back all records and doesn't filter them correctly.
I've considered having another column which could have Child/Parent data so that only the parent is returned but i'm sure there must be a better way of doing this.
i am using sql 2005 db. i have created a table called attendance where i store employee attendance, i want to separate number of absence for a particular employee and particular month. It have to show number of absence of a particular employee and particular month. Also i have to count no of absence. for eg: if an employee absence for two days for a particular month, it have to display 2 count.. how to do this? also how to write the store procedure for this?
I now want to count the number of returned rows so that I can say:IF rows_returned = 1 then login else deny I think you get the idea.P.S. I know this is not the most secure method of login but I do not want to use the ASP.NET configuration manager as I like to understand what is happening. I am starting off very simple and I shall work up from there.
I have a web service method which i'd like to return multiple rows from a datatable.
I am familar with returning values from web service methods but not multiple rows from a datatable. What is the best way to go about doing this? Do I need to return an array or list<>?
My code method is setup like this.
[WebMethod] public void UpdateBold(int count, float lat, float lng) { DataTable dt = new Gallery().DisplayNearestByLatLong(count, lat, lng); // return code here }
i hav a gridview ... now i need to add certain number of rows... say 5 rows .... which would b blank .. the rows consist of itemtemplate of textboxes.. here is the grid ...
[Code]....
now i am binding this gridview with certain data say :
[Code]....
now the problem is if the row contains 2 data originally ... then it alwz shows up with only two rows with binded data... but what i want is 2 (databinded rows ) + 5 (empty rows with textbox ) = 7 grid view rows .... now how to do it ... after i hav binded the textbox with data already....with some empty columns corresponding to the above code ?
Is it possible to return multiple values from a stored procedure? Basically, if I execute the stored proc on the C# side, then on the SQL side I do multiple calculations. Let's say I have 5 int values I need returned to the C# side.
I have a method in my button code to run the query, it returns a value and save it to a Session variable to be used later, this code works.Dim connString As String = ConfigurationManager.ConnectionStrings("DBConnectionString").ConnectionStringUsing myConnection As New SqlConnection(connString)
Const sql As String = "SELECT Rate1 FROM Rates WHERE (LayerID = '01')" Dim myCommand As New SqlCommand(sql, myConnection) myConnection.Open()
I currently have a datasource that displays several rows on an editable gridview where the user can update several rows at once. Each row contains an ActivityTotal column per row. I'd like to sum all the ActivityTotal rows displayed and have one totalValue.
Dim conn As SqlConnection = New SqlConnection("server='h'; user id='w'; password='w'; database='w'; pooling='false'") conn.Open() Dim query As New SqlCommand("DECLARE @investor varchar(10), @sql varchar(1000) Select @investor = 69836 select @sql = 'SELECT * FROM OPENQUERY(db,''SELECT * FROM table WHERE investor = ''''' + @investor + ''''''')' EXEC(@sql)", conn) dgBookings.DataSource = query.ExecuteReader dgBookings.DataBind()
I need to add in an If statement basically saying if the SQLCommand query returns no rows then show a specific label Can this be done?
I want to add array list in sqlcommand.but i don't no how to pass it. My code is.
ArrayList ProductId = new ArrayList(); for (int i = 0; i < ShipmentPackage.Items.Count; i++) { ProductId.Add(ShipmentPackage.Items[i].ProductVariant.ProductID); } select * from TableName where id= ProductId(ProductId is my Arraylist)
've made a quiz/score system where a user will go through categories (page by page using querystring) and after submitting his/her score i redirect them to the next page where i show them their score and i also want to show "out of x number of questions".
I've made a query in my dataset:
[Code]....
I get an error about "Cannot implicitly convert type "void" to "object". This is the first time i am trying to use the SQL COUNT so i am almost certain it has something with the way i write it in my BLL, but i cannot figure out why or how to write it as it should. Hopefully you can direct me in the right path?