2013-10-21 24 views
0

頁面加載時我從查詢字符串中提取參數並插入到數據庫中。初始化字符串的格式不符合在調試過程中從索引0開始的規範

這裏是web.config文件

<?xml version="1.0"?> 
<!-- 
    For more information on how to configure your ASP.NET application, please visit 
    http://go.microsoft.com/fwlink/?LinkId=169433 
    --> 
<configuration> 
    <connectionStrings> 
    <add name="MyConsString" connectionString="Data Source=localhost\SQLEXPRESS;Initial Catalog=MCAS_TEST;Integrated Security=SSPI;" providerName="System.Data.SqlClient"/> 
    </connectionStrings> 
    <system.web> 
    <compilation debug="true" targetFramework="4.5"/> 
    <httpRuntime targetFramework="4.5"/> 
    </system.web> 
</configuration> 

這是我的aspx頁面編碼

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <title>Sample Configuration Page</title> 
    <style type="text/css"> 
     .style1 
     { 
      width: 100%; 
     } 
    </style> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
     <table class="style1"> 
      <tr> 
       <td>IP_Address:</td> 
       <td> 
        <asp:TextBox ID="x" runat="server"></asp:TextBox> 
       </td> 
      </tr> 
      <tr> 
       <td>MAC_Address:</td> 
       <td> 
        <asp:TextBox ID="y" runat="server"></asp:TextBox> 
       </td> 
      </tr> 

     </table> 
    </div> 
    <asp:Button ID="Button1" runat="server" Text="Save"/> 
    </form> 
</body> 
</html> 

aspx頁面背後的C#代碼是

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 


public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

     if (Request.QueryString["x"] != null) 
     { 
      insertData(); 
     } 

    } 
    public void insertData() 
    { 
     using (SqlConnection con = new SqlConnection("MyConsString")) 
     { 
      con.Open(); 
      try 
      { 
       using (SqlCommand cmd = new SqlCommand("INSERT INTO Test(x, y) VALUES(@x, @y)", con)) 
       { 
        cmd.Parameters.Add(new SqlParameter("x", Request.QueryString["x"])); 
        cmd.Parameters.Add(new SqlParameter("y", Request.QueryString["y"])); 
        cmd.ExecuteNonQuery(); 
       } 
      } 
      catch (Exception Ex) 
      { 
       // Console.WriteLine("Unable To Save Data. Error - " + Ex.Message); 
       Response.Write("Unable To Save Data. Error - " + Ex.Message); 
      } 
     } 
    } 
} 

現在在執行它我有錯誤,初始化字符串的格式不符合specificatio ñ在指數0開始

我沒有得到爲什麼...

回答

0

我修改自己的代碼的一些研究是如下,它的工作以後。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 
using System.Data.SqlClient; 


public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

     if (Request.QueryString["x"] != null) 
     { 
      if (Request.QueryString["y"] != null) 
      { 
       insertData(); 
      } 
     } 
    // else 
     // { 
    //   Response.Redirect("http://localhost:53627/Default.aspx"); 
    // } 


    } 
    public void insertData() 
    { 
     using (SqlConnection con = new SqlConnection(GetConnectionString())) 
     { 
      con.Open(); 
      try 
      { 
       using (SqlCommand cmd = new SqlCommand("INSERT INTO Test(x, y) VALUES(@x, @y)", con)) 
       { 
        cmd.Parameters.Add(new SqlParameter("x", Request.QueryString["x"])); 
        cmd.Parameters.Add(new SqlParameter("y", Request.QueryString["y"])); 
        cmd.ExecuteNonQuery(); 
       } 
      } 
      catch (Exception Ex) 
      { 
       // Console.WriteLine("Unable To Save Data. Error - " + Ex.Message); 
       Response.Write("Unable To Save Data. Error - " + Ex.Message); 
      } 
     } 

    } 
    public string GetConnectionString() 
    { 
     return System.Configuration.ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString; 
    } 
} 
相關問題