In 3 tier architecture , filling textboxes with values from database table
-
Hi Friends, Can anyone help me with this? Using 3 tier architecture,how to fetch data directly from database table to textboxes when I clicked on select button in grid view,I have a table in SQL database.Upon clicking on select link button in grid view,the selected row values must be fetched from database table to text boxes and must be filled with respective values according to database table.I have used sqldatareader, taken OnSelectedIndexChanged ,written query in stored procedure for fetching selected row but values are not binding to text boxes.Any suggestions would be helpful.
-
Hi Friends, Can anyone help me with this? Using 3 tier architecture,how to fetch data directly from database table to textboxes when I clicked on select button in grid view,I have a table in SQL database.Upon clicking on select link button in grid view,the selected row values must be fetched from database table to text boxes and must be filled with respective values according to database table.I have used sqldatareader, taken OnSelectedIndexChanged ,written query in stored procedure for fetching selected row but values are not binding to text boxes.Any suggestions would be helpful.
-
As you did not showed what you have tried it will be hard us to know. Did you tried to debug and see if you really pulling any values back on your event to bind?
DAL: public DataTable FetchDetails(string pid, string pname, string pcid, string sdesc, string desc, string qty, string prce, string len) { var a = new ArrayList(); SqlCommand cmd = new SqlCommand("spfetchdetail", con); con.Open(); cmd.ExecuteNonQuery(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@pid", pid); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { pid = sdr["P_id"].ToString(); pname = sdr["P_name"].ToString(); pcid = sdr["c_name"].ToString(); sdesc = sdr["P_shortdesc"].ToString(); desc = sdr["P_desc"].ToString(); qty = sdr["P_qty"].ToString(); prce = sdr["P_price"].ToString(); len = sdr["P_length"].ToString(); a.Add(pid); a.Add(pname); a.Add(pcid); a.Add(sdesc); a.Add(desc); a.Add(qty); a.Add(prce); a.Add(len); } return dt; } BAL: public object fetchdetail(string pid, string pname, string pcid, string sdesc, string desc, string qty, string prce, string len) { return dll.FetchDetails(pid, pname, pcid, sdesc, desc, qty, prce, len); } CS: protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string pid = ""; string pname = ""; string pcid = ""; string sdesc = ""; string desc = ""; string qty = ""; string prce = ""; string len = ""; bll.fetchdetail(pid, pname, pcid, sdesc, desc, qty, prce, len); HiddenField1.Value = pid; txt_proName.Text = pname; ddl_category.SelectedValue = pcid; txt_sdesc.Text = sdesc; txt_ldesc.Text = desc; txt_qnty.Text = qty; txt_price.Text = prce; txt_len.Text = len; }
-
DAL: public DataTable FetchDetails(string pid, string pname, string pcid, string sdesc, string desc, string qty, string prce, string len) { var a = new ArrayList(); SqlCommand cmd = new SqlCommand("spfetchdetail", con); con.Open(); cmd.ExecuteNonQuery(); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@pid", pid); SqlDataAdapter sda = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); sda.Fill(dt); SqlDataReader sdr = cmd.ExecuteReader(); while (sdr.Read()) { pid = sdr["P_id"].ToString(); pname = sdr["P_name"].ToString(); pcid = sdr["c_name"].ToString(); sdesc = sdr["P_shortdesc"].ToString(); desc = sdr["P_desc"].ToString(); qty = sdr["P_qty"].ToString(); prce = sdr["P_price"].ToString(); len = sdr["P_length"].ToString(); a.Add(pid); a.Add(pname); a.Add(pcid); a.Add(sdesc); a.Add(desc); a.Add(qty); a.Add(prce); a.Add(len); } return dt; } BAL: public object fetchdetail(string pid, string pname, string pcid, string sdesc, string desc, string qty, string prce, string len) { return dll.FetchDetails(pid, pname, pcid, sdesc, desc, qty, prce, len); } CS: protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) { string pid = ""; string pname = ""; string pcid = ""; string sdesc = ""; string desc = ""; string qty = ""; string prce = ""; string len = ""; bll.fetchdetail(pid, pname, pcid, sdesc, desc, qty, prce, len); HiddenField1.Value = pid; txt_proName.Text = pname; ddl_category.SelectedValue = pcid; txt_sdesc.Text = sdesc; txt_ldesc.Text = desc; txt_qnty.Text = qty; txt_price.Text = prce; txt_len.Text = len; }
Couple of issues with your code in
FetchDetails
method. 1. You have populated the result into arraylist object which is local to this function 2. You executing command object with ExecuteNonQuery, Reader and DataAdapter 3. You populated Dataset to return from FetchDetails in DAL and did not received it in calling method to bind data. Populate your local variables in SelectChange event from DataSet object you reveiced from BAL and should work for you.
-
Couple of issues with your code in
FetchDetails
method. 1. You have populated the result into arraylist object which is local to this function 2. You executing command object with ExecuteNonQuery, Reader and DataAdapter 3. You populated Dataset to return from FetchDetails in DAL and did not received it in calling method to bind data. Populate your local variables in SelectChange event from DataSet object you reveiced from BAL and should work for you.
Thank u for u r response. Unfortunately I am new to work with three tier, I am unable to get your point.Could u pls modify the code which i have provided so that it will be very helpful for me.
-
Thank u for u r response. Unfortunately I am new to work with three tier, I am unable to get your point.Could u pls modify the code which i have provided so that it will be very helpful for me.
Here you go. You need to extract data from data table now in below code to populate your variables which you may be binding on screen. DAL:
public DataTable FetchDetails()
{
con.Open();
DataTable dt = new DataTable();
using(SqlCommand cmd = new SqlCommand("spfetchdetail", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pid", pid);SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); } return dt.Table\[0\];
}
BAL:
public DataTable fetchdetail()
{
return dll.FetchDetails(); // Call to your DAL returning Datatable
}CS:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string pid = ""; string pname = ""; string pcid = ""; string sdesc = ""; string desc = ""; string qty = ""; string prce = ""; string len = "";
DataTable dt = bll.fetchdetail(); // Call to BAL and reveice datatable
// extract the data from this datatable now to populate your variables now
HiddenField1.Value = pid;
txt_proName.Text = pname;
ddl_category.SelectedValue = pcid;
txt_sdesc.Text = sdesc;
txt_ldesc.Text = desc;
txt_qnty.Text = qty;
txt_price.Text = prce;
txt_len.Text = len;
} -
Here you go. You need to extract data from data table now in below code to populate your variables which you may be binding on screen. DAL:
public DataTable FetchDetails()
{
con.Open();
DataTable dt = new DataTable();
using(SqlCommand cmd = new SqlCommand("spfetchdetail", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@pid", pid);SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); } return dt.Table\[0\];
}
BAL:
public DataTable fetchdetail()
{
return dll.FetchDetails(); // Call to your DAL returning Datatable
}CS:
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string pid = ""; string pname = ""; string pcid = ""; string sdesc = ""; string desc = ""; string qty = ""; string prce = ""; string len = "";
DataTable dt = bll.fetchdetail(); // Call to BAL and reveice datatable
// extract the data from this datatable now to populate your variables now
HiddenField1.Value = pid;
txt_proName.Text = pname;
ddl_category.SelectedValue = pcid;
txt_sdesc.Text = sdesc;
txt_ldesc.Text = desc;
txt_qnty.Text = qty;
txt_price.Text = prce;
txt_len.Text = len;
}Thank u soo much for your reply. But unfortunately i'm getting error in DAL The name 'pid' does not exist in the current context This is my Stored Procedure: CREATE proc [dbo].[spfetchdetail] @pid int as begin select * from Products LEFT JOIN Category ON Products.P_C_id=Category.c_id where P_id=@pid end
-
Thank u soo much for your reply. But unfortunately i'm getting error in DAL The name 'pid' does not exist in the current context This is my Stored Procedure: CREATE proc [dbo].[spfetchdetail] @pid int as begin select * from Products LEFT JOIN Category ON Products.P_C_id=Category.c_id where P_id=@pid end
-
I might have removed all params so its error. You should pass the pid as parameter to DAL to resolve it.
In DAL public DataTable FetchDetails(int pid) { con.Open(); DataTable dt = new DataTable(); using (SqlCommand cmd = new SqlCommand("spfetchdetail", con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@pid", pid); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); } return dt; } In BAL: public DataTable fetchdetail(int pid) { return dll.FetchDetails(pid); } In CS page,passing pid string pid="";string pname = ""; string sdesc = ""; string desc = ""; string qty = ""; string prce = ""; string len = ""; DataTable dt = bll.fetchdetail(pid); getting type conversion error
-
In DAL public DataTable FetchDetails(int pid) { con.Open(); DataTable dt = new DataTable(); using (SqlCommand cmd = new SqlCommand("spfetchdetail", con)) { cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@pid", pid); SqlDataAdapter sda = new SqlDataAdapter(cmd); sda.Fill(dt); } return dt; } In BAL: public DataTable fetchdetail(int pid) { return dll.FetchDetails(pid); } In CS page,passing pid string pid="";string pname = ""; string sdesc = ""; string desc = ""; string qty = ""; string prce = ""; string len = ""; DataTable dt = bll.fetchdetail(pid); getting type conversion error
-
Well gave you direction but cant fix all here as it wont run. You need to debug and identify the issue. check if you really have data coming from SP outout in debug
Thank u so much vinod, i have modified the code a little and its perfectly working now.Thank u for ur time.