Click here to Skip to main content
15,889,266 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
private void txtDesc_TextChanged(object sender, EventArgs e)
       {
      SqlDataAdapter dap = new SqlDataAdapter("select partnumber from movement where     description=('"+txtDesc.Text+"')",con);
           DataSet dsp = new DataSet();
           dap.Fill(dsp, "movement");
           DataView dvp = new DataView(dsp.Tables["movement"]);
           txtPartno.Text = dvp[0]["partnumber"].ToString();
       }
Posted
Updated 20-Jan-14 4:01am
v2

Error line is (possibly):

C#
txtPartno.Text = dvp[0]["partnumber"].ToString();


Your query returns no row, therefore table has no row, therefore view has no row, therefore dvp[0] corresponds to no row. As a result, you have an exception in your hand.

But I think the actual problem is your implementation. If this event handler is for a TextBox, then whenever user presses a key, that code runs. For example, if you intend to write "a description", when you press key 'a', your query runs for description=('a') which (possibly) returns no row.

Some advices:
1- design multitier application (design a separate class for data management)
2- enclose your UI handling codes in a 'try catch' structure to avoid 'unhandled exception' and a total crash.
3- use the debugger to trace your code.
 
Share this answer
 
Add this line

C#
  if(dvp.Count > 0)
txtPartno.Text = dvp[0]["partnumber"].ToString();


or
C#
DataTable dt = new DataTable();
           dap.Fill(dt);
           if(dt.Rows.Count > 0)
           txtPartno.Text = dt.Rows[0]["partnumber"].ToString();
 
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