Insert , update and delete in C#. Example.

......................source code.............................................

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master"  CodeFile="product.aspx.cs" Inherits="product" %>

<asp:Content ID="Content1" runat ="server" ContentPlaceHolderID ="ContentPlaceHolder1">
<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Add" />
<asp:Button ID="Button5" runat="server" onclick="Button5_Click" Text="Edit" />


<asp:Panel ID="Panel1" runat="server">    <table class="style3">
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style4">
                Product</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Image</td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Image ID="Image1" runat="server" Width="75px" Height="75px"/>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Add" Width="150px"
                    onclick="Button1_Click" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server">
    <table class="style3">
        <tr>
            <td class="style4">
                Product</td>
            <td>
                <asp:DropDownList ID="DropDownList1" runat="server" Width="175px"
                    onselectedindexchanged="DropDownList1_SelectedIndexChanged"
                    AutoPostBack="True">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Image</td>
            <td>
           
                <asp:Image ID="Image2" runat="server" Width="75px" Height="75px"/>
             
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
              <asp:FileUpload ID="FileUpload2" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                NewImageName</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button2" runat="server" Text="Edit" Width="150px"
                    onclick="Button2_Click" />
                <asp:Button ID="Button3" runat="server" Text="Delete" Width="150px"
                    onclick="Button3_Click" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>

    </asp:Panel>
</asp:Content>
<asp:Content ID="Content2" runat="server" contentplaceholderid="head">
    <style type="text/css">
        .style3
        {
            width: 26%;
            background-color: #FF9933;
        }
        .style4
        {
            width: 88px;
        }
    </style>
</asp:Content>
.........................end source..........................

