954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

bata binding

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

patel28rajendra
Newbie Poster
9 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 

What DBMS?

Netcode
Veteran Poster
1,021 posts since Jun 2009
Reputation Points: 43
Solved Threads: 67
 

microsoft sql server 2005

patel28rajendra
Newbie Poster
9 posts since Sep 2011
Reputation Points: 10
Solved Threads: 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

Netcode
Veteran Poster
1,021 posts since Jun 2009
Reputation Points: 43
Solved Threads: 67
 

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();

            }

        }
    }
patel28rajendra
Newbie Poster
9 posts since Sep 2011
Reputation Points: 10
Solved Threads: 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();

            }

        }
    }
}
patel28rajendra
Newbie Poster
9 posts since Sep 2011
Reputation Points: 10
Solved Threads: 0
 
I want to fill data in drop down list from database table. plz give me code for this


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

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

jinal161188
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: