Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I would suggest</p> <ul> <li>using a <code>using () { ... }</code> block for your <code>SqlConnection</code> to ensure proper disposal of those objects</li> <li>use <strong>parametrized queries</strong> to avoid SQL injection attacks</li> </ul> <p>So change your code to be:</p> <pre><code>private void UserFullDetail_Load(object sender, EventArgs e) { string connectionString = "Data Source=PEWPEWDIEPIE\\SQLEXPRESS;Initial Catalog=master;Integrated Security=True"; DataTable dts3 = new DataTable(); using (SqlConnection SCScon = new SqlConnection(connectionString)) { string query = "SELECT cDetails, cDetails2, mainCate, PhoneNumber, PersonCharge FROM dbo.ComDet WHERE cName = @CName"; SqlDataAdapter daS = new SqlDataAdapter(query, SCScon); daS.SelectCommand.Parameters.Add("@CName", SqlDbType.VarChar, 100).Value = lblcName.Text; SCScon.Open(); daS.Fill(dts3); SCScon.Close(); } // get the first (and hopefully only) row from your DataTable DataRow firstRow = dts3.Rows[0]; // fill the values to your labels lblFirstDetail.Text = firstRow["cDetails"].ToString(); lblSecondDetail.Text = firstRow["cDetails2"].ToString(); lblCategory.Text = firstRow["mainCate"].ToString(); lblPhoneNumber.Text = firstRow["PhoneNumber"].ToString(); lblPersonInCharge.Text = firstRow["PersonCharge"].ToString(); } </code></pre> <p>Of course, for your "real world" app, you'd have to add some checking and error handling (e.g. make sure the value in <code>firstRow["mainCate"]</code> isn't returning <code>NULL</code> - because in that case, calling <code>.ToString()</code> on it will cause an exception), but that's the basic mechanism of how your code should look like.</p> <p>And also, from an architecture point of view - I would strongly recommend to put all your database access code into a separate assembly that handles all of this work - don't put all this code into all your code-behind classes! </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload