I am trying to pick data from the database through data access layer and use those values in different text boxes on the page.Basically on a click on the form, modal popup is planned to read the database and put the values into different text boxes which
can be latter changed/ updated.
What I am unable to do is accessing those values on the form itself progamatically. I will be thankful for any ideas and help.
My data access component is as follows:
cmd.Parameters.Add(
cmd.Parameters[
new
SqlParameter ("@reg_id",
SqlDbType.Int ));"@reg_id"].Value=reg_id;try
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);//
Code also to cater for NULL Values
ermm.. are you trying to reference a value from Gridview or from the database? If you are trying to get the values from the database then you can create a method that returns a DataTable, then from the DataTable you can iterate to each rows to get the individual
row values.
Here's a quick example:
public DataTable GetData(string ID)
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection("Your Connection String");
string sqlStatement = "SELECT TypeID FROM ProjectTypes WHERE ColumnID = @ID";
try
{
conn.Open();
SqlCommand sqlCmd = new SqlCommand(sqlStatement, conn);
sqlCmd.Parameters.AddWithValue("@ID",ID);
SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
sqlDa.Fill(dt);
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Fetch Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
return dt;
}
"Cannot implictly convert Type "class name" to system.data.datatable"
As I've mentioned, you have to create a method that returns a DataTable so that you can iterate each rows stored in the DataTable and populate your TextBox/Label control accordingly.
hwraja
Member
10 Points
11 Posts
Reading values from data access component individually
Aug 02, 2010 08:23 AM|LINK
Hi,
I am trying to pick data from the database through data access layer and use those values in different text boxes on the page.Basically on a click on the form, modal popup is planned to read the database and put the values into different text boxes which can be latter changed/ updated.
What I am unable to do is accessing those values on the form itself progamatically. I will be thankful for any ideas and help.
My data access component is as follows:
cmd.Parameters.Add(
cmd.Parameters[
new SqlParameter ("@reg_id", SqlDbType.Int ));"@reg_id"].Value=reg_id;try{
con.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.SingleRow);// Code also to cater for NULL Valuesreader.Read();
Access_Prog
emp = new Access_Prog(
reader["prog_code"] == DBNull.Value ? string.Empty : Convert.ToString(reader["prog_code"]),
reader["prog_startdate"] == DBNull.Value ? string.Empty : Convert.ToString(reader["prog_startdate"]),
reader["ia_date"] == DBNull.Value ? string.Empty : Convert.ToString(reader["ia_date"]),
reader["prog_status"] == DBNull.Value ? string.Empty : Convert.ToString(reader["prog_status"]),
reader["case_worker"] == DBNull.Value ? string.Empty : Convert.ToString(reader["case_worker"]),
reader["enddate"] == DBNull.Value ? string.Empty : Convert.ToString(reader["enddate"]),
reader["closure_reason"] == DBNull.Value ? string.Empty : Convert.ToString(reader["closure_reason"]),
(bool)reader["finished"],
(int)reader["client_id"]
);
reader.Close();
returnemp;
}
finally
{
con.Close();
}
How do i read these values individually and put them in different boxes!? What I dont want to do is to access these from page's .cs file.
cheers
h
ADO.NET data access c# asp.net
vinz
All-Star
128414 Points
18131 Posts
MVP
Re: Reading values from data access component individually
Aug 02, 2010 11:47 AM|LINK
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
hwraja
Member
10 Points
11 Posts
Re: Reading values from data access component individually
Aug 02, 2010 12:11 PM|LINK
Thanks for your response. But how do I call the reader within the page? What I am currently doing is:
protected void GridView3_SelectedIndexChanged(object sender, EventArgs e)
{
int reg_id = (int)GridView3.SelectedDataKey.Values["Reg_id"];
Label3.Text = reg_id.ToString();
db.GetRecord(reg_id);
}
How do i pick those values on the form? I was wondering whether a stored procedure with output parameters is the answer?
cheers,
vinz
All-Star
128414 Points
18131 Posts
MVP
Re: Reading values from data access component individually
Aug 02, 2010 12:20 PM|LINK
ermm.. are you trying to reference a value from Gridview or from the database? If you are trying to get the values from the database then you can create a method that returns a DataTable, then from the DataTable you can iterate to each rows to get the individual row values.
Here's a quick example:
public DataTable GetData(string ID) { DataTable dt = new DataTable(); SqlConnection conn = new SqlConnection("Your Connection String"); string sqlStatement = "SELECT TypeID FROM ProjectTypes WHERE ColumnID = @ID"; try { conn.Open(); SqlCommand sqlCmd = new SqlCommand(sqlStatement, conn); sqlCmd.Parameters.AddWithValue("@ID",ID); SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd); sqlDa.Fill(dt); } catch (System.Data.SqlClient.SqlException ex) { string msg = "Fetch Error:"; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } return dt; }You may also try to refer this example: Creating a Simple Data Access Framework in ASP.NET
MessageBox Controls for WebForms | Blog | Twitter | Linkedin
hwraja
Member
10 Points
11 Posts
Re: Reading values from data access component individually
Aug 02, 2010 12:52 PM|LINK
Hi Again,
I wasnt very clear in my last post.
I am picking a value ["Reg_id"] from the gridview. With this parameter I want to call the values through DAL (first post).
What I am unable to do is:
How do I call data from data access class in my page with the above value i.e. REG_ID]?
I tried
int abc = db.GetRecord(id);
it works Ok with gridview or detailsviews but what I want to do is read those values to different text boxes/ labels myself in the page.cs file.
I have also tried to read these values to a datatable in my page.cs file by
DataTable dt = db.GetRecord(reg_id);
but the following error comes up
"Cannot implictly convert Type "class name" to system.data.datatable"
cheers
vinz
All-Star
128414 Points
18131 Posts
MVP
Re: Reading values from data access component individually
Aug 27, 2010 02:56 PM|LINK
As I've mentioned, you have to create a method that returns a DataTable so that you can iterate each rows stored in the DataTable and populate your TextBox/Label control accordingly.
MessageBox Controls for WebForms | Blog | Twitter | Linkedin