I'm using Visual Studio 2008 with C#.
I have a .xsd file and it has a table adapter. I want to change the table adapter's command timeout.
Thanks for your help.
With some small modifications csl's idea works great.
partial class FooTableAdapter
{
/**
* <summary>
* Set timeout in seconds for Select statements.
* </summary>
*/
public int SelectCommandTimeout
{
set
{
for (int i = 0; i < this.CommandCollection.Length; i++)
if (this.CommandCollection[i] != null)
this.CommandCollection[i].CommandTimeout = value;
}
}
}
To use it, just set
this.FooTableAdapter.CommandTimeout = 60; somewhere before the this.FooTableAdapter.Fill();
If you need to change the timeout on a lot of table adapters, you could create a generic extension method and have it use reflection to change the timeout.
/// <summary>
/// Set the Select command timeout for a Table Adapter
/// </summary>
public static void TableAdapterCommandTimeout<T>(this T TableAdapter, int CommandTimeout) where T : global::System.ComponentModel.Component
{
foreach (var c in typeof(T).GetProperty("CommandCollection", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.GetProperty | System.Reflection.BindingFlags.Instance).GetValue(TableAdapter, null) as System.Data.SqlClient.SqlCommand[])
c.CommandTimeout = CommandTimeout;
}
Usage:
this.FooTableAdapter.TableAdapterCommandTimeout(60);
this.FooTableAdapter.Fill(...);
This is a little slower. And there is the possibility of an error if you use it on the wrong type of object. (As far as I know, there is no "TableAdapter" class that you could limit it to.)
I have investigated this issue a bit today and come up with the following solution based on a few sources.
The idea is to create a base class for the table adapter too inherit which increases the timeout for all commands in the table adapter without having to rewrite too much existing code. It has to use reflection since the generated table adapters don't inherit anything useful. It exposes a public function to alter the timeout if you want to delete what i used in the constructor and use that.
using System;
using System.Data.SqlClient;
using System.Reflection;
namespace CSP
{
public class TableAdapterBase : System.ComponentModel.Component
{
public TableAdapterBase()
{
SetCommandTimeout(GetConnection().ConnectionTimeout);
}
public void SetCommandTimeout(int Timeout)
{
foreach (var c in SelectCommand())
c.CommandTimeout = Timeout;
}
private System.Data.SqlClient.SqlConnection GetConnection()
{
return GetProperty("Connection") as System.Data.SqlClient.SqlConnection;
}
private SqlCommand[] SelectCommand()
{
return GetProperty("CommandCollection") as SqlCommand[];
}
private Object GetProperty(String s)
{
return this.GetType().GetProperty(s, BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance).GetValue(this, null);
}
}
}
I had a couple of issues with using Mitchell Gilman's solution that I was eventually able to workaround.
First of all, I needed to make sure to use the right namespace. It took me a while to figure out that Designer file for the xsd data set actually contains two namespaces, one for the data set in general and one for the table adapters. So the first thing is to note is that the namespace for the table adapter should be used, not for the data set in general.
Secondly, the commandcollection may not always be initialized when the timeout command is used for the first time. To work around this, I called the InitCommandCollection command if this was the case.
So the adapted solution I used was
namespace xxx.xxxTableAdapters
partial class FooTableAdapter
{
/**
* <summary>
* Set timeout in seconds for Select statements.
* </summary>
*/
public int SelectCommandTimeout
{
set
{
if (this.CommandCollection == null)
this.InitCommandCollection();
for (int i = 0; i < this.CommandCollection.Length; i++)
if (this.CommandCollection[i] != null)
this.CommandCollection[i].CommandTimeout = value;
}
}
}
Hope that's helpful to people!
In some cases you cannot access members like Adapter in your class, since they are defined as private to the class.
Fortunately, the wizard will generate partial classes, which means you can extend them. As described in [this thread by Piebald][1], you can write your own property to set the timeout on select-commands.
Generally, you would do this:
partial class FooTableAdapter
{
/**
* <summary>
* Set timeout in seconds for Select statements.
* </summary>
*/
public int SelectCommandTimeout
{
set
{
for ( int n=0; n < _commandCollection.Length; ++n )
if ( _commandCollection[n] != null )
((System.Data.SqlClient.SqlCommand)_commandCollection[n])
.commandTimeout = value;
}
}
}
Note that I have not actually tried this myself, but it seems like a viable solution.
Say your dataset is called MySET.
There is one table called MyTable
MySETTableAdapters.MyTableTableAdapter fAdapter =
new MySETTableAdapters.MyTableTableAdapter();
fAdapter.Adapter.SelectCommand.CommandTimeout = <fill inyour value here>;
Call ChangeTimeout Function by providing the TableAdapter and Time in seconds.
this.ChangeTimeout(this.taTest, 500);
Function :
private void ChangeTimeout(Component component, int timeout)
{
if (!component.GetType().FullName.Contains("TableAdapter")) {
return;
}
PropertyInfo adapterProp = component.GetType().GetProperty("CommandCollection", BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance);
if (adapterProp == null) {
return;
}
SqlCommand[] command = adapterProp.GetValue(component, null) as SqlCommand[];
if (command == null) {
return;
}
Interaction.command(0).CommandTimeout = timeout;
}
Here's some example code from MSDN, using VB.NET:
Imports System.Data.SqlClient
Namespace MyDataSetTableAdapters
Partial Class CustomersTableAdapter
Public Sub SetCommandTimeOut(ByVal timeOut As Integer)
For Each command As SqlCommand In Me.CommandCollection
command.CommandTimeout = timeOut
Next
End Sub
End Class
End Namespace
When it comes time to call a long query, just call the SetCommandTimeOut method before the query:
Dim ds As New MyDataSet
Dim customersTA As New MyDataSetTableAdapters.CustomersTableAdapter
' Increase time-out to 60 seconds
customersTA.SetCommandTimeOut(60000)
' Do the slow query
customersTA.FillSlowQuery(ds.Customers)
There seems to be a more convenient way to do this. Here's a quick recap of what I found.
Let's say I add a (class library) project called MyDB to my solution. Into that project I add a DataSet called "Data". And into that dataset, I drag a table called "X".
What I get on the design surface is an object that shows that I have an object called "XTableAdapter".
I now open the generated code, Data.Designer.cs, and look for XTableAdapter.
When I find it, I note that it's contained in namespace MyDB.DataTableAdapters - which is just a concatenation of the name of the project, "MyDB", the name of the DataSet, "Data", and "TableAdapters".
With that in hand, I now go back to the class library, still called Class1.cs (which I'll ignore for now).
I change its namespace from MyDB to MyDB.DataTableAdapters.
I change the class declaration to public partial class XTableAdapter,
and make it look like this:
using System.Data.SqlClient;
namespace MyDB.DataTableAdapters
{
public partial class XTableAdapter
{
public void SetTimeout(int seconds)
{
foreach (SqlCommand cmd in CommandCollection)
{
cmd.CommandTimeout = seconds;
}
}
}
}
The calling sequence could hardly be clearer:
int TwoMinutes = 120;
XTableAdapter.SetTimeout(TwoMinutes);
Less muss, less fuss, less reflection (well, none), less filling.
If you use a partial class, make you have the right namespace. Probably [your data set's name] + "TableAdapters'. Example:
namespace MyProject.DataSet1TableAdapters
You can open up the Properties folder, open Settings.settings and alter the Timeout property of your connection string.
This one is a bit old now and suspect this solution is not relevant to everyone, but I've ended up using AniPol's solution to override the ObjectDataSource control as follows:
public class MyObjectDataSource : ObjectDataSource
{
public MyObjectDataSource()
{
this.ObjectCreated += this.MyObjectDataSource_ObjectCreated;
}
private void MyObjectDataSource_ObjectCreated(object sender, ObjectDataSourceEventArgs e)
{
var objectDataSourceView = sender as ObjectDataSourceView;
if (objectDataSourceView != null && objectDataSourceView.TypeName.EndsWith("TableAdapter"))
{
var adapter = e.ObjectInstance;
PropertyInfo adapterProp = adapter.GetType()
.GetProperty(
"CommandCollection",
BindingFlags.NonPublic | BindingFlags.GetProperty | BindingFlags.Instance);
if (adapterProp == null)
{
return;
}
SqlCommand[] commandCollection = adapterProp.GetValue(adapter, null) as SqlCommand[];
if (commandCollection == null)
{
return;
}
foreach (System.Data.SqlClient.SqlCommand cmd in commandCollection)
{
cmd.CommandTimeout = 120;
}
}
}
}
I do like this ; Right click Fill() or GetX() function and click Goto Defination from menu.
You will see Source code of DATATABLE. And find ;
private global::System.Data.SqlClient.SqlCommand[] _commandCollection;
command line from your dataadapter class.
And Change the private to public .
Now you can access the _commandCollection and you can change all attributes.
But be careful when you add or change any Filed form DESIGNER , the public will be private again by autogenerate system.
And also , when you finish to call Fill or Get Function you must reset _commandColleciton calling this function ( InitCommandCollection() )
public void InitCommandCollection() {}
This function is also private by autogen, you must change to public also!
Example:
dsIslemlerTableAdapters.tblIslemlerTableAdapter _t = new dsIslemlerTableAdapters.tblIslemlerTableAdapter();
dsIslemler.tblIslemlerDataTable _m = new dsIslemler.tblIslemlerDataTable();
_t._commandCollection[0].CommandText = "Select * From tblIslemler Where IslemTarihi>='' And IslemTarihi<=''";
_m = _t.GetData();
_t.InitCommandCollection();
Expanding on the already very useful answers for tableadapters that helped me a lot, I also had the need to read out the actual timeout value. Thus:
namespace XTrans.XferTableAdapters
{
public partial class FooTableAdapter
{
int? _timeout = null;
///<summary>
///Get or set the current timeout in seconds for Select statements.
///</summary>
public int CurrentCommandTimeout
{
get
{
int timeout = 0;
if (_timeout != null)
{
timeout = (int)_timeout;
}
else
{
for (int i = 0; i < this.CommandCollection.Length; i++)
if (this.CommandCollection[i] != null)
timeout = this.CommandCollection[i].CommandTimeout;
}
return timeout;
}
set
{
if (this.CommandCollection == null)
this.InitCommandCollection();
for (int i = 0; i < this.CommandCollection.Length; i++)
if (this.CommandCollection[i] != null)
{
this.CommandCollection[i].CommandTimeout = value;
_timeout = value;
}
}
}
}
}
If you go to [name of DataSet].Designer.cs which is a file added under the data set file in the solution and then search for :
private void InitCommandCollection();
This is a function that you should be able to set properties for functions that have been defined in a table adapter.
The first line in that function is
this._commandCollection = new global::System.Data.IDbCommand[<number of function defined in a table adapater>];
and then in the next line for each of those function, you can set
((global::System.Data.SqlClient.SqlCommand)(this._commandCollection[<index>])).CommandTimeout = 0;
which 0 indicates no limitation and the function will not stop due to the time out and also it can be set to 10, 20, 30 or 1000 and so on
After scratching my head all day long I finally got the resolution for this. After designing your .xsd file all you need to do is go to your dataset.designer.cs page which is an auto generated page and change the code to provided below. It really works. Give it a try.
protected global::System.Data.SqlClient.SqlCommand[] CommandCollection
{
get
{
if ((this._commandCollection == null))
{
this.InitCommandCollection();
_commandCollection[0].CommandTimeout = 0;
}
_commandCollection[0].CommandTimeout = 0;
return this._commandCollection;
}
}
Related
I have two projects, the first one with a ClassLibraryServer namespace and Output method as following :
namespace ClassLibraryServer
{
public class Output
{
public static void Card(tblCard card, int myProperty)
{
if (kort.fltNr.Length != 6)
return;
using (myDataContext db = new myDataContext(ClassLibrary.Settings.Instance.SqlS))
{
tblReader[] readers = (from row in db.tblReader
where !row.fltOffline
select row).ToArray();
bool blocked = (db.tblCardBlocked.Where(b => b.fltProperty == myProperty && b.fltCard == card.id).FirstOrDefault() != null);
foreach (tblReader reader in readers)
{
Card(card, reader, blocked);
}
}
}
}
}
The Card method in foreach loop calls another method (Overloading the first card method) which has three parameters and inserts data to a sql table, I won't share it here as it takes too much space.
Now I want to call above method in another project with namespace ExtControlModule:
namespace ExtControlModule
{
public class TimeModule
{
using (ModuleDataContext dbsys = new ModuleDataContext(ClassLibrary.Settings.Instance.SqlS)) {
var loungee = (from tblLounge in dbsys.tblLounges
select tblLounge).ToList();
var lasaresFas = 0;
foreach (var LoungeRow in loungee)
{
lasaresFas= Convert.ToInt32(dbsys.tblLasares.FirstOrDefault(f => f.id == LoungeRow.fltCardreader).fltproperty);
tblCard cardLoading = dbsys.tblCard.FirstOrDefault(t => t.id == res.fltCardnumber);
Output.Card(cardLoading, lasaresFas);
}
}
}
Now when I want to assign parameters to Output.Card in ExtControlModule I get the following error in cardLoading parameter in Output.Card:
Argument !: cannot convert from 'ExtControlModule.tblCard' to ClassLibrary.db.tblCard'
ClassLibrary is another project in the same Solution which contains a public class Settings with the SqlS property which facilitate connection to SQL database.
Struggling with this for many days as I am new to both ASP.NET and Sql, hope my question is clear enough.
call method
ClassLibraryServer.Output.Card(cardLoading, lasaresFas);
I'm starting to dive into the world of C# Dynamics and Metaprogramming, and having some trouble.
I managed to create a CodeDom tree, and generate the following code:
namespace Mimsy {
using System;
using System.Text;
using System.Collections;
internal class JubJub {
private int _wabeCount;
private ArrayList _updates;
public JubJub(int wabeCount) {
this._updates = new ArrayList();
this.WabeCount = wabeCount;
}
public int WabeCount {
get {
return this._wabeCount;
}
set {
if((value < 0))
this._wabeCount = 0;
else
this._wabeCount = value;
this._updates.Add(this._wabeCount);
}
}
public string GetWabeCountHistory() {
StringBuilder result = new StringBuilder();
int ndx;
for(ndx = 0; (ndx < this._updates.Count); ndx = ndx + 1) {
if((ndx == 0))
result.AppendFormat("{0}", this._updates[ndx]);
else
result.AppendFormat(", {0}", this._updates[ndx]);
}
}
}
}
I am then compiling dynamically this namespace to an assembly named "dummy".
I can succesfully get an instance of this Type:
string typeName = "Mimsy.JubJub";
Type type = dummyAssembly.GetType(typeName);
dynamic obj = Activator.CreateInstance(type, new object[] { 8 });
//obj is a valid instance type
If I debug this code, I can see in the debugger that obj actually has the property WabeCount:
However, when trying to access this property, the compiler shouts that the dynamic property does not exist.
There are one or perhaps two problems with your code:
You are using an internal class, and trying to access it with dynamic. The two things don't play well together. See https://stackoverflow.com/a/18806787/613130. Use public clasas
You need to cast the value before assigning it to wabeCount, like:
obj.WabeCount = (int)wabes[ndx]
Note that technically, if your "main" assembly is strong named, you could add the InternalsVisibleToAttribute to the "dynamic" assembly to make its internal "things" visible to the main assembly... I do think it would be wasted work.
So my question is over basic encapsulation. I know I am setting up my getters and setters right (I actually have a question later about this) but I have multiple classes. I have another class and I understand that I am making pieces of my code view-able to my outside class by making certain things public. So I think I set up my first code file right. (Some background, I have a class that is connecting to a database and then another that is encapsulating all the data. The first code section posted is the encapsulating part, I then post my three methods I was messing up on.)
I feel okay with the getting and setting, I feel a little unsure of my constructor. I feel like I put my variables in the parameter list so that I put values in them from my outside class? Right? or should I be putting public forms of my private variables in my other code file and then passing those into my constructor in that same file?
/ this my first code file
using System;
public class clsEncapsulate
{
private int mID;
private string mName;
private string mClassification;
private DateTime mConvocationDate;
private string mLocation;
public int ID
{
get
{
return mID;
}
set
{
mID = value;
}
}
public string Name
{
get
{
return mName;
}
set
{
mName = value;
}
}
public string Classification
{
get
{
return mName;
}
set
{
mName = value;
}
}
private DateTime ConvocationDate
{
get
{
return mConvocationDate;
}
set
{
mConvocationDate = value;
}
}
private string Location
{
get
{
return mLocation;
}
set
{
mLocation = value;
}
}
public clsEncapsulate(int id, string name, string classification, DateTime convocationDate, string location)
{
bool running = false;
while(running == false)
{
ID = mID;
Name = mName;
Classification = mClassification;
ConvocationDate = mConvocationDate;
Location = mLocation;
running = true;
}
}
}
In my second code file I am just going to put the methods that I am having trouble with.
private void refreshbox()
{
string formattedConvocation;
string formattedDateTime;
string formattedConvocationName;
lstConvocations.Items.Clear();
foreach (clsEncapsulate currentConvocation in mConvocationAL)
{
formattedConvocationName = currentConvocation.Name;
formattedConvocationName = truncateString(formattedConvocationName, 30);
formattedConvocation = formattedConvocationName.PadRight(33);
formattedConvocation += currentConvocation.Classification.PadRight(17);
formattedDateTime = currentConvocation.ConvocationDate.ToShortDateString().PadRight(10)
+ currentConvocation.ConvocationDate.ToShortTimeString().PadLeft(8);
formattedConvocation += formattedDateTime;
lstConvocations.Items.Add(formattedConvocation);
}
}
Alright, so in order for my second code file to manipulate the variables in the first code file, I need to expose them to this method. I didn't know if I should be putting my public variables in the constructor, or if I should be declaring them somewhere in my first code file. I was very unsure of how to expose these variables to this method. I've fiddle around with it but my book doesn't address this situation exactly and I was having trouble figuring it out.
If someone does answer this question please break down why you're going to put what you're going to put! I want to understand why, say, I put my public variables in one place, and not another. Or why I declare an object of my encapsulate class in one place and not another. I was trying to declare an encapsulate object in my method so it would give this method access to the variables, but it wasn't working! Please tell me what I was doing wrong or if you want me to post more of my code.
Below are the two other methods I was messing up on.
/ second method from my second code file I was messing up on:
private void displayProperties(int index)
{
if (index == -1)
{
return;
}
clsEncapsulate selectedValue = (clsEncapsulate)mConvocationAL[index];
txtConvocationName.Text = selectedValue.Name;
txtConvocationClassification.Text = selectedValue.Classification;
txtConvocationDate.Text = selectedValue.ConvocationDate.ToShortDateString();
txtConvocationTime.Text = selectedValue.ConvocationDate.ToShortTimeString();
txtConvocationLocation.Text = selectedValue.Location;
txtID.Text = selectedValue.ID.ToString();
}
/ last method I was messing up on:
private void readConvocations(string filterConstraint, string sortField, string sortOrder)
{
OleDbConnection connection = null;
OleDbDataReader reader = null;
try
{
connection = new OleDbConnection();
connection.ConnectionString = mConnectionString;
connection.Open();
string statement = "SELECT ID, Name, Classification, Location, Date FROM Convocations ";
if(filterConstraint != "")
{
statement += "WHERE Name LIKE " + toSQL(filterConstraint, true) + " ";
}
string statement2 = statement;
statement = string.Concat(new string[]
{
statement2, "ORDER BY ", sortField, " ", sortOrder
});
OleDbCommand oleDbCommand = new OleDbCommand(statement, connection);
reader = oleDbCommand.ExecuteReader();
mConvocationAL.Clear();
while(reader.Read())
{
clsEncapsulteconvocation = new clsEncapsulate();
convocation.ID = (int)reader["ID"];
convocation.Name = (string)reader["Name"];
convocation.Classification = (string)reader["Classification"];
convocation.Location = (string)reader["Location"];
convocation.ConvocationDate = (DateTime)reader["Date"];
mConvocationAL.Add(convocation);
}
}
finally
{
if (reader != null)
{
reader.Close();
}
if (connection != null)
{
connection.Close();
}
}
}
Tell me if you need me to elaborate more to help you understand my situation. I am new at learning vocabulary and want to understand this! Thank you for helping. :)
The code you provided is one public object and a bunch of private methods so its difficult to get the overall picture of how your code it working together but there are a few principles you can apply to make your code better structured, now and in the future.
Have a read about SOLID (http://en.wikipedia.org/wiki/SOLID_(object-oriented_design)). The S and the D would apply well to your example.
Also you mentioned construtors and private properties. Try looking into Imutable types. That means once the object is created you cannot change it. For your clsEncapsulate class that would mean making your fields read only and remove the public setters.
Good luck.
I pulled an example from this MSDN page and have used it pretty much verbatim. When run the code compiles properly but changeCount increments endlessly whether or not there has actually been a change to the data returned. When a change actually has occurred dataGridView1 reflects the change correctly. Why does my SqlDependency seem like it's firing in a loop even though there apparently have been no changes?
Here's the source:
#region Using directives
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Windows.Forms;
#endregion
namespace PreAllocation_Check
{
public partial class Form1 : Form
{
int changeCount = 0;
const string tableName = "MoxyPosition";
const string statusMessage = "Last: {0} - {1} changes.";
DataSet dataToWatch = null;
SqlConnection MoxyConn = null;
SqlCommand SQLComm = null;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
if (CanRequestNotifications())
{
SqlDependency.Start(GetConnectionString());
if (MoxyConn == null)
MoxyConn = new SqlConnection(GetConnectionString());
if (SQLComm == null)
{
SQLComm = new SqlCommand(GetSQL(), MoxyConn);
SqlParameter prm = new SqlParameter("#Quantity", SqlDbType.Int);
prm.Direction = ParameterDirection.Input;
prm.DbType = DbType.Int32;
prm.Value = 100;
SQLComm.Parameters.Add(prm);
}
if (dataToWatch == null)
dataToWatch = new DataSet();
GetData();
}
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
SqlDependency.Stop(GetConnectionString());
if (MoxyConn != null)
MoxyConn.Close();
}
private bool CanRequestNotifications()
{
try
{
SqlClientPermission SQLPerm = new SqlClientPermission(PermissionState.Unrestricted);
SQLPerm.Demand();
return true;
}
catch
{
return false;
}
}
private string GetConnectionString()
{
return "server=***;database=***;user id=***;password=***";
}
private void GetData()
{
dataToWatch.Clear();
SQLComm.Notification = null;
SqlDependency SQLDep = new SqlDependency(SQLComm);
SQLDep.OnChange += new OnChangeEventHandler(SQLDep_OnChange);
using (SqlDataAdapter adapter = new SqlDataAdapter(SQLComm))
{
adapter.Fill(dataToWatch, tableName);
dataGridView1.DataSource = dataToWatch;
dataGridView1.DataMember = tableName;
}
}
private string GetSQL()
{
return "SELECT PortID, CONVERT(money, SUM(PreAllocPos), 1) AS PreAllocation, CONVERT(money, SUM(AllocPos), 1) AS Allocation, CONVERT(money, SUM(PreAllocPos) - SUM(AllocPos), 1) AS PreLessAlloc " +
"FROM MoxyPosition " +
"WHERE CONVERT(money, PreAllocPos, 1) <> CONVERT(money, AllocPos, 1) " +
"GROUP BY PortID " +
"ORDER BY PortID ASC;";
}
void SQLDep_OnChange(object sender, SqlNotificationEventArgs e)
{
ISynchronizeInvoke i = (ISynchronizeInvoke)this;
if (i.InvokeRequired)
{
OnChangeEventHandler tempDelegate = new OnChangeEventHandler(SQLDep_OnChange);
object[] args = { sender, e };
i.BeginInvoke(tempDelegate, args);
return;
}
SqlDependency SQLDep = (SqlDependency)sender;
SQLDep.OnChange -= SQLDep_OnChange;
changeCount++;
DateTime LastRefresh = System.DateTime.Now;
label1.Text = String.Format(statusMessage, LastRefresh.TimeOfDay, changeCount);
GetData();
}
}
}
Edit: It's worth noting that the database I want to run this against does not currently have the Broker Service enabled, and so to test my code I backed up my target database and restored it with a new name, then ran ALTER DATABASE my_db_name SET ENABLE_BROKER against it. All of my testing has been on this alternate database, which means I'm the only user on it.
This is an old question, but the problem is that your query doesn't meet the requirements.
Short answer:
add schema name to the table "FROM DBO.MoxyPosition " +
Longer answer:
You can see a list of requirements here, which are very similar to those of creating an indexed view. When a SQL Dependency is registered, if it is invalid the notification immediately fires letting you know it's invalid. When you think about it, this makes sense, because how can Visual studio know what the internal requirements are for the SQL Engine?
So in your SQLDep_OnChange function you'll want to look at the reason the dependency fired. The reason is in the e variable (info, source, and type). Details on the event object can be found here:
Info options
Source
Type
For your specific case notice how MS describes the Type property :
Gets a value that indicates whether this notification is generated
because of an actual change, OR BY THE SUBSCRIPTION.
I had a similar problem. Turns out doing SELECT * FROM dbo.MyTable was causing the update to fire constantly. Changing to SELECT Id, Column1, Column2 FROM dbo.MyTable fixed the problem.
It doesn't look like you're using * in your query, but you might try simplifying your query to see if you still have the issue.
I don't have an answer for this, but you did break at least one of the rules here: http://msdn.microsoft.com/en-us/library/aewzkxxh.aspx
when you failed to use two-part table names. Change MoxyPosition to dbo.MoxyPosition and review the rules linked above. I hope it helps, but something tells me something else is at fault here.
see what the Type of the SqlNotificationEventArgs is in your handler (defined as below). If you see it hit hundreds of times and the Type is Subscribe each time then your SQL statement is wrong - see guidelines in other postings
private void HandleOnChange(object sender, SqlNotificationEventArgs e)
{
...
var someType = e.Type; /*If it is Subscribe, not Change, then you may have your SQL statement wrong*/
...
}
OK, I am mostly a LAMP developer so I am new to the entity framework. However, I am familiar with the basics in LINQ and have generated a entity model from my DB. Now here is my requirement:
I have a datagrid on a WinForm that will be refreshed from a data source on a remote server every few seconds as changes to the dataset are made from other sources. Obviously, I'd like to construct a lambda expression to get the right anonymous type to satisfy the columns that needs to be shown in my datagrid. I have done this and here is the result (I'm using a custom datagrid control, btw):
And my code thus far:
Models.dataEntities objDB = new Models.dataEntities();
var vans = from v in objDB.vans
select v;
gcVans.DataSource = vans;
OK, so now I have my basic data set. One problem I had is that the "Status" column will show a calculated string based on several parameters in the data set. I added this to my entities via a partial class. As you can see in the screenshot, this is working correctly:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace WindowsFormsApplication1.Models {
public partial class van {
public string van_status {
get {
if (this.is_offline == 1) {
return "Offline";
} else if (this.is_prayer_room == 1) {
return "In Prayer Room";
} else {
return "TODO: Create statuses";
}
}
}
}
}
This added property works fine. However, the second I try to project the status in an anonymous type so I can also retrieve the school name, I get an error:
Models.dataEntities objDB = new Models.dataEntities();
var vans = from v in objDB.vans
select new {
van_name = v.van_name,
school_name = v.school.school_name,
capacity = v.capacity,
phone = v.phone,
van_status = v.van_status
};
gcVans.DataSource = vans;
So, I have two questions:
1) If I cannot use computed properties of my partial classes in LINQ projections, how am I supposed to show my computed string in my datagrid?
2) Am I even approaching this correctly? When I resolve #1, how would I refresh this data (ie during a timer fire event)? Would I simply call objDB.refresh() and then tell my datagrid to update from the datasource? Does calling that method actually run the lambda expression above or does it load everything from the DB?
Thanks for any assistance with this. Also, if you have any best practices to share that would be awesome! I hope I explained this as thoroughly as you need to provide assistance.
1) Instead of modifying your EF object with a partial class you could always create your own class that contains your read only property van_status. The code you've got would be nearly identical:
Models.dataEntities objDB = new Models.dataEntities();
gcVans.DataSource = from v in objDB.vans
select new DisplayVan {
van_name = v.van_name,
school_name = v.school.school_name,
capacity = v.capacity,
phone = v.phone,
};
The van_status property, since it's read-only, will not need to be specified in the query.
2) I'm more a web developer than a desktop developer so I'll give you my take on how to refresh the grid (it may not be the preferred methodology for fat clients)...
I'm reluctant to trust .Refresh() methods and hope all works to maximal efficiency and work properly. Instead, encapsulate the code from #1 in a method of your own and invoke it from your timer event firing (or however you choose to implement the periodic refresh).
Another good option would be to create an extension method.
Here is a simple example:
using System;
namespace WindowsFormsApplication1 {
static class Program {
[STAThread]
static void Main() {
Van van = new Van();
string status = van.GetStatus();
}
}
public static class VanExtension {
public static string GetStatus(this Van van) {
if(van.is_offline == 1) {
return "Offline";
}
else if(van.is_prayer_room == 1) {
return "In Prayer Room";
}
return "TODO: Create statuses";
}
}
public class Van {
public int is_offline { get; set; }
public int is_prayer_room { get; set; }
}
}
Be sure to put this extension class in the same namespace as the entity class.