2012-06-29 91 views
0

在这里,我写的上传图片的编码control.but获得在SqlConnection的地方一些运行时error.error母猪 首先我
1.图像名称框 - 文本框
2.Image上传控制 - ASP imageupload控件
3.上传按钮
的SqlConnection运行时错误

错误:从未同步的代码块调用对象同步方法。

代码下面

public partial class ProfileDetails : System.Web.UI.Page 
{ 
    string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; 
//SqlConnection con = new SqlConnection("Data Source=CHATHU-LAPTOP;Initial Catalog=ProfilemgtDB;User ID=sa;Password=sa123"); 
protected void Page_Load(object sender, EventArgs e) 
{ 

} 
protected void Button1_Click(object sender, EventArgs e) 
{ 

} 

protected void Upload_Click(object sender, EventArgs e) 
{ 
    string path = Server.MapPath("images/"); 
    if (FileUpload1.HasFile) 
    { 
     string ext = Path.GetExtension(FileUpload1.FileName); 
     if (ext == ".jpg" || ext == ".png") 
     { 
      FileUpload1.SaveAs(path + FileUpload1.FileName); 
      string name = "~/images/" + FileUpload1.FileName; 
      string s = "Insert into Profile values('" + TextBox12.Text.Trim() + " '.'" + name + "')"; 

      SqlConnection con = new SqlConnection(connStr); 
      SqlCommand cmd = new SqlCommand(s, con); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
      con.Close(); 
      Response.Write("File Uploaded"); 
     } 
     else 
     { 
      Response.Write("You can upload only JPG & PNG"); 
     } 

    } 
    else { 

     Response.Write("Please Select File"); 
    } 
    } 
} 

ERROR:对象同步方法是从代码不同步块调用。

+0

哪一行是抛出该错误? –

+0

SqlConnection con = new SqlConnection(connStr); –

+2

可能与你的错误没有任何关系,但是这个代码是一个等待发生的灾难。切勿使用字符串连接来创建sql值以传递INSERT,UPDATE,DELETE,SELECT等操作 – Steve

回答