0

Hello,
I am new to C# and i am stuck when i try to fetch the data from SQL Server to Visual studio. I am writing the code as a n-tier application - Data Access, Business logic and the front end. I am fetching data in a disconnected env. I am able to retrive data in a grid view but, I am not able to fetch in my TB.
Here's my code so far-

DataAccess Layer:

 public DataTable EditCustomer(string query, string queryType, List<SqlParameter> parameters)
        {
            DataSet ds;
            conn = new SqlConnection("Data Source=Laptop-HP\\SQLEXPRESS;Initial Catalog=Banking_new;Integrated
                                        Security=SSPI");
            cmd = new SqlCommand(query, conn);
            if (queryType == "SPS")
                cmd.CommandType = CommandType.StoredProcedure;
            else
                cmd.CommandType = CommandType.Text;
            if (parameters != null)
            {
                if (parameters.Count > 0)
                {
                    foreach (SqlParameter param in parameters)
                        cmd.Parameters.Add(param);
                }
                try
                {
                    SqlDataAdapter da = new SqlDataAdapter(cmd);
                    ds = new DataSet();
                    da.Fill(ds);
                    return ds.Tables[0];
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.Message);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

Business Logic:

 public DataTable editCustomer()
        {
            DataAccessLayer.DataManager objEditData = new DataAccessLayer.DataManager();
            List<SqlParameter> param = new List<SqlParameter>();
            return objEditData.EditCustomer("select * from customer where customerid = '100'", "Text", param);
        }

FrontEnd:

 protected void Button4_Click(object sender, EventArgs e)
        {
            CustomerEntities.Customer objEdit = new CustomerEntities.Customer();
            objEdit.CustId = TextBox1.Text;

            BusinessLayer.Program objBussEdit = new BusinessLayer.Program();

            GridView1.DataSource = objBussEdit.editCustomer();
            GridView1.DataBind();
        }

I am not able to assign the datatable values to the textboxes.

Please help.

Edited by kool005

2
Contributors
1
Reply
22
Views
3 Years
Discussion Span
Last Post by johnrosswrock
0

Try this..

SqlConnection Conn = new SqlConnection(Connection_String);
SqlCommand Comm = new SqlCommand(Command, Conn);
Conn.Open();
SqlDataReader srd = Comm.ExecuteReader();
if (srd.Read())
{
    textBox.Text = srd.GetValue(0).ToString();
}
Conn.Close();

P.S:This just a example change it according to your requirement

Edited by johnrosswrock

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.