2017-06-20 76 views
5

我想保存使用肥皂Jquery的C#的WebMethod數據,但無法保存數據SQL服務器請幫助我,我怎麼能節省使用香皂的jQuery數據。我使用IDE Visual Studio 2015如何在SQL Server 2012中使用Soap Jquery保存數據?

<script type="text/javascript"> 

    function SavePersonRecord() { 

     var Name = $.trim($('#<%=txtName.ClientID %>').val()); 
     var LName = $.trim($('#<%=txtlname.ClientID %>').val()); 
     var Company = $.trim($('#<%=txtCompany.ClientID %>').val());  
     var Messege = "";   
     if (Name == '') { 
      Messege = "Can not Blank Name"; 
     }   
     if (LName == '') { 
      Messege += "Can not Blank Last Name"; 
     } 
     if (Company == '') { 
      Messege += " Company Name Can not Blank"; 
     } 
     if (Messege.length == 0) {   
      $.ajax({ 
       type: "POST", 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       url: "Soap-Service.aspx/InsertPersonRecord", 
       data: "{'FirstName':'" + Name + "', 'LastName':'" + LName + "','Company':'" + Company + "'}", 
       success: function (Record) { 

         $('#txtName').val(); 
         $('#txtlName').val(); 
         $('#txtCompany').val(); 

        if (Record.d == true) { 

         $('#Result').text("Your Record insert"); 
        } 
        else { 
         $('#Result').text("Your Record Not Insert"); 
        } 

       }, 
       Error: function (textMsg) { 

        $('#Result').text("Error: " + Error); 
       } 
      }); 
     } 
     else {    
      $('#Result').html(''); 
      $('#Result').html(Messege); 
     } 
     $('#Result').fadeIn(); 
    } 
    </script> 





    <table border="0" cellpadding="5" cellspacing="5" style="border: solid 2px Green;"> 
           <tr> 
            <td colspan="2" style="background-color: red; color: white; font-weight: bold; font-size: 12pt; text-align: center; font-family: Verdana;">Enter Employee Information</td> 
           </tr> 
           <tr> 
            <td>First Name: 
            </td> 
            <td> 
             <asp:TextBox ID="txtName" runat="server" Text="" /> 
            </td> 
           </tr> 
           <tr> 
            <td>Last Name: 
            </td> 
            <td> 
             <asp:TextBox ID="txtlname" runat="server" /> 
            </td> 
           </tr> 
           <tr> 
            <td>Company 
            </td> 
            <td> 
             <asp:TextBox ID="txtCompany" runat="server" /> 
            </td> 
           </tr> 
           <tr> 
            <td></td> 
            <td> 
             <asp:Button ID="btnInsertRecord" Text="Save" runat="server" OnClientClick="SavePersonRecord(); return false" /> 
            </td> 
           </tr> 
          </table> 

enter image description here

+1

請問這個問題得到upvoted?請描述你的努力和具體問題。 –

+0

@RomanoZumbé正是我想知道的,一些代碼(你試過的任何代碼)也會有幫助。 –

回答

0

最後,我的代碼,那麼這裏運行的代碼有一個在Web應用程序中的一些小變化

<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script> 
 
    <script type="text/javascript"> 
 
     $(function() { 
 
      $('#btnSubmit').click(function() { 
 
       var Name = $('#txtname').val(); 
 
       var Contact = $('#txtcontact').val(); 
 
       //var body = $('#txtbody').val(); 
 
       if (Name != '' && Contact != '') { 
 
        $.ajax({ 
 
         type: "POST", 
 
         contentType: "application/json; charset=utf-8", 
 
         url: "Soap-Jquery.aspx/InsertData", 
 
         data: "{'EmpName':'" + Name + "','ConNo':'" + Contact + "'}", 
 
         dataType: "json", 
 
         success: function (data) { 
 
          var obj = data.d; 
 
          if (obj == 'true') { 
 
           $('#txtname').val(''); 
 
           $('#txtcontact').val(''); 
 
           //$('#txtbody').val(''); 
 
           $('#lblmsg').html("Details Submitted Successfully"); 
 
           window.location.reload(); 
 
          } 
 
         }, 
 
         //error: function (result) { 
 
         // alert("Error"); 
 
         //} 
 
        }); 
 
       } 
 
       else { 
 
        alert('Please enter all the fields') 
 
        return false; 
 
       } 
 
      }) 
 
     }); 
 
    </script>
