473,883 Members | 2,100 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing DataContext to Button Event Handler

How do I pass a DataContext to a button_click event handler. I have the
following:

public partial class frmMain : Form
{
public frmMain()
{
InitializeCompo nent();

string cnStr =
@"Data Source=.\SQLEXP RESS;AttachDbFi lename=L:\Works pace\SQL
Databases\Train ing.mdf;"
+ "Integrated Security=True;C onnect Timeout=30;User
Instance=True";

DBDataContext db = new DBDataContext(c nStr); // typed DataContext
}

private void btnList_Click(o bject sender, EventArgs e)
{
var act_types =
from a in db.Activities // error: name 'db' does not exist
in
// the current
context
from t in db.Types // ditto
where a.Activity_Type == t.Type_Id

orderby a.Activity_Date
select new
{
a.Activity_Date ,
t.Type_Desc,
a.Activity_Dist ance,
a.Activity_Dura tion,
a.Activity_Rout e
};

dgvList.DataSou rce = act_types; // show query results in
DataGridView

}

Thanks
Oct 15 '08 #1
2 2545
hi Paolo,

Paolo wrote:
How do I pass a DataContext to a button_click event handler.
Use a global variable:

public partial class frmMain : Form
{
DBDataContext _DataContext = null;

public frmMain()
{
InitializeCompo nent();
string cnStr = @"Data Source=.\...";>
_DataContext = new DBDataContext(c nStr); // typed DataContext
}

private void btnList_Click(o bject sender, EventArgs e)
{
var act_types = from a in _DataContext.Ac tivities ...
}
}
mfG
--stefan <--
Oct 15 '08 #2
Stefan: yes, visibility. This hit me while lying in bed this morning. Thx.

"Stefan Hoffmann" wrote:
hi Paolo,

Paolo wrote:
How do I pass a DataContext to a button_click event handler.
Use a global variable:

public partial class frmMain : Form
{
DBDataContext _DataContext = null;

public frmMain()
{
InitializeCompo nent();
string cnStr = @"Data Source=.\...";>
_DataContext = new DBDataContext(c nStr); // typed DataContext
}

private void btnList_Click(o bject sender, EventArgs e)
{
var act_types = from a in _DataContext.Ac tivities ...
}
}
mfG
--stefan <--
Oct 15 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
49593
by: Pavils Jurjans | last post by:
Hallo, I have been programming for restricted environments where Internet Explorer is a standard, so I haven't stumbled upon this problem until now, when I need to write a DOM-compatible code. The question is about best practices for passing parameters to an event function. I have, say, the following HTML:
5
9102
by: memememe | last post by:
is there a way to pass values using the eventargs of a certain event, lets say onclick?? or how does everyone else pass values? lets say each button has 5 things (string, ints, etc) it needs to know, how can you pass that to the event handler?
5
1809
by: DC Gringo | last post by:
I've got a command button to submit a value from a dropdown list that should then filter a SELECT query. I'm simply appending a WHERE colx = <variableSelectedFromDropdownList>. How do I pass this value into the event handler? -- MY EVENT HANDLER Sub RunReport_OnClick(sender As Object, e As System.EventArgs) _sqlStmt = _sqlStmt & " AND colx = '<variableSelectedFromDropdownList>'"
7
9574
by: Richard Grant | last post by:
Hi. In c/C++ i can pass the address of a subroutine to another subroutine as an actual parameter How do I do that in VB .NET What should be the syntax for a parameter to receive the address of a subroutine Let's say theres a sub that creates buttons and I want it to receive as a parameter the address of the sub that handles the OnClick event for the button being created How do I pass such a parameter Thanks in advance Richar
6
8786
by: Joseph Geretz | last post by:
I'm porting a C# Outlook Addin originally engineered as a COM Addin over to use VSTO. I've gotten this to the point where my VSTO Addin installs its Menu items and Toolbar buttons when Outlook launches. I've wired up my event handler to each Menu item and toolbar button. (I use the same Event handler and I use the Tag property which is different for every Menu Item and Toolbar buton to determine which menu or button is being clicked and to...
7
10398
by: AMP | last post by:
Hello, I have this in form1: namespace Pass { public partial class Form1 : Form { public Form2 form2; public Form1() {
1
4723
by: =?Utf-8?B?VG9kZCBCZWF1bGlldQ==?= | last post by:
I have a "Save Changes" button on the form. I'm binding its IsEnabled property to a property of the object that I'm dealing with. When I connect the panel the buttons are in with an object (set the DataContext when the user selects an object from a list), the buttons correctly evaluate. When I tweak the property in the object, the notification is correctly evaluated and the button's state changes as expected. When I first load the form,...
0
1644
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hi All, Perhaps its because its been a long week, but I just can't seem to figure out how to bind data in a DataTemplate using DataContext. The following is in the XAML code: <Button x:Name="btnRegularDataContext" Margin="5"> <TextBlock Text="{Binding}" /> </Button>
2
9800
by: =?Utf-8?B?V29ua28gdGhlIFNhbmU=?= | last post by:
Hello, I have a ComboBox that has a DataContext set on it. When a PropertyChanged event is fired (based on a static instance of some class), I'd like to update the DataContext on the combo, but it seems that I need to set it to null first: Combo1.DataContext = null; Combo1.DataContext = dc;
0
9798
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11160
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10766
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9588
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
7136
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5807
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4622
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4230
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3241
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.