..............................code behind..................................
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.Collections;
using System.Text;
using System.Data.SqlClient;
public partial class product : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            bind_dropdown();
        }
    }

    SqlDataAdapter da;
    protected void bind_dropdown()
    {
        da = new SqlDataAdapter("select * from prod", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "p_name";
        DropDownList1.DataValueField = "p_id";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--select--", "0"));
    
        
      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        insert_product();
        TextBox1.Text = "";
        Image2.ImageUrl= "";
        

    }

    protected void insert_product()
    {
        try
        {
            con.Open();
            string name = FileUpload1.FileName;
            string path = Server.MapPath("");
            string str = path + "//" + "productimage" + "//" + name;
            FileUpload1.PostedFile.SaveAs(str);
            string s = "~" + "//" + "productimage" + "//" + name;
            string insert = "insert into prod values('" + TextBox1.Text + "','" + s + "')";
            SqlCommand cmd = new SqlCommand(insert, con);

            cmd.ExecuteNonQuery();
            Image1.ImageUrl = s.ToString();
            con.Close();
            Response.Write("Insert successfully!");
        }
        catch(Exception ex)
        {

        }
        

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
      try
        {
            edit();
            TextBox2.Text = "";
            bind_dropdown();
          
        }
        catch(Exception ex)
      {

        }
    }

    protected void deleteOldImage()
    {
        string str = "delete from prod where p_name='" + DropDownList1.SelectedValue + "'"; ;
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    SqlCommand cmd;
    protected void edit()
    {
        deleteimgfiel();
        int id3=id1(DropDownList1.SelectedItem.Text);
        con.Open();
        string name = FileUpload2.FileName;
        string path = Server.MapPath("");
        string str = path + "//" + "productimage" + "//" + name;
        FileUpload2.PostedFile.SaveAs(str);
        string s = "~" + "//" + "productimage" + "//" + name;
       
        string str1 = "update prod set p_name='"+TextBox2.Text+"' ,p_path ='"+s+"' where p_id="+id3+"";
        cmd = new SqlCommand(str1,con);
        
        cmd.ExecuteNonQuery();
        con.Close();

       
    }
    int id2;
    protected int id1(string name)
    {
        
        string str="select p_id from prod where p_name='"+name+"'";
        con.Open();
        cmd=new SqlCommand(str,con);
        SqlDataReader dr=cmd.ExecuteReader();
        
        if(dr.Read())
        {
            id2=Convert.ToInt32(dr[0]);
        }
        con.Close();
        return id2;

    }
    protected void deleteimgfiel()
    { 
    string str="select p_path from prod where p_name='"+DropDownList1.SelectedItem+"'";
        cmd=new SqlCommand(str,con);
        con.Open();
         SqlDataReader dr=cmd.ExecuteReader();
         
        if(dr.Read())
        {
            string path1=Server.MapPath(dr[0].ToString());
            if(System.IO.File.Exists(path1))
            {
                System.IO.File.Delete(path1);
            }
        }

        con.Close();
    
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        bind_dropdown();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        string str = bindImage();
        Image2.ImageUrl = str;
       
    }

    protected string bindImage()
    {
        con.Open();
        string image = "";
        string str = "select p_path from prod where p_id='"+DropDownList1.SelectedValue+"'";
        SqlCommand cmd;
        cmd = new SqlCommand(str,con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            image=dr[0].ToString();
        }
        con.Close();
        return image;
       
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        deleteimgfiel();
        int id3 = id1(DropDownList1.SelectedItem.Text);
        string str="delete from prod where p_id="+id3+"";
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        bind_dropdown();
    }

    
}

..............................end code behind..................................
Share:

How to send mail in C#?

.........................login soure...........

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FFCCFF;
        }
        .style2
        {
            width: 107px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    LoginId</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                        Height="16px" Width="158px">
                        <asp:ListItem>Admin</asp:ListItem>
                        <asp:ListItem>User</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Username</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Height="16px" Width="155px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Height="16px" Width="155px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Login" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
..........................................end login source code.................
..................code behind.........................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;

public partial class login : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    DataSet ds = new DataSet();
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            if (DropDownList1.SelectedValue == "User")
            {
                con.Open();
                Session["user"] = "1";
                cmd = new SqlCommand("select*from textinsert where name='" + TextBox2.Text + "' and pwd='" + TextBox3.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Session["name"]=TextBox2.Text;
                    Session["pwd"]=TextBox3.Text;
                    Response.Redirect("registration.aspx?name="+Session["name"]+" and pwd="+ Session["pwd"]+"");
                }

            }
            if (DropDownList1.SelectedValue == "Admin")
            {
                Session["user"] = "2";
                con.Open();
                cmd = new SqlCommand("select*from textinsert where name='" + TextBox2.Text + "' and pwd='" + TextBox3.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Response.Redirect("registration.aspx");
                }
            }
        }
        catch(Exception ex)
        {
        }
    }

    }


    
