Windows Develop Bookmark and Share   
 index > Windows Forms Designer > CommandText Property Editor
 

CommandText Property Editor

Dear Sir

I'd like to create a user control that has a CommandText property that allows the user to create his SQL statement at design time.

When someone adds this control to their form, there will be a property called CommandText that when it is clicked (via the Properties grid), it will bring up the Query Builder dialog box.

I found the following code and tried to use it, but it doesn't work
<!--[if !supportLineBreakNewLine]--><!--[endif]-->

Code Snippet:

[DefaultValue(""), Editor("Microsoft.VSDesigner.Data.ADO.Design.OleDbCommandTextEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"), RefreshProperties(RefreshProperties.All)]
public string CommandText
{

get { return m_CommandText; }

set { m_CommandText = value; }

}

I think there are something missing, like how I could path the ConnectionString to the Editor? Can any one give a hint on this issue?

regards,

Ahmed Hashish

Ahmed Hashish  Sunday, September 23, 2007 12:39 PM

Hi

You are right; the query builder does need a connection. But only add a connection string is not enough. As far as I know the build-in query builder is designed for IDbCommand. You can custom a query builder for yourself. Try something like the following.

The code for UserControl:

Code Block

public partial class UCCommandText : UserControl

{

string m_CommandText;

[Editor(typeof(CommandTextUIEditor), typeof(UITypeEditor))]

public string CommandText

{

get { return m_CommandText; }

set { m_CommandText = value; }

}

private string _connectionstring;

[RefreshProperties(RefreshProperties.All), DefaultValue(""), Editor("Microsoft.VSDesigner.Data.SQL.Design.SqlConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]

public string Connectionstring

{

get { return _connectionstring; }

set { _connectionstring = value; }

}

}

The code for CommandTextUIEditor:

Code Block

class CommandTextUIEditor : UITypeEditor

{

private IWindowsFormsEditorService edSvc = null;

protected virtual void SetEditorProps(UCCommandText editingInstance, UCCommandText editor)

{

if (editingInstance != null)

{

editor.Connectionstring = editingInstance.Connectionstring;

editor.CommandText = editingInstance.CommandText;

}

}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

{

if (context != null && context.Instance != null &&

provider != null)

{

edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

if (edSvc != null)

{

UCCommandText command = new UCCommandText();

SetEditorProps((UCCommandText)context.Instance, command);

CusCommandTextEditor editor = new CusCommandTextEditor(command);

edSvc.ShowDialog(editor);

if (editor.DialogResult == DialogResult.OK)

{

value = command.CommandText;

}

}

}

return value;

}

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)

{

if (context != null && context.Instance != null)

{

return UITypeEditorEditStyle.Modal;

}

return base.GetEditStyle(context);

}

public override bool GetPaintValueSupported(ITypeDescriptorContext context)

{

return false;

}

Hi,

And here is the code for CusCommandTextEditor Form:

Code Block

public partial class CusCommandTextEditor : Form

{

public CusCommandTextEditor()

{

InitializeComponent();

}

DataTable dsResultdt = new DataTable();

private UCCommandText command;

public CusCommandTextEditor(UCCommandText command)

: this()

{

this.command = command;

this.txtCommandText.Text = command.CommandText;

}

private void execute()

{

SqlConnection connection = new SqlConnection(command.Connectionstring);

try

{

//clear the data table

this.dsResultdt.Clear();

this.dsResultdt.Columns.Clear();

// Open connection

connection.Open();

// Update CommandText

this.command.CommandText = this.txtCommandText.Text;

// Execute command & fill result

SqlDataAdapter adapter = new SqlDataAdapter(this.command.CommandText, connection);

adapter.Fill(dsResultdt);

// Set result table

this.dataGridView1.DataSource = this.dsResultdt;

}

catch (Exception ex)

{

this.dsResultdt.Clear();

this.lblError.Text = ex.Message;

}

finally

{

connection.Close();

}

}

private void btnExecute_Click(object sender, EventArgs e)

{

this.execute();

}

private void btnOk_Click(object sender, EventArgs e)

{

this.command.CommandText = this.txtCommandText.Text;

}

private void btnCancel_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.Cancel;

}

private void txtCommandText_TextChanged(object sender, EventArgs e)

{

if (this.txtCommandText.Text != null &&

this.txtCommandText.Text.Trim().Length > 0)

{

this.btnExecute.Enabled = true;

}

else

{

this.btnExecute.Enabled = false;

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, September 27, 2007 1:57 PM

Hi

You are right; the query builder does need a connection. But only add a connection string is not enough. As far as I know the build-in query builder is designed for IDbCommand. You can custom a query builder for yourself. Try something like the following.

The code for UserControl:

Code Block

public partial class UCCommandText : UserControl

{

string m_CommandText;

[Editor(typeof(CommandTextUIEditor), typeof(UITypeEditor))]

public string CommandText

{

get { return m_CommandText; }

set { m_CommandText = value; }

}

private string _connectionstring;

[RefreshProperties(RefreshProperties.All), DefaultValue(""), Editor("Microsoft.VSDesigner.Data.SQL.Design.SqlConnectionStringEditor, Microsoft.VSDesigner, Version=8.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a", "System.Drawing.Design.UITypeEditor, System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a")]

public string Connectionstring

{

get { return _connectionstring; }

set { _connectionstring = value; }

}

}

The code for CommandTextUIEditor:

Code Block

class CommandTextUIEditor : UITypeEditor

{

private IWindowsFormsEditorService edSvc = null;

protected virtual void SetEditorProps(UCCommandText editingInstance, UCCommandText editor)

{

if (editingInstance != null)

{

editor.Connectionstring = editingInstance.Connectionstring;

editor.CommandText = editingInstance.CommandText;

}

}

public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)

{

if (context != null && context.Instance != null &&

provider != null)

{

edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));

if (edSvc != null)

{

UCCommandText command = new UCCommandText();

SetEditorProps((UCCommandText)context.Instance, command);

CusCommandTextEditor editor = new CusCommandTextEditor(command);

edSvc.ShowDialog(editor);

if (editor.DialogResult == DialogResult.OK)

{

value = command.CommandText;

}

}

}

return value;

}

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context)

{

if (context != null && context.Instance != null)

{

return UITypeEditorEditStyle.Modal;

}

return base.GetEditStyle(context);

}

public override bool GetPaintValueSupported(ITypeDescriptorContext context)

{

return false;

}

Hi,

And here is the code for CusCommandTextEditor Form:

Code Block

public partial class CusCommandTextEditor : Form

{

public CusCommandTextEditor()

{

InitializeComponent();

}

DataTable dsResultdt = new DataTable();

private UCCommandText command;

public CusCommandTextEditor(UCCommandText command)

: this()

{

this.command = command;

this.txtCommandText.Text = command.CommandText;

}

private void execute()

{

SqlConnection connection = new SqlConnection(command.Connectionstring);

try

{

//clear the data table

this.dsResultdt.Clear();

this.dsResultdt.Columns.Clear();

// Open connection

connection.Open();

// Update CommandText

this.command.CommandText = this.txtCommandText.Text;

// Execute command & fill result

SqlDataAdapter adapter = new SqlDataAdapter(this.command.CommandText, connection);

adapter.Fill(dsResultdt);

// Set result table

this.dataGridView1.DataSource = this.dsResultdt;

}

catch (Exception ex)

{

this.dsResultdt.Clear();

this.lblError.Text = ex.Message;

}

finally

{

connection.Close();

}

}

private void btnExecute_Click(object sender, EventArgs e)

{

this.execute();

}

private void btnOk_Click(object sender, EventArgs e)

{

this.command.CommandText = this.txtCommandText.Text;

}

private void btnCancel_Click(object sender, EventArgs e)

{

this.DialogResult = DialogResult.Cancel;

}

private void txtCommandText_TextChanged(object sender, EventArgs e)

{

if (this.txtCommandText.Text != null &&

this.txtCommandText.Text.Trim().Length > 0)

{

this.btnExecute.Enabled = true;

}

else

{

this.btnExecute.Enabled = false;

}

}

}

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Thursday, September 27, 2007 1:57 PM
Thank You, I'll try it
Ahmed Hashish  Monday, October 01, 2007 11:09 AM

Hi

What are the namespaces Or dll's required to do this
Muneer essa  Tuesday, April 15, 2008 7:41 AM

Namespace: System.Drawing.Design

Assembly: System.Drawing (in system.drawing.dll)

Rong-Chun Zhang  Tuesday, April 15, 2008 7:45 AM

thanks

but i am getting the following error

The type or namespace name 'CommandTextUIEditor' could not be found (are you missing a using directive or an assembly reference?)

Muneer essa  Tuesday, April 15, 2008 9:14 AM

Hi

The CommandTextUIEditor is a custom UITypeEditor, you should create this class yourself, please check my sample above.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, April 15, 2008 9:25 AM


hi
okay i've created the class
but now i am getting this error

The type or namespace name 'IWindowsFormsEditorService' could not be found (are you missing a using directive or an assembly reference?)

Muneer essa  Tuesday, April 15, 2008 9:31 AM

Namespace: System.Windows.Forms.Design
Assembly: System.Windows.Forms (in system.windows.forms.dll)

For more information, you can check the document of .Net framework
http://msdn2.microsoft.com/en-us/library/aa139615.aspx

Rong-Chun Zhang  Tuesday, April 15, 2008 9:39 AM

okay ... i get it

but can i call the query builder at run time
Muneer essa  Tuesday, April 15, 2008 9:42 AM

Hi

The CusCommandTextEditor is actually a Form; you can create an instance of this form and use it.

Hope this helps.
Best regards.
Rong-Chun Zhang

Rong-Chun Zhang  Tuesday, April 15, 2008 9:51 AM


i know that

but can i invoke the built-in query builder of visual studio at run time
Muneer essa  Tuesday, April 15, 2008 9:54 AM

You can use google to search for other answers

Custom Search

More Threads

• Adding controls at design time
• SnapToGrid System.ComponentModel.Design.DesignSurface question
• An error occurred while parsing EntityName
• How do I force the printer to print in grayscale mode from C#?
• Updating Properties Window at design-time
• Re: designer can't load form. Where is 'more information'?
• Exposing ListView Column Header Collection Editor on a composite control
• ScrollBars location for panel
• ActiveX Control in Windows Service designer
• Looking for free "gauge" control