1

I'm using VS with C# to create a search function. This function will use a text box so the user can input a claim number and a search button so the data from that claim is displayed in another set of text boxes.

The problem is, after I enter the claim number and click the button, no data is displayed and i get no error when running it.

I followed some suggestion from other questions in here but so far I haven't been able to get it to work, here is my code:

        SqlConnection con = new SqlConnection("Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=SISProductionDB;Data Source=Servername");
        DataTable dt = new DataTable();
        DataSet ds = new DataSet();
        SqlDataReader myReader = null;

        con.Open();          

        SqlCommand myCommand = new SqlCommand("SELECT * FROM Claim WHERE ClaimNumber = '%@ClaimNumber%'", con);
        myCommand.Parameters.AddWithValue("@ClaimNumber", NumQuerellaTxt.Text);
        SqlDataAdapter SDA = new SqlDataAdapter(myCommand.CommandText, con);

        myReader = myCommand.ExecuteReader();
        myReader.Read();

        while (myReader.Read()) 
        {
            ClaseQuerellaTxt.Text = (myReader["ClaimID"].ToString());
            TipoQuerellaTxt.Text = (myReader["ClaimTypeID"].ToString());
            FuenteQuerellaTxt.Text = (myReader["ClaimTypeID"].ToString());
            EstatusQuerellaTxt.Text = (myReader["ClaimTypeID"].ToString());
            OficialAtiendeTxt.Text = (myReader["ClaimTypeID"].ToString());
            OficialInvestigaTxt.Text = (myReader["ClaimTypeID"].ToString());
            QuerellaTxt.Text = (myReader["ClaimTypeID"].ToString());
            FechaQuerellaTxt.Text = (myReader["ClaimTypeID"].ToString());
            FechaIncidenteTxt.Text = (myReader["ClaimTypeID"].ToString());
            HoraIncidenteTxt.Text = (myReader["ClaimTypeID"].ToString());
            AbogadoAtiendeTxt.Text = (myReader["ClaimTypeID"].ToString());
            FechaVistaTxt.Text = (myReader["ClaimTypeID"].ToString());
            HoraVistaTxt.Text = (myReader["ClaimTypeID"].ToString());
            SalaVistaTxt.Text = (myReader["ClaimTypeID"].ToString());
        }

        con.Close();
3
  • It is a good practice to use a stored procedure instead of setting your query inside the code. Commented Feb 27, 2014 at 15:32
  • Generally select * should only be used in ad-hoc queries, if someone changes the table structure your query will break. Better to explicitly name the columns you want Commented Feb 27, 2014 at 15:33
  • I'd also recommend using a using for your connection and datareader. If something errors here your connection may not be disposed of Commented Feb 27, 2014 at 15:34

1 Answer 1

8

Before you read the solution below, you should probably try to use the debugger and find the issue yourself. Debugging is an essential skill that you need to develop. You should be able to find bugs like this.

Answer: You are skipping the first row:

myReader.Read();

while (myReader.Read()) 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for the tip about the bug, I managed to fix that, unfortunately it did not solve my problem, The program still doesn't display any data in the text boxes, any more suggestions? I would appreciate it a lot.
Use the debugger to find out how many rows were read. (You'll find then number to be zero.) => Your query must be faulty. Run the query in SSMS. You'll see that no rows are returned here either. Now you know that your C# app is not at fault. Just the query is. Much simpler problem to solve.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.