....................................registration source code......................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="registration.aspx.cs" Inherits="registration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FF9999;
        }
        .style2
        {
            width: 109px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="style1">
            <tr>
                <td class="style2">
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
                </td>
            </tr>
        </table>
    
    </div>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </p>
    </form>
</body>
</html>
..........................code behind..........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;
public partial class registration : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {   
            string str=Session["user"].ToString();

            if (str=="1")
            {
                user();
            }
            if(str=="2")
            {
                adminbind();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("insert into textinsert values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        //Response.Write("successfully submitted!");
        cmd = new SqlCommand("select count(id) as id from textinsert", con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if(dt.Rows.Count>0)
        {
            
            Int64 count=(Convert.ToInt64(dt.Rows[0]["id"].ToString()));
            string message;

            if (count > 4)
            {
                if (count == 4)
                {
                    message="you got first step";
                    send_email(message);
                }
                if (count == 16)
                {
                    message="you got second step";
                    send_email(message);
                }

                if (count == 50)
                {
                    message = "you third first step";
                    send_email(message);
                }
            }

        }
      
    }

    public void send_email(string msg)
    {
        MailMessage ms = new MailMessage("kulngr21@gmail.com", "kulngr@hotmail.com");
        SmtpClient ab = new SmtpClient();

        ms.Body = msg;
        ms.IsBodyHtml = true;
        ab.EnableSsl = true;
        ab.Send(ms);

        Response.Write("mmail sent...");
    }
     
    protected void adminbind()
    {
        con.Open();
        // string name=Request.QueryString["name"].ToString();
        // string pwd=Request.QueryString["pwd"].ToString();
        string str = "select * from textInsert ";
        da = new SqlDataAdapter(str, con);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    SqlDataAdapter da1;
    protected void user()
    {
        try
        {
            con.Open();
            string name = Session["name"].ToString();
            string str = "select*from textinsert where name='" + name + "'";
            da1 = new SqlDataAdapter(str,con);
            DataTable dt = new DataTable();
            da1.Fill(dt);
          
          
                GridView1.DataSource = dt;
                GridView1.DataBind();
          
            con.Close();
        }
        catch(Exception ex)
        {

        }
    }
}
.........................................end code behind.....................

Share:

How to create references through foreign key among three tables in SQL Server ?

....................................First Example.............

CREATE TABLE t_Analog(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Value] [float] NOT NULL,
    CONSTRAINT [uc_t_Analog] UNIQUE(AppName, ItemName)
)

CREATE TABLE t_Discrete(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Value] [bit] NOT NULL,
    CONSTRAINT [uc_t_Discrete] UNIQUE(AppName, ItemName)
)

CREATE TABLE t_Message(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Value] [nvarchar](256) NOT NULL,
    CONSTRAINT [uc_t_Message] UNIQUE(AppName, ItemName)
)
...................Second Example............
CREATE TABLE t_AllItems(
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [itemType] [int] NOT NULL,
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    CONSTRAINT [pk_t_AllItems] PRIMARY KEY CLUSTERED ( [id] )
    CONSTRAINT [uc_t_AllItems] UNIQUE([id], [AppName], [ItemName])
) ON [PRIMARY]

CREATE TABLE t_Analog(
    [itemId] [bigint] NOT NULL,
    [Value] [float] NOT NULL,
    FOREIGN KEY (itemId) REFERENCES t_AllItems(id)
)

CREATE TABLE t_Discrete(
    [itemId] [bigint] NOT NULL,
    [Value] [bit] NOT NULL,
    FOREIGN KEY (itemId) REFERENCES t_AllItems(id)
)

CREATE TABLE t_Message(
    [itemId] [bigint] NOT NULL,
    [Value] [nvarchar](256) NOT NULL,
    FOREIGN KEY (itemId) REFERENCES t_AllItems(id)
)
..........Third.......................
CREATE TABLE AllItems(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [itemType] [int] NOT NULL,
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    CONSTRAINT [pk_AllItems] PRIMARY KEY CLUSTERED ( [id] ASC )
) ON [PRIMARY]

CREATE TABLE Analog(
    [itemId] [int] NOT NULL,
    [Value] [float] NOT NULL
)

CREATE TABLE Discrete(
    [itemId] [int] NOT NULL,
    [Value] [bit] NOT NULL
)

CREATE TABLE Message(
    [itemId] [bigint] NOT NULL,
    [Value] [nvarchar](256) NOT NULL
)

ALTER TABLE [Analog] WITH CHECK ADD CONSTRAINT [FK_Analog_AllItems] FOREIGN KEY([itemId])
REFERENCES [AllItems] ([id])
GO
ALTER TABLE [Analog] CHECK CONSTRAINT [FK_Analog_AllItems]
GO

ALTER TABLE [Discrete] WITH CHECK ADD CONSTRAINT [FK_Discrete_AllItems] FOREIGN KEY([itemId])
REFERENCES [AllItems] ([id])
GO
ALTER TABLE [Discrete] CHECK CONSTRAINT [FK_Discrete_AllItems]
GO

ALTER TABLE [Message] WITH CHECK ADD CONSTRAINT [FK_Message_AllItems] FOREIGN KEY([itemId])
REFERENCES [AllItems] ([id])
GO
ALTER TABLE [Message] CHECK CONSTRAINT [FK_Message_AllItems]
GO
..............Fourth.....

CREATE TABLE t_Generic(
[AppName] [nvarchar](20) NOT NULL,
[ItemName] [nvarchar](32) NOT NULL,
[Type] [nvarchar](32) NOT NULL,
[AnalogValue] [Float] NULL,
[DiscreteValue] [bit] NULL,
[MessageValue] [nvarchar](256) NULL,
CONSTRAINT [uc_t_Generic] UNIQUE(AppName, ItemName)
)
Share:

Wednesday, 26 December 2012

Insert , update and delete in C#. Example.

......................source code.............................................

<%@ Page Language="C#" AutoEventWireup="true" MasterPageFile="~/MasterPage.master"  CodeFile="product.aspx.cs" Inherits="product" %>

<asp:Content ID="Content1" runat ="server" ContentPlaceHolderID ="ContentPlaceHolder1">
<asp:Button ID="Button4" runat="server" onclick="Button4_Click" Text="Add" />
<asp:Button ID="Button5" runat="server" onclick="Button5_Click" Text="Edit" />


<asp:Panel ID="Panel1" runat="server">    <table class="style3">
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
        <tr>
            <td class="style4">
                Product</td>
            <td>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Image</td>
            <td>
                <asp:FileUpload ID="FileUpload1" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Image ID="Image1" runat="server" Width="75px" Height="75px"/>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button1" runat="server" Text="Add" Width="150px"
                    onclick="Button1_Click" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>
    </asp:Panel>
    <asp:Panel ID="Panel2" runat="server">
    <table class="style3">
        <tr>
            <td class="style4">
                Product</td>
            <td>
                <asp:DropDownList ID="DropDownList1" runat="server" Width="175px"
                    onselectedindexchanged="DropDownList1_SelectedIndexChanged"
                    AutoPostBack="True">
                </asp:DropDownList>
            </td>
        </tr>
        <tr>
            <td class="style4">
                Image</td>
            <td>
           
                <asp:Image ID="Image2" runat="server" Width="75px" Height="75px"/>
             
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
              <asp:FileUpload ID="FileUpload2" runat="server" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                NewImageName</td>
            <td>
                <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                <asp:Button ID="Button2" runat="server" Text="Edit" Width="150px"
                    onclick="Button2_Click" />
                <asp:Button ID="Button3" runat="server" Text="Delete" Width="150px"
                    onclick="Button3_Click" />
            </td>
        </tr>
        <tr>
            <td class="style4">
                &nbsp;</td>
            <td>
                &nbsp;</td>
        </tr>
    </table>

    </asp:Panel>
</asp:Content>
<asp:Content ID="Content2" runat="server" contentplaceholderid="head">
    <style type="text/css">
        .style3
        {
            width: 26%;
            background-color: #FF9933;
        }
        .style4
        {
            width: 88px;
        }
    </style>
</asp:Content>
.........................end source..........................

..............................code behind..................................
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.Collections;
using System.Text;
using System.Data.SqlClient;
public partial class product : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString());
    
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Panel1.Visible = false;
            Panel2.Visible = false;
            bind_dropdown();
        }
    }

    SqlDataAdapter da;
    protected void bind_dropdown()
    {
        da = new SqlDataAdapter("select * from prod", con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        DropDownList1.DataSource = dt;
        DropDownList1.DataTextField = "p_name";
        DropDownList1.DataValueField = "p_id";
        DropDownList1.DataBind();
        DropDownList1.Items.Insert(0, new ListItem("--select--", "0"));
    
        
      
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        insert_product();
        TextBox1.Text = "";
        Image2.ImageUrl= "";
        

    }

    protected void insert_product()
    {
        try
        {
            con.Open();
            string name = FileUpload1.FileName;
            string path = Server.MapPath("");
            string str = path + "//" + "productimage" + "//" + name;
            FileUpload1.PostedFile.SaveAs(str);
            string s = "~" + "//" + "productimage" + "//" + name;
            string insert = "insert into prod values('" + TextBox1.Text + "','" + s + "')";
            SqlCommand cmd = new SqlCommand(insert, con);

            cmd.ExecuteNonQuery();
            Image1.ImageUrl = s.ToString();
            con.Close();
            Response.Write("Insert successfully!");
        }
        catch(Exception ex)
        {

        }
        

    }
    protected void Button2_Click(object sender, EventArgs e)
    {
      try
        {
            edit();
            TextBox2.Text = "";
            bind_dropdown();
          
        }
        catch(Exception ex)
      {

        }
    }

    protected void deleteOldImage()
    {
        string str = "delete from prod where p_name='" + DropDownList1.SelectedValue + "'"; ;
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
    }
    SqlCommand cmd;
    protected void edit()
    {
        deleteimgfiel();
        int id3=id1(DropDownList1.SelectedItem.Text);
        con.Open();
        string name = FileUpload2.FileName;
        string path = Server.MapPath("");
        string str = path + "//" + "productimage" + "//" + name;
        FileUpload2.PostedFile.SaveAs(str);
        string s = "~" + "//" + "productimage" + "//" + name;
       
        string str1 = "update prod set p_name='"+TextBox2.Text+"' ,p_path ='"+s+"' where p_id="+id3+"";
        cmd = new SqlCommand(str1,con);
        
        cmd.ExecuteNonQuery();
        con.Close();

       
    }
    int id2;
    protected int id1(string name)
    {
        
        string str="select p_id from prod where p_name='"+name+"'";
        con.Open();
        cmd=new SqlCommand(str,con);
        SqlDataReader dr=cmd.ExecuteReader();
        
        if(dr.Read())
        {
            id2=Convert.ToInt32(dr[0]);
        }
        con.Close();
        return id2;

    }
    protected void deleteimgfiel()
    { 
    string str="select p_path from prod where p_name='"+DropDownList1.SelectedItem+"'";
        cmd=new SqlCommand(str,con);
        con.Open();
         SqlDataReader dr=cmd.ExecuteReader();
         
        if(dr.Read())
        {
            string path1=Server.MapPath(dr[0].ToString());
            if(System.IO.File.Exists(path1))
            {
                System.IO.File.Delete(path1);
            }
        }

        con.Close();
    
    }
    protected void Button4_Click(object sender, EventArgs e)
    {
        Panel1.Visible = true;
        Panel2.Visible = false;
    }
    protected void Button5_Click(object sender, EventArgs e)
    {
        Panel1.Visible = false;
        Panel2.Visible = true;
        bind_dropdown();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {

        string str = bindImage();
        Image2.ImageUrl = str;
       
    }

    protected string bindImage()
    {
        con.Open();
        string image = "";
        string str = "select p_path from prod where p_id='"+DropDownList1.SelectedValue+"'";
        SqlCommand cmd;
        cmd = new SqlCommand(str,con);
        SqlDataReader dr = cmd.ExecuteReader();
        if (dr.Read())
        {
            image=dr[0].ToString();
        }
        con.Close();
        return image;
       
    }
    protected void Button3_Click(object sender, EventArgs e)
    {
        deleteimgfiel();
        int id3 = id1(DropDownList1.SelectedItem.Text);
        string str="delete from prod where p_id="+id3+"";
        cmd = new SqlCommand(str, con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        bind_dropdown();
    }

    
}

..............................end code behind..................................

Friday, 21 December 2012

How to send mail in C#?

.........................login soure...........

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="login.aspx.cs" Inherits="login" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FFCCFF;
        }
        .style2
        {
            width: 107px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td class="style2">
                    LoginId</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                        Height="16px" Width="158px">
                        <asp:ListItem>Admin</asp:ListItem>
                        <asp:ListItem>User</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Username</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server" Height="16px" Width="155px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server" Height="16px" Width="155px"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Login" />
                </td>
            </tr>
        </table>
   
    </div>
    </form>
</body>
</html>
..........................................end login source code.................
..................code behind.........................
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;

public partial class login : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    DataSet ds = new DataSet();
    SqlDataAdapter da;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {

            if (DropDownList1.SelectedValue == "User")
            {
                con.Open();
                Session["user"] = "1";
                cmd = new SqlCommand("select*from textinsert where name='" + TextBox2.Text + "' and pwd='" + TextBox3.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Session["name"]=TextBox2.Text;
                    Session["pwd"]=TextBox3.Text;
                    Response.Redirect("registration.aspx?name="+Session["name"]+" and pwd="+ Session["pwd"]+"");
                }

            }
            if (DropDownList1.SelectedValue == "Admin")
            {
                Session["user"] = "2";
                con.Open();
                cmd = new SqlCommand("select*from textinsert where name='" + TextBox2.Text + "' and pwd='" + TextBox3.Text + "'", con);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Response.Redirect("registration.aspx");
                }
            }
        }
        catch(Exception ex)
        {
        }
    }

    }


    
