Showing posts with label cursor. Show all posts
Showing posts with label cursor. Show all posts

Friday, 20 January 2017

SQL CURSOR, looping on talbe's or view's rows with Cursor like Recordset or DataReader

Hi All, here an example on how loop on some rows in SQL Stored Procedure, like a visual basic RecordSet or .NET Framework DataReader, simple put your query at line 7 of script:

SET @SQL ='SELECT Field1,Field2 FROM Table_Name '

That's all, We have spent lot of time to find an example which works, finally it comes:








--LWebCode SQL Script
DECLARE @SQL NVARCHAR (4000)
DECLARE @DynamicSQL NVARCHAR(250)
DECLARE @Field1 NVARCHAR(100)
DECLARE @Field2 NVARCHAR(100)
DECLARE @outputCursor CURSOR

SET @SQL ='SELECT Field1,Field2 FROM Table_Name '
SET @DynamicSQL = 'SET @outputCursor = CURSOR FORWARD_ONLY STATIC FOR ' +
@SQL + ' ; OPEN @outputCursor'
exec sp_executesql -- sp_executesql will essentially create a sproc
@DynamicSQL, -- The SQL statement to execute (body of sproc)
N'@outputCursor CURSOR OUTPUT', -- The parameter list for the sproc: OUTPUT CURSOR
@outputCursor OUTPUT
FETCH NEXT FROM @outputCursor INTO @Field1,@IDField2
WHILE @@FETCH_STATUS = 0
BEGIN
/*here is loop put your code here*/
SET @SQL='SELECT * FROM t_name WHERE Field1=''' + @Field1 + ''''
EXEC SP_EXECUTESQL @SQL
FETCH NEXT FROM @outputCursor INTO @Field1,@Field2
END

Wednesday, 11 January 2017

C# AJAX TextBox Cursor,Caret Focus with AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"

Hi All, here explain how to mantain get and set cursor/caret position after a c# with AJAX Textbox TextChanged with AutoPostBack="True", this solution is cross browser compatible.
Put this Javascript code in your *.aspx Page:

<script language="javascript" type="text/javascript" >
//lwebcode.bloggger.com
//http://blog.vishalon.net/javascript-getting-and-setting-caret-position-in-textarea
var selstart=0;
var selend=0;

 var pos = 0;
 function GetPos(el)
 {
    if("selectionStart" in el) {
       pos = el.selectionStart;
    } else if("selection" in document) {
       el.focus();
       var Sel = document.selection.createRange();
       var SelLength = document.selection.createRange().text.length;
       Sel.moveStart("character", -el.value.length);
       pos = Sel.text.length - SelLength;
    }
    return pos;
 }

 function SetPos(ctrl, mypos)
 {
        if(ctrl.setSelectionRange)
        {
            ctrl.focus();
            ctrl.setSelectionRange(mypos, mypos);
        }
        else if (ctrl.createTextRange)
        {
            var range = ctrl.createTextRange();
            range.collapse(true);
            range.moveEnd('character', mypos);
            range.moveStart('character', mypos);
            range.select();
        }
  }
</script>


Always in your *.aspx page:

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged" > 
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" OnTextChanged="TextBox2_TextChanged" >

On Page Code behind *.apsx.cs

private const string SCRIPT_DOFOCUS =
        @"window.setTimeout('DoFocus()', 1);
    function DoFocus()
    {
        try {
            document.getElementById('REQUEST_LASTFOCUS').focus();
            SetPos(document.getElementById('REQUEST_LASTFOCUS'), pos);
        } catch (ex) {}
    }";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HookOnFocus(this.Page as Control);
        }
        ScriptManager.RegisterStartupScript(this, typeof(Page), "ScriptDoFocus", SCRIPT_DOFOCUS.Replace("REQUEST_LASTFOCUS", Request["__LASTFOCUS"]), true);
    }
   
    private void HookOnFocus(Control CurrentControl)
    {
        //checks if control is one of TextBox, DropDownList, ListBox or Button
        if ((CurrentControl is TextBox)
            /*|| 
            (CurrentControl is DropDownList) ||
            (CurrentControl is ListBox) ||
            (CurrentControl is Button)*/
           
            )
            //adds a script which saves active control on receiving focus
            //in the hidden field __LASTFOCUS.
            (CurrentControl as WebControl).Attributes.Add(
               "onfocus",
               "try{document.getElementById('__LASTFOCUS').value=this.id; GetPos(this);} catch(e) {}");
        //checks if the control has children
        if (CurrentControl.HasControls())
            //if yes do them all recursively
            foreach (Control CurrentChildControl in CurrentControl.Controls)
                HookOnFocus(CurrentChildControl);
    }


protected void TextBox1_TextChanged(object sender, EventArgs e) 
{ //your code... }
protected void TextBox2_TextChanged(object sender, EventArgs e) 
{ //your code... }