$INeed$
Well-Known Member
Inserting Values Pictures To The Database
Here in this section I will discuss how to insert values,pictures to the database. I have already discussed in the previous section how to create a database and tables so before going through this topic the concepts of Sql Connection, SqlCommand Object must be cleared.
I have used “Online Shopping Application” as main example.
In building such application websites we need to divide it into two perspective.
i) Admin
ii) User/customer.
Now all the items/products are inserted into the database by the admin.
In this below example I have used class application. A class always reduces complexity and its objects can be used every time without writing the same program .
How to create a class?
Step 1 - Go to Website and click Add New Item
Step 2 - Select Class and name it
Step 3 - Click Add
Example: Inserting Categories To the Database:
Class Used:
i)Category.cs
ii)Dal.cs
 
Category.cs Program Source Code:
	
	
	
		
Dal.cs Program Source Code:
	
	
	
		
Now The Database Part:
Table used
Category:
i) CategoryID- int**primary key
ii) CategoryName- varchar(50)
iii) CategoryDescription- varchar(50)
iv) CategoryImage- varchar(100)
Here in the above diagram:
There are two textboxs
i) TextBox1-CategoryName
ii) TextBox2-CategoryDescription
A FileUpload tool is used name-FileUpload1
Label-lblmsg used to display result during run time
And a Button-Button1(Add Category) Where the main program source code is written.
Source Code:
	
	
	
		
				
			Here in this section I will discuss how to insert values,pictures to the database. I have already discussed in the previous section how to create a database and tables so before going through this topic the concepts of Sql Connection, SqlCommand Object must be cleared.
I have used “Online Shopping Application” as main example.
In building such application websites we need to divide it into two perspective.
i) Admin
ii) User/customer.
Now all the items/products are inserted into the database by the admin.
In this below example I have used class application. A class always reduces complexity and its objects can be used every time without writing the same program .
How to create a class?
Step 1 - Go to Website and click Add New Item
Step 2 - Select Class and name it
Step 3 - Click Add
Example: Inserting Categories To the Database:
Class Used:
i)Category.cs
ii)Dal.cs
Category.cs Program Source Code:
		Код:
	
	using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// Summary description for Category
/// </summary>
public class Category
{
    private int _catid;
    private String _catname;
    private String _catdesc;
    private String _catimage;
    public Category()
    {
        this._catid = -1;
        this._catname = "";
        this._catdesc = "";
        this._catimage= "";
    }
    public int CategoryID
    {
        get
        {
            return _catid;
        }
        set
        {
            _catid = value;
        }
    }
    public String CategoryName
    {
        get
        {
            return _catname;
        }
        set
        {
            _catname = value;
        }
    }
    public String CategoryDescription
    {
        get
        {
            return _catdesc;
        }
        set
        {
            _catdesc = value;
        }
    }
    public String filename
    {
        get
        {
            return _catimage;
        }
        set
        {
            _catimage = value;
        }
    }
    public int AddCategory()
    {
        int dup =0;
        int row=0;
        String query = "insert into category values('" + CategoryName + "','" + CategoryDescription + "','" + filename + "')";
        dal dal1 = new dal();
        dup =  dal1.checkDuplicate("select * from category where categoryname='" + CategoryName + "'");
        if (dup == 0)
      {
          row = dal1.InsertUpdateDelete(query);
      }
      else
          row = -1;
      return row;
  }
}
	Dal.cs Program Source Code:
		Код:
	
	using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
/// Summary description for dal
public class dal
{
    String mystring = System.Configuration.ConfigurationManager.ConnectionStrings["OurDatabaseConnectionString1"].ConnectionString;
    public dal()
      {
           
      }
    public int InsertUpdateDelete(String query)
    {
        int result = 0;
        using (SqlConnection con = new SqlConnection(mystring))
        {
            con.Open();
            SqlCommand com = new SqlCommand(query, con);
          
        }
        return result;
    }
    public DataSet getRecord(String query)
    {
        DataSet ds = null;
        using (SqlConnection con = new SqlConnection(mystring))
        {
            con.Open();
            SqlCommand com = new SqlCommand(query, con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            ds=new DataSet();
            da.Fill(ds);
        }
        return ds;
    }
    public int checkDuplicate(String query)
    {
        int result = 0;
        using (SqlConnection con = new SqlConnection(mystring))
        {
            con.Open();
            SqlCommand com = new SqlCommand(query, con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds);
            result = ds.Tables[0].Rows.Count;
        }
        return result;
    }
}
	Now The Database Part:
Table used
Category:
i) CategoryID- int**primary key
ii) CategoryName- varchar(50)
iii) CategoryDescription- varchar(50)
iv) CategoryImage- varchar(100)
Here in the above diagram:
There are two textboxs
i) TextBox1-CategoryName
ii) TextBox2-CategoryDescription
A FileUpload tool is used name-FileUpload1
Label-lblmsg used to display result during run time
And a Button-Button1(Add Category) Where the main program source code is written.
Source Code:
		Код:
	
	using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
public partial class addcategory : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        TextBox3.Visible = false;
        if (IsPostBack == false)
        {
           
        }
    }
   
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        String filename = "";
        if (FileUpload1.HasFile)
        {
            filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
            string ext = Path.GetExtension(FileUpload1.PostedFile.FileName);
            if (ext.Equals(".jpg") || ext.Equals(".gif") || ext.Equals(".png") || ext.Equals(".bmp"))
            {
                FileUpload1.PostedFile.SaveAs(Server.MapPath("images/" + filename));
               
            }
            else
            {
                lblmsg.Text = "File Type is not supported";
                return;
            }
        }
        Category cat1 = new Category();
        cat1.CategoryName = TextBox1.Text;
        cat1.CategoryDescription = TextBox2.Text;
        cat1.filename = filename;
        int result = cat1.AddCategory();
        if (result == -1)
        {
            lblmsg.Text = "Category " + TextBox1.Text + "  already exists";
        }
        else
        {
            lblmsg.Text = "Category" + TextBox1.Text + " succesfully inserted";
        }
    }
   
}