....................................registration source code......................
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="registration.aspx.cs" Inherits="registration" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .style1
        {
            width: 100%;
            background-color: #FF9999;
        }
        .style2
        {
            width: 109px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="style1">
            <tr>
                <td class="style2">
                    Name</td>
                <td>
                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Password</td>
                <td>
                    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    Address</td>
                <td>
                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td class="style2">
                    &nbsp;</td>
                <td>
                    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
                </td>
            </tr>
        </table>
    
    </div>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        &nbsp;</p>
    <p>
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </p>
    </form>
</body>
</html>
..........................code behind..........
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.Text;
using System.Data.SqlClient;
using System.IO;
using System.Net.Mail;
public partial class registration : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ToString());
    SqlCommand cmd;
    SqlDataReader dr;
    SqlDataAdapter da;
    DataSet ds = new DataSet();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {   
            string str=Session["user"].ToString();

            if (str=="1")
            {
                user();
            }
            if(str=="2")
            {
                adminbind();
            }
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        cmd = new SqlCommand("insert into textinsert values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "')", con);
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        //Response.Write("successfully submitted!");
        cmd = new SqlCommand("select count(id) as id from textinsert", con);
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.Fill(dt);
        if(dt.Rows.Count>0)
        {
            
            Int64 count=(Convert.ToInt64(dt.Rows[0]["id"].ToString()));
            string message;

            if (count > 4)
            {
                if (count == 4)
                {
                    message="you got first step";
                    send_email(message);
                }
                if (count == 16)
                {
                    message="you got second step";
                    send_email(message);
                }

                if (count == 50)
                {
                    message = "you third first step";
                    send_email(message);
                }
            }

        }
      
    }

    public void send_email(string msg)
    {
        MailMessage ms = new MailMessage("kulngr21@gmail.com", "kulngr@hotmail.com");
        SmtpClient ab = new SmtpClient();

        ms.Body = msg;
        ms.IsBodyHtml = true;
        ab.EnableSsl = true;
        ab.Send(ms);

        Response.Write("mmail sent...");
    }
     
    protected void adminbind()
    {
        con.Open();
        // string name=Request.QueryString["name"].ToString();
        // string pwd=Request.QueryString["pwd"].ToString();
        string str = "select * from textInsert ";
        da = new SqlDataAdapter(str, con);
        da.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    SqlDataAdapter da1;
    protected void user()
    {
        try
        {
            con.Open();
            string name = Session["name"].ToString();
            string str = "select*from textinsert where name='" + name + "'";
            da1 = new SqlDataAdapter(str,con);
            DataTable dt = new DataTable();
            da1.Fill(dt);
          
          
                GridView1.DataSource = dt;
                GridView1.DataBind();
          
            con.Close();
        }
        catch(Exception ex)
        {

        }
    }
}
.........................................end code behind.....................

Sunday, 16 December 2012

How to create references through foreign key among three tables in SQL Server ?

....................................First Example.............

CREATE TABLE t_Analog(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Value] [float] NOT NULL,
    CONSTRAINT [uc_t_Analog] UNIQUE(AppName, ItemName)
)

CREATE TABLE t_Discrete(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Value] [bit] NOT NULL,
    CONSTRAINT [uc_t_Discrete] UNIQUE(AppName, ItemName)
)

CREATE TABLE t_Message(
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    [Value] [nvarchar](256) NOT NULL,
    CONSTRAINT [uc_t_Message] UNIQUE(AppName, ItemName)
)
...................Second Example............
CREATE TABLE t_AllItems(
    [id] [bigint] IDENTITY(1,1) NOT NULL,
    [itemType] [int] NOT NULL,
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    CONSTRAINT [pk_t_AllItems] PRIMARY KEY CLUSTERED ( [id] )
    CONSTRAINT [uc_t_AllItems] UNIQUE([id], [AppName], [ItemName])
) ON [PRIMARY]

CREATE TABLE t_Analog(
    [itemId] [bigint] NOT NULL,
    [Value] [float] NOT NULL,
    FOREIGN KEY (itemId) REFERENCES t_AllItems(id)
)

CREATE TABLE t_Discrete(
    [itemId] [bigint] NOT NULL,
    [Value] [bit] NOT NULL,
    FOREIGN KEY (itemId) REFERENCES t_AllItems(id)
)

CREATE TABLE t_Message(
    [itemId] [bigint] NOT NULL,
    [Value] [nvarchar](256) NOT NULL,
    FOREIGN KEY (itemId) REFERENCES t_AllItems(id)
)
..........Third.......................
CREATE TABLE AllItems(
    [id] [int] IDENTITY(1,1) NOT NULL,
    [itemType] [int] NOT NULL,
    [AppName] [nvarchar](20) NOT NULL,
    [ItemName] [nvarchar](32) NOT NULL,
    CONSTRAINT [pk_AllItems] PRIMARY KEY CLUSTERED ( [id] ASC )
) ON [PRIMARY]

