上传图片代码无法在asp.net中运行c# [英] upload image code not working in asp.net c#

查看:88
本文介绍了上传图片代码无法在asp.net中运行c#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我有5个字段。序列号,类别是下拉列表,描述图片上传字段和成本价格。



我想从文本框和图像添加序列号,类别描述成本价格从fileupload控件上传并存储在sql server中的数据库中,并显示在visual studio的文件夹中。



i编写了代码,但它无效。



i已将序列号isidentity属性设置为是。



因此,在运行时字段将被移动1行。代替类别1插入,代替描述类别值插入等等。



我发送代码。



任何人都可以查看并告诉我。请在这里停留很长时间。



预先谢谢



Hi
I have 5 fields. serial number,category which is a dropdown list, description image upload field and cost price.

I want to add serial number,category description cost price from text box and image to be uploaded from fileupload control and get stored in database in sql server and shows in folder in visual studio.

i have written the code but its not working.

i have made serial number isidentity property to yes.

so for this at runtime fields are getting moved by 1 row. in place of category 1 getting inserted, in place of description category value getting inserted and so on and so forth.

I am sending the code.

Can anyone check and tell me please.stuck here for a long time.

Thanks In advance

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;
using System.IO;
public partial class AddEarRing : 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 invent values('" + txtcode.Text + "', '" + DropDownList1.Text + "', '" + txtdesc.Text + "', '" + TextBox1.Text + "', '" + TextBox4.Text + "', '" + txtprice.Text + "')", con);
 

cmd.ExecuteNonQuery();
con.Close();
 

 
Label8.Visible = true;
Label8.Text = "Your Data Successfully added";
txtcode.Text = "";
 
txtdesc.Text = "";
TextBox1.Text = "";
TextBox4.Text = "";
txtprice.Text = "";
}
 
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
string path = Server.MapPath("Images/");
 
if (FileUpload1.HasFile)
{
string ext = Path.GetExtension(FileUpload1.FileName);
if (ext == ".jpg" || ext == ".png" || ext == ".jpg")
{
FileUpload1.SaveAs(path + FileUpload1.FileName);
string name = "~/Images/" + FileUpload1.FileName;
 
string s = ("insert into invent values('" + TextBox4.Text + "'");
SqlCommand cmd = new SqlCommand(s, con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Label9.Text = "Your file has been uploaded";
 
}
else
{
Response.Write("Please select a file");
}
}
}
}

推荐答案

HI,希望这个链接对你有帮助,



< a href =http://www.c-sharpcorner.com/UploadFile/0c1bb2/uploading-images-to-database-using-Asp-Net-C-Sharp/> http://www.c-sharpcorner。 COM / UploadFile / 0c1bb2 /上载图像到大tabase-using-Asp-Net-C-Sharp / [ ^ ]
hope this link will help you,

http://www.c-sharpcorner.com/UploadFile/0c1bb2/uploading-images-to-database-using-Asp-Net-C-Sharp/[^]


您可以编写以下代码来上传图片





You can write following code for uploding image


string path = "";
      path = Server.MapPath("./img/");

      if (f1.HasFile)
      {
          path = path + Session["uname"] + ".jpg";
          f1.SaveAs(path);
          string img2;
          img2 = "~/img/" + Session["uname"]+".jpg";
          img1.ImageUrl = img2;
        
      }


这篇关于上传图片代码无法在asp.net中运行c#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