2013-04-16 3 views
-1

이 코드는 단추를 의미합니다.
그러나 언제든지 실행할 수있는 것은 ArgumentException was unhandled by user code입니다.SqlConnection을 여는 중 ArgumentException이 발생했습니다.

충분한 데이터베이스가 있는지 확인하기 위해 데이터베이스를 검색하는 사이트입니다. TxtCitytxtCity을해야한다는

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; 
using System.Configuration; 

public partial class _Default : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     con.Open(); 
    } 
    protected void Button1_Click(object sender, EventArgs e) 
    { 
     SqlCommand cmd = new SqlCommand("insert into Toner Values('"+txtFname.Text+"','"+txtLname.Text+"','"+TxtCity.Text+"')",con); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     Label1.Visible = true; 
      Label1.Text = "Indsætning succesfuld!!!"; 
     TxtCity.Text = ""; 
     txtFname.Text = ""; 
     txtLname.Text = ""; 
    } 
    protected void TextBox3_TextChanged(object sender, EventArgs e) 
    { 

    } 
} 
+2

당신은 더 구체적으로해야합니다. 코드가 끊어지는 라인은 무엇입니까? – NibblyPig

+0

LINQTOSQL을 사용하고 있습니까? – gasroot

+0

해당 내용이 다음과 같이 변경되었습니다. 'SqlConnection con = new SqlConnection (ConfigurationManager.ConnectionStrings [ "ConnectionString"]. ConnectionString);' 그리고 LINQTOSQL을 사용하지 마십시오. 방금 ​​데이터베이스 로컬을 만들었습니다. –

답변

-1

내 추측이다.

이 시도 : 유래를 사용할 때

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; 
using System.Configuration; 

public partial class _Default : System.Web.UI.Page 
{ 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     string query = "insert into Toner Values (@Fname, @Lname, @City)"; 
     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString); 
     SqlCommand cmd = new SqlCommand(query, con); 
     //Use parameterized query to prevent SQL injection 
     cmd.Parameters.Add("Fname", SqlDbType.VarChar, 50).Value = txtFname.Text; 
     cmd.Parameters.Add("Lname", SqlDbType.VarChar, 50).Value = txtLname.Text; 
     //C# is case-sensitive... is it txtCity or TxtCity? 
     cmd.Parameters.Add("City", SqlDbType.VarChar, 50).Value = TxtCity.Text; 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
     Label1.Visible = true; 
     Label1.Text = "Indsætning succesfuld!!!"; 
     TxtCity.Text = ""; 
     txtFname.Text = ""; 
     txtLname.Text = ""; 
    } 
} 
+0

-1 : 연결 초기화 중입니다. –

관련 문제