Sql database is StudentInfo and Table name is Registration
ID----------Name---------------Email---------------------------PhoneNo
1 Munasunghe amilamunasinghe@yahoo.com 0717069425
2 Liyanarachchi hareshliya6@gmail.com 0756706352
protected void Page_Load(object sender, EventArgs e)
{
string query = "select ID, Name, Email, PhoneNo from Registration";
SqlCommand cmd1 = new SqlCommand(query);
DataTable dt1 = GetData(cmd1);
int rowcount = dt1.Rows.Count;
/* I want to read data in each row step by step and assign to variables*/
}
The function GetData is used to get data from the Database.
private DataTable GetData(SqlCommand cmd)
{
DataTable dt = new DataTable();
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
SqlDataAdapter sda = new SqlDataAdapter();
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
sda.SelectCommand = cmd;
sda.Fill(dt);
return dt;
}
catch
{
return null;
}
finally
{
con.Close();
sda.Dispose();
con.Dispose();
}
}
ID is Primarykey.
Results should be like(Name,Email,Phone No are variables and 1,2,... are ID value)
Name[1]=Munasunghe
Name[2]=Liyanarachchi
Email[1]=amilamunasinghe@yahoo.com
Email[2]=hareshliya6@gmail.com
Phone No[1]=0717069425
Phone No[2]=0756706352