Dear Team,
i want to save image as well as data in database but still i am stack here.
plz check and help me.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
namespace Iamgedatasave
{
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string conn_string = @"Data Source=(localdb)\Projects;Initial Catalog=Emp;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";
SqlConnection con = new SqlConnection(conn_string);
//create command object
SqlCommand cmd = new SqlCommand();
//pass connection and query to your command object
cmd.Connection = con;
cmd.CommandText = "Select * from Prod";
//create adaptor to fill data from database
SqlDataAdapter da = new SqlDataAdapter(cmd);
//create datatable which holds the data
DataTable dt = new DataTable();
da.Fill(dt);
//bind your data to gridview
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string pr_name = TextBox1.Text;
int pr_price = Convert.ToInt32(TextBox2.Text);
//take virtual path to store in the database
string path = "~/Image/" + FileUpload1.FileName;
//create insert query to store data
string query = "insert into prod values('" + pr_name + "', " + pr_price + ",'" + path + "')";
//store image in folder image. to get the absolute path we use Server.MapPath
FileUpload1.SaveAs(Server.MapPath("Image")+"/"+FileUpload1.FileName);
//Create connection. kindly change the conn_strig according to your system
string conn_string = @"Data Source=(localdb)\Projects;Initial Catalog=Emp;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False";
SqlConnection con = new SqlConnection(conn_string);
con.Open();
//create command object
SqlCommand cmd = new SqlCommand();
//pass connection and query to your command object
cmd.Connection = con;
cmd.CommandText = query;
int x = cmd.ExecuteNonQuery();
con.Close();
if (x > 0)
{
Response.Write("Item inserted Successfully");
}
else
{
Response.Write("Try Again");
}
}
}
}