Software Development : A dizzy job...keeping abreast and being competitive is a 24X7 involvement
Mar 22, 2007
Mar 15, 2007
Enabling the CLR in the Sql Server 2005
If you want to run a CLR object in Sql Server 2005 (or SqlExpress), (note that CLR is disabled by default on Sql Server 2005).
You will need to enable the CLR.
Here are the commands that I used to enable the CLR.
-----------------------------------------------------------
EXEC sp_configure 'show advanced options' , '1';
go
reconfigure;
go
EXEC sp_configure 'clr enabled' , '1'
go
reconfigure;
-----------------------------------------------------------
You will need to enable the CLR.
Here are the commands that I used to enable the CLR.
-----------------------------------------------------------
EXEC sp_configure 'show advanced options' , '1';
go
reconfigure;
go
EXEC sp_configure 'clr enabled' , '1'
go
reconfigure;
-----------------------------------------------------------
Mar 14, 2007
Connecting to Sql Server 2005 from c# winforms An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005
c# winforms An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: TCP Provider, error: 0 - No such host is known.)
Q: I get the following error when trying to connect:"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
I enabled Named Pipes but left TCP/IP disabled because this is a localhost connection on the same computer.
Here is the portion of the code I am using to connect:
string dbCString = "Persist Security Info=False;Integrated Security=true;Trusted_Connection=true;
Initial Catalog=MxData;server=(local)";
SqlConnection ThisConnection = new SqlConnection(dbCString);ThisConnection.Open();
After a lot of research, here's the answer.
The connection string should read as follows:
"Server=.\\SQLEXPRESS;Initial Catalog=MxData;Integrated Security=SSPI";
Note that you need a dot followed by TWO left slashes in your server name.
BTW, Initial Catalog is whatever database you want to use.
Q: I get the following error when trying to connect:"An error has occurred while establishing a connection to the server. When connecting to SQL Server 2005, this failure may be caused by the fact that under the default settings SQL Server does not allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"
I enabled Named Pipes but left TCP/IP disabled because this is a localhost connection on the same computer.
Here is the portion of the code I am using to connect:
string dbCString = "Persist Security Info=False;Integrated Security=true;Trusted_Connection=true;
Initial Catalog=MxData;server=(local)";
SqlConnection ThisConnection = new SqlConnection(dbCString);ThisConnection.Open();
After a lot of research, here's the answer.
The connection string should read as follows:
"Server=.\\SQLEXPRESS;Initial Catalog=MxData;Integrated Security=SSPI";
Note that you need a dot followed by TWO left slashes in your server name.
BTW, Initial Catalog is whatever database you want to use.
Mar 4, 2007
Send Mail Using Lotus Notes
'This public sub will send a mail and attachment if neccessary to the recipient including the body text.
'Requires that notes client is installed on the system.
Public Sub SendNotesMail(Subject As String, attachment As String, recipient As String, bodytext As String, saveit As Boolean)
'Set up the objects required for Automation into lotus notes
Dim Maildb As Object 'The mail database
Dim UserName As String 'The current users notes name
Dim MailDbName As String 'THe current users notes mail database name
Dim MailDoc As Object 'The mail document itself
Dim AttachME As Object 'The attachment richtextfile object
Dim Session As Object 'The notes session
Dim EmbedObj As Object 'The embedded object (Attachment)
'Start a session to notes
Set Session = CreateObject("Notes.NotesSession")
'Get the sessions username and then calculate the mail file name
'You may or may not need this as for MailDBname with some systems you
'can pass an empty string
UserName = Session.UserName
MailDbName = Left$(UserName, 1) & Right$(UserName, (Len(UserName) - InStr(1, UserName, " "))) & ".nsf"
'Open the mail database in notes
Set Maildb = Session.GETDATABASE("", MailDbName)
If Maildb.IsOpen = True Then
'Already open for mail
Else
Maildb.OPENMAIL
End If
'Set up the new mail document
Set MailDoc = Maildb.CREATEDOCUMENT
MailDoc.Form = "Memo"
MailDoc.sendto = recipient
MailDoc.Subject = Subject
MailDoc.Body = bodytext
MailDoc.SAVEMESSAGEONSEND = saveit
'Set up the embedded object and attachment and attach it
If attachment <> "" Then
Set AttachME = MailDoc.CREATERICHTEXTITEM("Attachment")
Set EmbedObj = AttachME.EMBEDOBJECT(1454, "", attachment, "Attachment")
MailDoc.CREATERICHTEXTITEM ("Attachment")
End If
'Send the document
MailDoc.PostedDate = Now() 'Gets the mail to appear in the sent items folder
MailDoc.SEND 0, recipient
'Clean Up
Set Maildb = Nothing
Set MailDoc = Nothing
Set AttachME = Nothing
Set Session = Nothing
Set EmbedObj = Nothing
End Sub
Adding controls to ASP.NET page at runtime
We add control dynamically, attach event handler to it's event and nothing happened. What is wrong?
It is common question asked again and again. Adding controls to ASP.NET WebForm at runtime is a simple task:
private void Page_Load(object sender, System.EventArgs e)
{
Button newButton = new Button();
newButton.Text = "New Button";
newButton.ID = "newButton";
newButton.Click += new System.EventHandler(this.newButton_Click);
WebUserControl1 newUserControl = (WebUserControl1)LoadControl("WebUserControl1.ascx");
newUserControl.ID = "newUserControl";
this.PlaceHolder.Add(newUserControl);
this.PlaceHolder.Add(newButton);
}
Problem
Now let's raise a bet little. What if we want to add new control as a result of some user action (button click for example)?
We are moving the same code from Page_Load to Button_Click event and… Here troubles begin. Our controls will show as expected, but only first time.
Any postback will reset page to its original state. Dynamically created controls will be unable to fire any event.
What is happening here? The answer is simple.
Page class is stateless. It is destroyed after rendering. Page recreates child controls collection,
based on tags in aspx file, then postback data can be handled and event handlers attached (in OnInit event).
Controls we just created dynamically do not exist in aspx file; page has no knowledge about them.
Solution
Solution is also simple. We have to recreate controls on load stage of page lifecycle. After it's done, each control can handle his postback data,
retrieve saved viewstate, raise events etc.
Now code skeleton will look like this:
private void Page_Load(object sender, System.EventArgs e)
{
RecreatePersistedControls();
}
private void Button_Click(object sender, System.EventArgs e)
{
CreateControl("newControl");
PersistControl("newControl");
}
private void RecreatePersistedControls()
{
// Call CreateControl for each persisted control
}
private void CreateControl(string id)
{
// Create controll with specified id,
// add it to controls collection, attach event handlers
}
private void PersistControl(string id)
{
// Persist specified id (in session for example)
}
See fully functional example at Code Project
Subscribe to:
Posts (Atom)