RSS

Monthly Archives: June 2013

add an item to combobox before bind data


Solution 1:

DataTable dt = new DataTable();

// cboTown.Items.Clear();

if (obj.RowCount > 0)
{
// cboTown.Items.Insert(-1, “—-For All—-“);
dt = obj.DefaultView.Table;
DataRow row = dt.NewRow();
row[“town”] = “—-For All—-“;
row[“surveyformid”] = “0”;
dt.Rows.InsertAt(row, 0);

cboTown.DataSource = dt;
cboTown.DisplayMember = “town”;
cboTown.ValueMember = “surveyformid”;
this.cboTown.SelectedIndex = 0;
}

Solution 2;

DataTable table = new DataTable("myData");
using (SqlConnection conn = new SqlConnection(connString))
{
    using (SqlDataAdapter da = new SqlDataAdapter(@"SELECT * FROM MyTable", conn))
         da.Fill(table);
}

DataRow row  = table.NewRow();
row["town"] =  "Please select bellow...";
  row["surveyformid"] = "0";
table.Rows.InsertAt(row, 0); 
 cboTown.DataSource = table;
                cboTown.DisplayMember = "town";
                cboTown.ValueMember = "surveyformid";
                this.cboTown.SelectedIndex = 0;
  this.cboTown.SelectedIndex = 0;

Untitled
 
Leave a comment

Posted by on June 12, 2013 in WinForm

 

Tags: ,

remove html tags sql server


One of the developer at my company asked is it possible to parse HTML and retrieve only TEXT from it without using regular expression. He wanted to remove everything between < and > and keep only Text. I found question very interesting and quickly wrote UDF which does not use regular expression.

Following UDF takes input as HTML and returns TEXT only. If there is any single quotes in HTML they should be replaced with two single quotes (not double quote) before it is passed as input to function.

CREATE FUNCTION [dbo].[udf_StripHTML]
(@HTMLText VARCHAR(MAX))
RETURNS VARCHAR(MAX)
AS
BEGIN
DECLARE @Start INT
DECLARE @End INT
DECLARE @Length INT
SET @Start = CHARINDEX('<',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
SET @Length = (@End - @Start) + 1
WHILE @Start > 0
AND @End > 0
AND @Length > 0
BEGIN
SET @HTMLText = STUFF(@HTMLText,@Start,@Length,'')
SET @Start = CHARINDEX('<',@HTMLText)
SET @End = CHARINDEX('>',@HTMLText,CHARINDEX('<',@HTMLText))
SET @Length = (@End - @Start) + 1
END
RETURN LTRIM(RTRIM(@HTMLText))
END
GO

 

 

est above function like this :

SELECT dbo.udf_StripHTML('<b>UDF at humrahimcs </b><br><br><a href="http://www.https://humrahimcs.wordpress.com">https://humrahimcs.wordpress.com</a>')

Result Set:

UDF at humrahimcs

 

select
substring ([dbo].[Team].Description,0,50) ,
substring (dbo.udf_StripHTML([dbo].[Team].Description),0,50)
from
[dbo].[Team]

 
Leave a comment

Posted by on June 3, 2013 in SQL Query

 

Tags: ,