Read InsertCommand before it gets sent to SQL - asp.net

Good morning,
I have an ASPX page with an UpdatePanel containing a Gridview and SqlDataSource control. In the SqlDataSource control I specify an InsertCommand. I would like to be able to read the InsertCommand that my page will send to the SQL database. Reading the following
mySqlDataSource.InsertCommand
in code-behind gives me the InsertCommand with #parameters rather than the actual value for each parameter that will be sent to SQL.
Is there a way to read the final InsertCommand that will actually be sent to the database for execution?
EDIT: Please note, my question could apply to Select, Update, Insert or any command that is sent from my ASPX page to SQL. The command must be converted to a language that SQL can interpret and execute and that is the very version of the command that I am trying to read.
Thank you.

I guess you should subscribe to the Inserting event of your DataSource.
Then, in your Inserting_Handler, you may browse the your DataSource.InsertParameters Collection
private void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e)
{
var txt = e.Command.CommandText;
//the parameters names and values are in e.Command.Parameters
}
Hope this will help

Related

XtraReports Not working in V11.2.8 Version

Currently i am using Devexpress XtraReports V11.2.8. My problem is i am able to populate report in V11.1.6 but i am unable to populate report in V11.2.8 it showing only scroll bar in the reportviewer when i debug it is properly mapping and binding the data. FYI: I need to bind/Initialize reportviewer in button click only. Devexpress people said that i need to initialize reportviewer with the report in page_load it self but we can't do that because we have large amount of data exist and we need to populate report with particular input criteria matched result only. Please advice.
FYI: refer this link for devexpress response on the above issue http://www.devexpress.com/Support/Center/Issues/ViewIssue.aspx?issueid=Q362696
I can't find any solution about version problem without additional information.
but, the 2nd problem you told, i can give a possible solution to that. You can take necessary input you need for the report and then click on the button to preview the report regarding on inputs. It is better to create a stored procedure in your database for this report.
Suppose, you need to show a report for specific 'Cust_Id'. In the 'Form1' put a text field for 'Cust_Id' and a 'button'. On button click event put the following code,
private void simpleButton1_Click(object sender, EventArgs e)
{
Portfolio_XtraReport port = new Portfolio_XtraReport();
string custID = textEdit1.Text.ToString();
port.Portfolio(custID);
port.ShowPreview();
}
now in the 'Portfolio_XtraReport.cs' put the following code,
public Portfolio_XtraReport()
{
Portfolio("N");
}
public void Portfolio(string custId)
{
if (custId != "N")
{
InitializeComponent();
sP_PORTFOLIOTableAdapter.GetData(custId);
}
}
Here, sP_PORTFOLIOTableAdapter is the table adapter for the dataset, I used a stored procedure which takes 'Cust_Id' as input.

SQL datasource /datagrid from code behind

I am trying to extract the username from the authentication cookie and use it to send a request to the database to pull in the the information for that particular user using a datagrid or sql data source.
I know how to implement a sql datasource /datagrid in the front end (asp.net) but I am confused about how to do it in the code behind so that I can utilize the value of the username there
The code I am using to pull in the username is
IPrincipal p = HttpContext.Current.User;
string userid = p.Identity.Name;
Thanks
** This is what you put in the code behind
datagrid1.datasource = MethodToGetTheDataSourceFromTheDatabase(p.Identity.Name);
datagrid1.databindI();
**
Then you would just have MethodToGetTheDataSourceFromTheDatabase execute the sql statement that you want executed and return the datatype(dataset, dataadapter, List<>, etc...)

How do I reference the databound control from an ObjectDataSource event?

Take for example a DetailsView control with an ObjectDataSource as its datasource.
Normally in the DetailsView.ItemUpdated event I would grab a reference to the details view by casting the sender:
DetailsView dv = (DetailsView)sender;
In certain situations it becomes necessary to handle the event inside the ObjectDataSource.ItemUpdated event. In this case sender is now of type ObjectDataSource. What I want to be able to do is write clean code that isnt hardcoded like
Label label1 = DetailsView1.FindControl("Label1");
I looked over the documentation and also did some searches but couldnt find how I would write some code like the following:
protected void ObjectDataSource1_Inserted(object sender, ObjectDataSourceStatusEventArgs e)
{
ObjectDataSource ods = (ObjectDataSource)sender;
DetailsView dv = (DetailsView)ods.SOMETHING_HERE;
}
Does anyone know what I should be putting in the SOMETHING_HERE in the snippet above?
That's happen because the "OnInserted" event is suppose to be an event examine the values of a return value or output parameters, or to determine whether an exception was thrown after an Insert operation has completed. The return value, output parameters, and exception handling properties are available from the ObjectDataSourceStatusEventArgs object that is associated with the event.
What you can do here is just call ObjectDataSource.select() that returns the view in this case but I don't think it's a good choice.
You should review you business logic and try to manage it somewhere it makes more sense
Anyway your code should look like the below:
ObjectDataSource ods = YourDataSource.select();
DetailsView dv = (DetailsView)ods;
Considering the example you provided, I don't think there is anything you can replace for Something_Here. It is the ODS linked to DV and not the other way. Also one DataSource can be linked to several DataBound Controls.
So as far as I know it is simply not possible.

