Click here to Skip to main content
15,914,071 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Everyone!

I am having a problem on how to insert the data from the database to their specific labels assigned.First, it will search the records based from the inputted information from my two text boxes. If it matches any, it will display the information that will insert to the labels assigned to them but I got an error message,"Index was outside the bounds of the array". But I know to myself that I am accessing the right indexes in the database. Can anyone help me out? I need to present this also today :( and I really need a hand.

Here is my code:
C#
connection_repgen();
        amicassaCon_repgen.Open();
        SqlCommand searchquery = new SqlCommand("SELECT ContractBuyerCode,ContractBuyerName,ContractSBU,ContractProjectName,ContractPrjUnitDesc,ContractModel,ContractStatus FROM MC.tblContracts WHERE ContractCompanyCode ='" + company_code.Text + "' AND ContractNo = '" + contract_no.Text + "'", amicassaCon_repgen);
        SqlDataReader dr = searchquery.ExecuteReader();
        while (dr.Read())
        {
            
            buyer_code.Text = dr[7].ToString();
            company_name.Text = dr[0].ToString();
            status.Text = dr[3].ToString();
            buyer_name.Text = dr[10].ToString();
            project_name.Text = dr[11].ToString();
            unit_desc.Text = dr[12].ToString();
            model.Text = dr[18].ToString();
           
           
        }
        dr.Close();
        amicassaCon_repgen.Close();
Posted
Updated 18-Aug-14 17:30pm
v2

index is based on position of column in your select statement, you have
ContractBuyerCode,ContractBuyerName,ContractSBU,ContractProjectName,ContractPrjUnitDesc,ContractModel,ContractStatus 

columns in your select sql. so ContractBuyerCode has 0 index, ContractBuyerName has index 1, so on...
change the code accordingly
 
Share this answer
 
Comments
DarkDreamer08 18-Aug-14 23:37pm    
Thank you so muchh..... !!!!
And to add to the answer concerning indexing start point: To be safe from SQL injections, data conversion problems etc, you should use SqlParameter in your queries instead of directly concatenating values from the text boxes to the SQL statement.

So you code could look something like
C#
...
SqlCommand searchquery = new SqlCommand(
"SELECT tc.ContractBuyerCode,                       
        tc.ContractBuyerName, 
        tc.ContractSBU, 
        tc.ContractProjectName, 
        tc.ContractPrjUnitDesc, 
        tc.ContractModel, 
        tc.ContractStatus 
FROM MC.tblContracts tc
WHERE tc.ContractCompanyCode = @ContractCompanyCode
AND   tc.ContractNo          = @ContractNo", amicassaCon_repgen);
   searchquery.Parameters.Add( new SqlParameter() {
      ParameterName = "@ContractCompanyCode",
      DbType = SqlDbType.VarChar,
      Size = 100,
      Value = company_code.Text }; // Remember to validate the data first?
   searchquery.Parameters.Add( new SqlParameter() {
      ParameterName = "@ContractNo",
      DbType = SqlDbType.Int,
      Value = contract_no.Text };
   SqlDataReader dr = searchquery.ExecuteReader();
...


For more information, see:
- SQL injection[^]
- SqlParameter class[^]
 
Share this answer
 
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900