CREATE TABLE Analog(
    [itemId] [int] NOT NULL,
    [Value] [float] NOT NULL
)

CREATE TABLE Discrete(
    [itemId] [int] NOT NULL,
    [Value] [bit] NOT NULL
)

CREATE TABLE Message(
    [itemId] [bigint] NOT NULL,
    [Value] [nvarchar](256) NOT NULL
)

ALTER TABLE [Analog] WITH CHECK ADD CONSTRAINT [FK_Analog_AllItems] FOREIGN KEY([itemId])
REFERENCES [AllItems] ([id])
GO
ALTER TABLE [Analog] CHECK CONSTRAINT [FK_Analog_AllItems]
GO

ALTER TABLE [Discrete] WITH CHECK ADD CONSTRAINT [FK_Discrete_AllItems] FOREIGN KEY([itemId])
REFERENCES [AllItems] ([id])
GO
ALTER TABLE [Discrete] CHECK CONSTRAINT [FK_Discrete_AllItems]
GO

ALTER TABLE [Message] WITH CHECK ADD CONSTRAINT [FK_Message_AllItems] FOREIGN KEY([itemId])
REFERENCES [AllItems] ([id])
GO
ALTER TABLE [Message] CHECK CONSTRAINT [FK_Message_AllItems]
GO
..............Fourth.....

CREATE TABLE t_Generic(
[AppName] [nvarchar](20) NOT NULL,
[ItemName] [nvarchar](32) NOT NULL,
[Type] [nvarchar](32) NOT NULL,
[AnalogValue] [Float] NULL,
[DiscreteValue] [bit] NULL,
[MessageValue] [nvarchar](256) NULL,
CONSTRAINT [uc_t_Generic] UNIQUE(AppName, ItemName)
)

Popular

Blog Archive

Total Pageviews

Archive