<asp:Panel ID="pnl_add_payment" runat="server"> 
 
    
 
    <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> 
 

 
    <section> 
 

 
     <div class="container"> 
 
      <div class="row"> 
 
       <div class="form-group"> 
 
        <button type="button" class="btn btn-primary ribbon">Data Insert Using Jquery</button> 
 
        <asp:Label ID="lblmsg" runat="server" Text="" ForeColor="Red"></asp:Label> 
 
        <div class="col-md-12" style="margin-top: 15px;"> 
 
         <div class="col-md-2">Enter Name</div> 
 
         <div class="col-md-4"> 
 
          
 
           <asp:TextBox ID="txtname" runat="server" class="form-control" placeholder="Enter Name"></asp:TextBox> 
 
          <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtname" ErrorMessage="*" ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator> 
 
          
 
         </div> 
 
         <div class="col-md-2">Enter Contact No</div> 
 
         <div class="col-md-4"> 
 
          <asp:TextBox ID="txtcontact" runat="server" class="form-control" placeholder="Enter Contact No"></asp:TextBox> 
 
          <asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtcontact" runat="server" ErrorMessage="Only Numbers allowed" ValidationExpression="\d+" ForeColor="Red" ValidationGroup="a"></asp:RegularExpressionValidator> 
 
          <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtcontact" ErrorMessage="*" ForeColor="Red" ValidationGroup="a"></asp:RequiredFieldValidator> 
 
         </div> 
 
         <div class="col-md-6"> 
 
          <asp:Button ID="btnSubmit" runat="server" Text="Submit" value="Submit" ValidationGroup="a" /> 
 
         </div> 
 

 
        </div> 
 

 
       </div> 
 
       
 
       <%--End Form Group--%> 
 
      </div> 
 
      <%--End Row--%> 
 
     </div> 
 
     <%--End Container--%> 
 
    </section>  
 
    <br /><br /> 
 
    <section> 
 
     <div class="container"> 
 
      <div class="row"> 
 
       <div class="col-md-6"> 
 

 

 
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" Width="100%"> 
 

 
         <Columns> 
 
          <asp:BoundField DataField="EmpName" HeaderText="Name" /> 
 
          <asp:BoundField DataField="ConNo" HeaderText="Number" /> 
 
         </Columns> 
 
         <FooterStyle BackColor="#FFFFCC" ForeColor="#330099" /> 
 
         <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" /> 
 
         <PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" /> 
 
         <RowStyle BackColor="White" ForeColor="#330099" /> 
 
         <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" /> 
 
         <SortedAscendingCellStyle BackColor="#FEFCEB" /> 
 
         <SortedAscendingHeaderStyle BackColor="#AF0101" /> 
 
         <SortedDescendingCellStyle BackColor="#F6F0C0" /> 
 
         <SortedDescendingHeaderStyle BackColor="#7E0000" /> 
 
         
 
        </asp:GridView> 
 
       </div> 
 
      </div> 
 
     </div> 
 
    </section> 
 

 
</asp:Panel>

[WebMethod] public static string InsertData(string EmpName,string ConNo) string msg = string.Empty; 使用(SqlConnection的CON =新的SqlConnection(ConfigurationManager.ConnectionStrings [ 「構造」]的ConnectionString)。) { 使用(的SqlCommand CMD =新的SqlCommand( 「sp_insertEmp」,CON)) { con.Open(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue(「@ EmpName」,EmpName); cmd.Parameters.AddWithValue(「@ ConNo」,ConNo); int i = cmd.ExecuteNonQuery(); con.Close(); 如果(我== 1){

    msg = "True"; 
       } 
       else 
       { 
        msg = "false"; 
       } 

      } 
     } 

     return msg; 
    } 
2

模糊的問題。但是,請執行以下步驟:

  1. 您是否在瀏覽器控制檯中發現了任何會導致您的JavaScript代碼出錯的錯誤?
  2. 您是否在「錯誤:」部分捕獲了任何錯誤?
  3. 如果上面兩個看起來都不錯,你有沒有把dubugger放在你的webmethod中來檢查它是否被擊中?
  4. 畢竟這是否存在任何服務器端異常(在您的webmethod中)? 4
  5. 回答)是肯定的,張貼例外
+0

沒有先生我沒有得到任何錯誤,但使用調試器它顯示的JavaScript錯誤字符串不是JSON格式 –

+0

這裏的任何問題,只是一個瘋狂的猜測? data:「{'FirstName':'」+ Name +「','LastName':''+ LName +'','Company':'」+ Company +「'}」 –

+0

Sir問題顯示在這裏URL: 「SOAP的服務。aspx/SaveUser「,(字符串是不是JSON格式)相同的腳本我正在使用另一個例子,它工作正常,但是當我在Visual Studio 2015中創建新的Web窗體項目,然後顯示此問題,我分享另一個腳本此腳本是也顯示相同的錯誤,但在另一個項目中的例子它工作正常.. –

0

好吧,我可以看到你與你的Ajax調用了問題。它應該是:

 $.ajax({ 
      type: "POST", 
      dataType: "json", 
      contentType: "application/json; charset=utf-8", 
      url: "Soap-Service.aspx/SaveUser", //Here you got wrong method 
      //right here you should pass object (not string) with right property names (like you have in your web method) 
      data: { FName: Name, LNAme: LName, Company: Company}, 
      success: function (Record) { 
       $('#txtName').val(); 
       $('#txtlName').val(); 
       $('#txtCompany').val(); 

       if (Record.d == true) { 

        $('#Result').text("Your Record insert"); 
       } 
       else { 
        $('#Result').text("Your Record Not Insert"); 
       } 
      }, 
      error: function (textMsg) { 
       $('#Result').text("Error: " + Error); 
      } 
     }); 

上面你的WebMethod你應該增加:

[WebInvoke(Method="POST",ResponseFormat=WebMessageFormat.Json)] 
相關問題