hey guys..i need some professional friends critics about the coding i made in my small project...when i want to select one row data from my project i coded like that..in my DataAccess layer
public SqlDataAdapter FirmaDetails(string gelenFirma)
{
cmd = new SqlCommand("sp_FirmaDetails", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@gelen_firma", System.Data.SqlDbType.NVarChar).Value = gelenFirma;
da = new SqlDataAdapter(cmd);
return da;
}
and then in my Helper class
public DataTable FirmaDetails(string gelenFirma)
{
da = d.FirmaDetails(gelenFirma);
da.Fill(dt);
return dt;
}
then to get the datas inside of my form
private void DetayForm_Load(object sender, EventArgs e)
{
dt = new DataTable();
dt = helpr.FirmaDetails(textBox1.Text);
for (int i=0; i < dt.Rows.Count; i++)
{
lblYetkili.Text = dt.Rows[i][0].ToString();
lblGsm.Text = dt.Rows[i][1].ToString();
lblEmail.Text = dt.Rows[i][2].ToString();
lblYetkili2.Text = dt.Rows[i][3].ToString();
lblGsm2.Text = dt.Rows[i][4].ToString();
lblEmail2.Text = dt.Rows[i][5].ToString();
lblYetkili3.Text = dt.Rows[i][6].ToString();
lblGsm3.Text = dt.Rows[i][7].ToString();
lblEmail3.Text = dt.Rows[i][8].ToString();
}
}
now i wonder if it is too rookie technique(actually i am already) if it is how i can improve in better way..should i use SqlDataReader..thanks for your advices
Need critics about coding technique
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Soumya Parida4 pointsPosted Dec 25, 2010, 5:32 PM EST
From your code, I am guessing that you have one connection open for the Helper.
I would suggest to combine the 2 methods FirmaDetails, and return a Datatable. And if you dont query the database very often you might want to open and close the connection within that method itself [Its not required though]
Also you are iterating through all the rows, if the stored procedure return more than 1 row you may not be showing the first row in the datatable. So instead check if rowcount > 1 and then simply get the first row to populate the data.
SQLDataReader is used when you have a huge data and dont want to load up everything into the memory, rather go one by one, but then you need that connection open specifically for the reader.
Erdinc Kolukisa67 pointsPosted Dec 27, 2010, 10:43 PM EST
thanks again