Thursday, May 19, 2016

Show or Hide div on checkbox click JavaScript

In this article, I am going to explain how to div show and hide on  checkbox click in ASP. Net  using JavaScript

Suppose You have a checkbox with the name of "chkselect" and you want to show or hide div on checkbox click , Here aspx code which provide you such facility


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!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>
    <script type="text/javascript">
        function showhide() {
            var select = document.getElementById("chkselect");
            if (select.checked) {
                document.getElementById("showhide").setAttribute("style", "display:inline");
            }
            else {
                document.getElementById("showhide").setAttribute("style", "display:none");
            }
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <fieldset>
    <legend>Show and Hide Div</legend>
    <asp:CheckBox ID="chkselect" runat="server" onchange="return showhide();"/>
    </fieldset>
    <div id="showhide" runat="server" style="display:none;">
    <span style="color:Red">Welcome at http://www.dotnetdarpan.blogspot.com/</span>
    </div>
    </form>
</body>
</html>


Output

Wednesday, May 18, 2016

Code for forgot password using ADO.Net and C#


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication21
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox1.Items.Add("nickname");
        }
        private void Form2_Load(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from login1", "data source=MKS-PC\\SQLEXPRESS;Initial Catalog=master;integrated security=true");
            ds = new DataSet();
            da.Fill(ds);
        }
        private void button1_Click(object sender, EventArgs e)
        {
            bool b = true;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr[0].ToString().Trim() == textBox1.Text && dr[3].ToString().Trim() == textBox2.Text)
                {
                    MessageBox.Show(dr[1].ToString());
                    b = false;
                }
            }
            if (b)
            {
                MessageBox.Show("Wrong uid or ans");
            }
        }
    }
}

Output
 Click on "forgetpassword" button a new window will be open now fill "uid","select ques", "ans" Information and click on submit button


Tuesday, May 17, 2016

Print DIV content using JavaScript in asp.net

In this article I am going to explain how to print div content in ASP. net c#

Code of aspx page to print div

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="PrintDiv.aspx.cs" Inherits="PrintDiv" %>
 
<!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>
</head>
<body>
    <form id="form1" runat="server">
    <div id="PrintDiv">
    <table>
    <tr>
    <td style="padding:10px; color:#FF99CC; font-size:14px; font-weight:bold;">Hello ! Welcome at www.dotnetDarpan.blogspot.com</td>
    </tr>
     <tr>
    <td>Note: For any query mail us on  <b>gupta.sharad2010@gmail.com</b></td>
    </tr>
    </table>
    </div>
 
    <asp:Button ID="btnPrint" Text="Print" runat="server"  OnClientClick="PrintDiv();"/>
    </form>
 
    <script type="text/javascript">
        function PrintDiv() {
            var PrintDiv = document.getElementById("PrintDiv").innerHTML;
            var printWindow = window.open('', '', 'height=200,width=400');
            printWindow.document.write('<html><head><title>DIV Print DEMO</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(PrintDiv);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        }
</script>
</body>
</html>

Function to print div

<script type="text/javascript">
        function PrintDiv() {
            var PrintDiv = document.getElementById("PrintDiv").innerHTML;
            var printWindow = window.open('', '', 'height=200,width=400');
            printWindow.document.write('<html><head><title>DIV Print DEMO</title>');
            printWindow.document.write('</head><body >');
            printWindow.document.write(PrintDiv);
            printWindow.document.write('</body></html>');
            printWindow.document.close();
            printWindow.print();
        }
</script>

Output


Monday, May 16, 2016

AcceptChanges and Rejectchanges

Acceptchanges –
 
We can accept the changes by using AcceptChanges method of the DataRow, Datatable and dataset , this method set the current row value to be original value and also acceptchanges method accept all the changes for entire datatable.

Syntax of the Acceptchanges
ds.Tables[0].Rows[0].AcceptChanges();

RejectChanges – 
By using the Rejectchanges method to the datarow the old value become discard .

Syntax of the Rejectchanges

ds.Tables[0].Rows[0].Rejectchanges();

Friday, May 13, 2016

CommandBuilder in ADO.Net

The CommandBuilder use to generate update, Delete, insert commands on a single database table for data adapter, in other words it is used to permanently save the record in the database.

The Example of the CommandBuilder is given below: 

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace WindowsFormsApplication20
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
        private void button1_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from emp", "data source=MKS-PC\\SQLEXPRESS;Initial Catalog=master;integrated security=true");
            ds = new DataSet();
            da.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            DataRow dr = ds.Tables[0].NewRow();
            dr[0] = textBox1.Text;
            dr[1] = textBox2.Text;
            dr[2] = textBox3.Text;
            ds.Tables[0].Rows.Add(dr);
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            SqlCommandBuilder cmd = new SqlCommandBuilder(da);
            da.Update(ds.Tables[0]);
            MessageBox.Show("save");
        }
    }
}
Output  

Thursday, May 12, 2016

How to create table, Update and Delete rows in Data Grid view

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
 
namespace WindowsFormsApplication19
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        DataTable dt = new DataTable();
        private void button1_Click(object sender, EventArgs e)
        {
            DataColumn dc1 = new DataColumn("Id", typeof(int));
            DataColumn dc2 = new DataColumn("Name", typeof(string));
            DataColumn dc3 = new DataColumn("Salary", typeof(int));
            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            DataRow dr = dt.NewRow();
            dr[0] = 1;
            dr[1] = "Sachin";
            dr[2] = 2000;
            dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;
        }
 
        private void button2_Click(object sender, EventArgs e)
        {
            dt.Rows[0].Delete();
        }
 
        private void button3_Click(object sender, EventArgs e)
        {
            dt.Rows[0][0] = 2;
        }
    }
}

Output : 

click on show button: 
 click on update button :
click on delete button :

Wednesday, May 11, 2016

C# login page using ADO.Net

Firstly create table in the database by name login 

C# code :-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
 
namespace WindowsFormsApplication16
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        SqlDataAdapter da;
        DataSet ds;
 
 
        private void Form1_Load(object sender, EventArgs e)
        {
 
 
        }
        private void button1_Click(object sender, EventArgs e)
        {
            da = new SqlDataAdapter("select * from login", "data source=MKS-PC\\SQLEXPRESS;Initial Catalog=master;integrated security=true");
            ds = new DataSet();
            da.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                if (dr[0].ToString().Trim() == textBox1.Text && dr[1].ToString().Trim() == textBox2.Text)
                {
                    Form2 f = new Form2();
                    f.Show();
 
                    break;
 
                }
 
                else
                {
                    MessageBox.Show("Invalid");
                }
 
            }
        }
    }
}

Output :