Tuesday, August 18, 2009

Binding Data to Dropdownlist from Database

Binding Data to Dropdownlist from DataBase
Today Iam going to write on Binding Data to Dropdownlist from Database

Consider that we want to populate the Dropdownlist with a list of countries. For this first we need to create a new .aspx page with the following code

<asp:dropdownlist id =" “dpdcountry”" runat="”server”">

Now create a new classlibrary with the class name DataBinding.cs and write the following code

public class Databinding
public static DataTable Execute(string sqlstring)
SqlConnection con = new SqlConnection(connectionstring);
DataTable dt = new DataTable(“tb1”);
SqlCommand cmd = new SqlCommand(sqlstring, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt) ;
catch(Exception e)
throw e;

return dt;

In the above code Databinding is the class name, Execute is the method which returns a datatable. In the connectionstring you need to specify the 4 parameters of the SqlConnection string.

Now in the .aspx.cs file under Page_Load method you need to write the following the code

string str = “select country_id , name from Country”;
Datatable dt = Databinding.Execute(str);
dpdcountry.DataValueField = dt.columns[0].ToString();
dpacountry.DataTextField = dt.cloumns[1].ToString();
dpdcountry.DataSource = dt;

On a whole, first we declared a dropdownlist with the name dpdcountry in the .aspx page. Then in .aspx.cs page, in the Page_Load method we are sending the sql string to the class Databinding which connects to the database and returns the data table containg the country names. Then this data table is assigned to the dropdownlist as its datasource. So, when you run this code you get a page with the dropdownlist populated with country names.
1. You should add the Class Library name in the Referrences of the Project
2. You should use the Class Library name in the directories part of the .aspx.cs page

Thursday, August 6, 2009

C# InterviewQuestions-Part 10

Interview Questions
91. How many types of errors are there?
a ) 2 types
1. Run time errors
2. Compile time errors

92. Give some examples of compile time and runtime errors?
a ) Compile time:
missing semocolons
use of undeclared variables
bad references to objects
dividing an integer by zero
using negative size for an array
passing a parameter that is not in a valid range

93. What is an exception?
a ) An exception is a condition that occurs by a run time error in the program

94. What is the base class used for all the exceptions in c#?
a ) It is Exception

95. Why do we use throw keyword?
a ) Throw keyword is used to throw our own exceptions.

96. How many exceptions we can write in throws clause?
a ) We can use multiple exceptions.

97. Can a finally block exists with try block but without a catch block?
a ) Yes

98. Can we have nested try blocks in c#?
a ) yes

99. What is the difference between Write() and WriteLine() methods?
a ) Write() method outputs one or more values to the screen without a newline character
WriteLine() method outputs one or more values to the screen with a newline character

100. What is the difference between Read() and Readline() methods?
a ) Read() method returns a single character as int
ReadLine() method returns a string containing a line of text