Thursday, 7 April 2016

How to bind Dropdownlist in Gridview Rowdatabound Using Asp.Net C#

Bind Dropdownlist in Gridview Rowdatabound

Display Some Details in Gridview but we need list of values show to the user on gridview we bind the inside of gridview Dropdownlist using RoeDataBound event method Asp.Net C#.

DEMO


                                   Download 


HTML CODING

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

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <table>
        <tr><td>&nbsp;</td><td>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing">
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:TemplateField HeaderText="User Id">
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("UserId"%>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:TemplateField HeaderText="User Name">
                        <EditItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:DropDownList ID="DropDownList1" runat="server">
                            </asp:DropDownList>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <HeaderStyle ForeColor="#CC3300" />
            </asp:GridView>
            </td></tr>
    </table>
    </div>
    </form>
</body>
</html>



C# CODING


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;

public partial class Mysql_StoredProcedure : System.Web.UI.Page
{
    protected void GridBind()
    {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbc"].ToString());
        con.Open();
        MySqlCommand cmd = new MySqlCommand("select * from userinformation", con);

        MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        con.Close();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        GridBind();

    }
   
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            // GridViewRow gr = (GridViewRow)FindControl("GridView1");
            DropDownList ddl = (DropDownList)e.Row.FindControl("DropDownList1");
            MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbc"].ToString());
            con.Open();
            MySqlCommand cmd1 = new MySqlCommand("select UserName from userinformation", con);
            MySqlDataReader rd1 = cmd1.ExecuteReader();
            ddl.Items.Clear();
            ddl.Items.Add("Select Username");
            while (rd1.Read())
            {
                ddl.Items.Add(rd1[0].ToString());
            }
            con.Close();

            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {

                // GridViewRow gr = (GridViewRow)FindControl("GridView1");
                DropDownList ddll = (DropDownList)e.Row.FindControl("DropDownList1");
                MySqlConnection con1 = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbc"].ToString());
                con1.Open();
                MySqlCommand cmd = new MySqlCommand("select UserName from userinformation", con1);
                MySqlDataReader rd = cmd.ExecuteReader();
                ddl.Items.Clear();
                ddl.Items.Add("Select Username");
                while (rd.Read())
                {
                    ddll.Items.Add(rd[0].ToString());
                }
                con1.Close();

            }
        }
    }
  
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GridBind();
    }

}













































Gmail Error The SMTP server requires a secure connection or the client was not authenticated

No comments    
categories: , ,
The SMTP server requires a secure connection or the client was not authenticated

Got this  errors  means that Gmail has blocked your application . but username and password is correct you will do below like this 


Go to below link

https://www.google.com/settings/security/lesssecureapps


Turn on - the less secure apps

Access for less secure apps






Add a Confirm delete option in ASP.Net


Display multiple details in Gridview we want to delete some row before confirmation messahe open confirm delete that is onclientclick event Using Asp.Net C#.

DEMO


HTML CODING 

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div align="center">
    <table
        <tr><td>&nbsp;</td><td>
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" OnRowDeleting="GridView1_RowDeleting">
                <Columns>
                    <asp:TemplateField HeaderText="Action" ShowHeader="False">
                        <ItemTemplate>

           <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Delete"
              
                OnClientClick="return confirm('Are You Sure Want to you Delete?');" Text="Delete">

           </asp:LinkButton>

                        </ItemTemplate>
                    </asp:TemplateField>
                    <asp:BoundField DataField="UserId" HeaderText="User Id" />
                    <asp:BoundField DataField="UserName" HeaderText="User Name" />
                    <asp:BoundField DataField="Location" HeaderText="Location" />
                    <asp:BoundField DataField="fromdate" HeaderText="FromDates" />
                </Columns>
                <HeaderStyle ForeColor="#CC3300" />
            </asp:GridView>
            </td></tr>
    </table>
    </div>
    </form>
</body>
</html>



 C# CODING


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;
using System.Data;

public partial class Mysql_StoredProcedure : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["dbc"].ToString());
        con.Open();
        MySqlCommand cmd = new MySqlCommand("select * from userinformation", con);

        MySqlDataAdapter adp = new MySqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        adp.Fill(ds);
        if (ds.Tables[0].Rows.Count > 0)
        {
            GridView1.DataSource = ds;
            GridView1.DataBind();
        }
        con.Close();

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
     
        // Write Delete condition

        Response.Write("<script>alert('Deleted Successfully')</script>");

    }
}