Friday, 30 September 2016

How to Pass large text in Ajax and C#

Pass large text in Ajax and C#

Some times we  came to some situation that we need to insert large text without posting the form.
That is form will not be submitted , but the data will be saved.Here is an example.First i am giving the Html & script

 ----------------------------------------------------------------------------------------------------------------------------

 <script type="text/javascript" src="http://code.jquery.com/jquery.min.js" ></script>
<textarea name="textarea" class="textarea_class" ></textarea>


<input type=text class="text _class" />


<button id="btnSave" value="Save" onclick="Save()" ></button>

<script type="text/javascript">
    function Save() 
        {
        var Xhtml = $('.textarea_class').val();
        var encodedHTML = escape(Xhtml);         //here encoding Html.
        var Title = $('.jqte-test').val();;       
        $.ajax({
            type: "POST",
            url: "BlogWrite/SaveData",
            data: "{Title:'" + Title + "',Html:'" + encodedHTML + "'}",
            contentType: "application/json",
            dataType: "json",
            success: function (data) {
                alert('02');
               
            },
            error: function (data) {
                alert('023');
            }
        });
    }
</script> 

 
here , i am using jqery axaj direct from cdn .We will put a large text on the textarea.Call ajax

-----------------------------------------------------------------------------------------------------------------
Here is the MVC controlle code
 
public string SaveData(string Title, string Html)
        {
            SqlConnection Con = new SqlConnection(@"your connection string");
            Con.Open();            
            try
            {
                SqlCommand Cmd = new SqlCommand();
                Cmd.Connection = Con;
                Cmd.CommandText = "sp_blog_topics";
                Cmd.CommandType = CommandType.StoredProcedure;
                Cmd.Parameters.AddWithValue("@in_mode", 1);
                Cmd.Parameters.AddWithValue("@in_title", Title);
                Cmd.Parameters.AddWithValue("@in_html", Html);
                Cmd.ExecuteNonQuery();
                /*
                SqlDataAdapter adpt = new SqlDataAdapter(Cmd);
                DataSet ds = new DataSet();
                adpt.Fill(ds);
                 * */
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine("{0} First exception caught.", e);
            }
            finally
            {
                Con.Close();
            }
            string retMsg = "Saved";
            return retMsg;
        }
The title data and html is send to stored procedure and then save
 ------------------------------------------------------------------------------
Here is the stored procedure


CREATE PROCEDURE dbo.sp_blog_topics
(
       @in_mode INT,
       @in_title VARCHAR(MAX),
       @in_html VARCHAR(MAX)
)     
AS    
       IF (@in_mode=1)
       BEGIN
       INSERT INTO tbl_topic(Title,Html)
       SELECT @in_title,@in_html
       END

       RETURN

 
 

No comments:

Post a Comment

বাঙালির বেড়ানো সেরা চারটি ঠিকানা

  বাঙালি মানে ঘোড়া পাগল | দু একদিন ছুটি পেলো মানে বাঙালি চলল ঘুরতে | সে সমুদ্রই হোক , পাহাড়ি হোক বা নদী হোক। বাঙালির ...