Search This Blog

Friday, 5 February 2016

Drop down selected text in javascript

var dropDownList = document.getElementById("DropDownList1");

var selectedText = dropDownList.options[dropDownList.selectedIndex].text;

Step 1:- JavaScript function

        function GetSelectedValueText() {
            var ddl = document.getElementById("DropDownList1");

            var Value = ddl.value;

            var Text = ddl.options[ddl.selectedIndex].text;

            alert("Value " + Value + "\nText " + Text);
        }

Step 2:- Complete Code

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="Demo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>drop down selected text in javascript</title>
    <script type="text/javascript">
        function GetSelectedValueText() {
            var ddl = document.getElementById("DropDownList1");

            var Value = ddl.value;

            var Text = ddl.options[ddl.selectedIndex].text;

            alert("Value " + Value + "\nText " + Text);
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <asp:DropDownList ID="DropDownList1" runat="server" ClientIDMode="Static">
                <asp:ListItem Selected="True" Value="0">--Select--</asp:ListItem>
                <asp:ListItem Value="1">India</asp:ListItem>
                <asp:ListItem Value="2">USA</asp:ListItem>
                <asp:ListItem Value="3">UK</asp:ListItem>
                <asp:ListItem Value="4">London</asp:ListItem>
            </asp:DropDownList>
            <br />
            <br />
            <br />
            <br />
            <asp:Button ID="Button1" runat="server" Text="Get Text Value" OnClientClick="GetSelectedValueText()" />
        </div>
    </form>
</body>
</html>

Step 3:- Output




Note:

Select text from dropdownlist and click on button


Cannot serialize the DataTable. DataTable name is not set.

Step1:- Create BindData() method

protected void BindData()
        {
            DataTable dt = new DataTable();

            DataRow dr;

            dt.Columns.Add(new DataColumn("Column1"typeof(string)));

            dt.Columns.Add(new DataColumn("Column2"typeof(string)));

            dt.Columns.Add(new DataColumn("Column3"typeof(string)));

            dt.Columns.Add(new DataColumn("Column4"typeof(string)));

            int totalRow = 10;

            for (int i = 0; i < totalRow; i++)
            {
                dr = dt.NewRow();

                dr["Column1"] = "Row" + i;

                dr["Column2"] = "Row" + i;

                dr["Column3"] = "Row" + i;

                dr["Column4"] = 1000 / (i + 1);

                dt.Rows.Add(dr);
            }

            string strXML = ConvertDataTableToXML(dt);
        }

Step 2:- Create ConvertDataTableToXML() method

public string ConvertDataTableToXML(DataTable dt)
        {
            MemoryStream ms = new MemoryStream();
            dt.WriteXml(ms, true);
            ms.Seek(0, SeekOrigin.Begin);
            StreamReader sr = new StreamReader(ms);
            string xml = sr.ReadToEnd();
            sr.Close();
            return (xml);
        }

Step3:- Complete Code

using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Demo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindData();
            }
        }

        protected void BindData()
        {
            DataTable dt = new DataTable();

            DataRow dr;

            dt.Columns.Add(new DataColumn("Column1"typeof(string)));

            dt.Columns.Add(new DataColumn("Column2"typeof(string)));

            dt.Columns.Add(new DataColumn("Column3"typeof(string)));

            dt.Columns.Add(new DataColumn("Column4"typeof(string)));

            int totalRow = 10;

            for (int i = 0; i < totalRow; i++)
            {
                dr = dt.NewRow();

                dr["Column1"] = "Row" + i;

                dr["Column2"] = "Row" + i;

                dr["Column3"] = "Row" + i;

                dr["Column4"] = 1000 / (i + 1);

                dt.Rows.Add(dr);
            }

            string strXML = ConvertDataTableToXML(dt);
        }

        public string ConvertDataTableToXML(DataTable dt)
        {
            MemoryStream ms = new MemoryStream();
            dt.WriteXml(ms, true);
            ms.Seek(0, SeekOrigin.Begin);
            StreamReader sr = new StreamReader(ms);
            string xml = sr.ReadToEnd();
            sr.Close();
            return (xml);
        }
    }
}

Step4:- Put debugger on ConvertDataTableToXML method and Run the code

When you run the code, you will get the following error:-





Error states that while creating the DataTable object, you are not given the DataTable name.

See heighted code.

So, you need to assign a table name to DataTable.

Before
DataTable dt = new DataTable();

After
DataTable dt = new DataTable("Table1");

or 

DataTable dt = new DataTable();
dt.TableName = "Table1";


Tuesday, 2 February 2016

Number of referencing columns in foreign key differs from number of referenced columns, table

Msg 8139, Level 16, State 0, Line 26
Number of referencing columns in foreign key differs from number of referenced columns, table 'Table2'.


Let’s explore it.


Step 1:- Create Table1

CREATE TABLE Table1
(
       Id int identity,
       Name varchar(50)
)

Step 2:- Create Table2

CREATE TABLE Table2
(
       Id int,
       Name varchar(50)
)

Step 3:- Add Primary Key on Table1, column Id

ALTER TABLE Table1 ADD CONSTRAINT PK_Id PRIMARY KEY(Id)

Step 4:- Add Foreign Key on Table2, column Id i.e. references to Table1 column Id

ALTER TABLE Table2 ADD CONSTRAINT FK_Id FOREIGN KEY REFERENCES Table2(Id)

If you get the following error

Msg 8139, Level 16, State 0, Line 26
Number of referencing columns in foreign key differs from number of referenced columns, table 'Table2'.

Change the query and specified the column name for foreign key constraints

ALTER TABLE Table2 ADD CONSTRAINT FK_Id FOREIGN KEY(Id) REFERENCES Table1(Id)


Here, Id is Table2 table column.


NOTE:

DROP the both table CONSTRAINT

ALTER TABLE TABLE2 DROP CONSTRAINT FK_Id
ALTER TABLE TABLE1 DROP CONSTRAINT PK_Id

Run the below query

ALTER TABLE Table2 ADD CONSTRAINT FK_Id FOREIGN KEY(Id) REFERENCES Table1(Id)

If you run the above query, you will get the following error

Msg 1776, Level 16, State 0, Line 46
There are no primary or candidate keys in the referenced table 'Table1' that match the referencing column list in the foreign key 'FK_Id'.
Msg 1750, Level 16, State 0, Line 46
Could not create constraint or index. See previous errors.


Error state that you are trying to create foreign key on table2 table but referenced table 'Table1' does not have primary key or candidate key on column Id

Drop table Table2, Table1


FOREIGN KEY constraint does not have to be linked only to a PRIMARY KEY constraint in another table, it can also be defined to reference the columns of a UNIQUE constraint in another table.