Catch error in GridView automatic databinding

I have a gridview with a DataSourceID set, so the databinding happens automatically. The problem is that sometimes, the procedure defined in the SqlDataSource takes a very long time to finish, so the binding comes with a timeout expired error.
How can I catch this error without manually databinding the gridview and surrounding it with try/catch statements?
If an exception occurs when the SqlDataSource is executing, it fires its appropriate post-action event - Selected, in this case. You can optionally create an event handler for this event and in the event handler say that you've handled the exception.
This diagram shows how this interaction works with the ObjectDataSource (the concept is the same with the SqlDataSource control). When examining the diagram below, replace the words "ObjectDataSource" with "SqlDataSource" and "Underlying Object" with "Database" to have it be pertinent for the SqlDataSource.
As you can see, the Selecting event is raised before the data is sent off to the database and the Selected event is raised after the data comes back (or if there's an exception).
You can create a Selected event handler in your page and check to see if an exception occurred and decide whether you want to handle it yourself. Fredrik Normen has a good blog entry on this: Handle the data-source control exception by your own.
Additional reading material: Accessing and Updating Data in ASP.NET: Examining the Data Source Control's Events.
Happy Programming!
Why not fix the problem with the query timing out instead? Either optimise the DB (preferred) or set the connection/command timeout to be higher than the current value.
You can adjust the timeout as follows by hooking into the SqlDataSource Selecting event:
protected void ds_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
e.Command.CommandTimeout = 5000;
}
If you are using SQL Server you might want to look at tools like the index tuning wizard/tuning advisor, show query execution plan or SQL Server Profiler.
how about binding asynchronously? once completed, the callback function can call databind if no errors were returned.
EDIT: I guess that's manual...not what you wanted.
The only think you can do is handling the Page_Error Event
http://msdn.microsoft.com/en-us/library/ed577840.aspx

FormView_ItemUpdating in not updating

I am using a FormView to update an existing SQL Server record. The rows from the sqldatasource display fine in the FormView and I can edit them. When I click Update, I get the ItemUpdating event but not the ItemUpdated event and the revisions are not written to the database.
Can anyone help me in this please.
In your ItemUpdating event handler, make sure of the following things:
-If you are not using optimistic concurrency checking, remove any old values the FormView may be placing in the OldValues collection.
-Make sure that all of the parameters required by your stored procedure, query, or data source have values and are named correctly in either the Keys or NewValues collections (and make sure that no duplicates exist).
In some cases (usually when an ObjectDataSource is involved), I've had to override the values set by the FormView control, by doing something like this:
protected void myFormView_ItemUpdating(object sender, FormViewUpdateEventArgs e)
{
// remove the old values
e.Keys.Clear();
e.OldValues.Clear();
e.NewValues.Clear();
// set the parameter for the key
e.Keys.Add("#key", valueGoesHere);
// set other parameters
e.NewValues.Add("#param1", aValue);
e.NewValues.Add("#param2", anotherValue);
}
It's not pretty, but it give you absolute control over what gets passed to the DataSource. Generally you should not have to do this if the controls in your FormView are all bound using Bind() for two-way databinding (instead of Eval), but at the very least you could put a break point in ItemUpdating and open up the e.Keys, e.OldValues, and e.NewValues collections to see if the contents are what you expected.
A next step would be to launch SQL Server Profiler to run a trace and examine the actual query being performed.
If you take out the ItemUpdating event and the ItemUpdated event, does your SQL statement execute without errors?
If so, why don't you post some of the code you are using?
Can we see what the sqldatasource looks like? Did you remember to put all the parameters in the sqldatasource under the insert and update parameters list?
Oh also are you setting the cancel property at all in the itemupdating event?

Resources