3
Contributors
6
Replies
7
Views
5 Years
Discussion Span
Last Post by jinal161188
0

Its quite easy but you would agree with me that you would learn faster and better if you try it out yourself. Try something out and if you encounter any problems, just post your codes and whatever error message

0

Error:

Incorrect syntax near the keyword 'table'.

MY code is:

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            

            ddl.AppendDataBoundItems = true;

            String strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            String strQuery = "SELECT name FROM table";

            SqlConnection con = new SqlConnection(strConnString);

            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.Text;

            cmd.CommandText = strQuery;

            cmd.Connection = con;

            try
            {

                con.Open();

                ddl.DataSource = cmd.ExecuteReader();

                ddl.DataTextField = "ContactName";

                ddl.DataValueField = "CustomerID";

                ddl.DataBind();

            }

           

            finally
            {

                con.Close();

                con.Dispose();

            }

        }
    }

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

Finally i got the answer
my code is :

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Data.Sql;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            

            ddl.AppendDataBoundItems = true;

            String strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;

            String strQuery = "SELECT name FROM customer";

            SqlConnection con = new SqlConnection(strConnString);

            SqlCommand cmd = new SqlCommand();

            cmd.CommandType = CommandType.Text;

            cmd.CommandText = strQuery;

            cmd.Connection = con;

            try
            {

                con.Open();

                ddl.DataSource = cmd.ExecuteReader();

                ddl.DataTextField = "name";

                ddl.DataValueField = "name";

                ddl.DataBind();

            }

           

            finally
            {

                con.Close();

                con.Dispose();

            }

        }
    }
}

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

0

I want to fill data in drop down list from database table.
plz give me code for this

----------------------------------------------------


<asp:DropDownList ID="dropdepartment" runat="server"
DataSourceID="SqlDataSource2" DataTextField="Department"
DataValueField="Department_id">
</asp:DropDownList>

or you can also use the dataset or querystring....

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.