Click here to Skip to main content
15,892,161 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi im trying to use SqldataReader to read all the columns from my table and show them in the textbox of the form, but it gives me an error saying that there no object reference
the dr (SqldataReader) is below Public Class frmRegCoop

VB
selectCommand.Connection = conn
  selectCommand.CommandText = "SELECT * FROM [tblEjemplo] WHERE COOP_ID= @COOP_ID"

  selectCommand.CommandType = CommandType.Text
  selectCommand.Parameters.Add("@COOP_ID", SqlDbType.Int).Value = txtIdCoop.Text

  While dr.Read ()
      Try
          conn.Open()

          selectCommand.ExecuteReader()
          txtNombreCoop.Text = dr("@COOP_NOMBRE").ToString()
          EstatusRecordRCoop.Text = "Record Encontrado"

      Catch ex As SqlException
          EstatusRecordRCoop.Text = "Error"
          MessageBox.Show(ex.Message)
      Finally
          conn.Close()
      End Try

  End While

Thanks
Posted
Updated 21-Mar-13 5:27am
v2
Comments
Sergey Alexandrovich Kryukov 21-Mar-13 11:20am    
What exactly do you mean by "VB"?
—SA
[no name] 21-Mar-13 11:22am    
Without more information it is difficult to say. This is one of the easiest problem to find and fix, you just have to debug it. You do not show where you instantiate dr in your code though.
TesterJSR 21-Mar-13 11:39am    
the dr (SqldataReader) is below Public Class frmRegCoop
[no name] 21-Mar-13 11:44am    
That means nothing to me. You are actually getting the reader, selectCommand.ExecuteReader(), after you are trying to use it, dr.Read().

1 solution

You haven't executed the command, os the chancesa re that the reader is not initialized. Try:
VB
selectCommand.Parameters.Add("@COOP_ID", SqlDbType.Int).Value = txtIdCoop.Text
dr = selectCommand.ExecuteReader()
While dr.Read ()


"I fix it but they are now without the @ am I vulnerable to sqlInjection? also the new error I get is after searchin,inserting or updating one time after I do it for second time it tells me The variable name '@COOP_ID' has already been declared. Variable names must be unique within a query batch or stored procedure."

Change your code to like this:
VB
selectCommand.CommandType = CommandType.Text
selectCommand.Parameters.Add("@COOP_ID", SqlDbType.Int).Value = txtIdCoop.Text
conn.Open()

selectCommand.ExecuteReader()

    Try
        While dr.Read ()
            txtNombreCoop.Text = dr("@COOP_NOMBRE").ToString()
            EstatusRecordRCoop.Text = "Record Encontrado"
        End While
    Catch ex As SqlException
        EstatusRecordRCoop.Text = "Error"
        MessageBox.Show(ex.Message)
    Finally
        conn.Close()
    End Try

You may also want to make the the textbox.Text set add the text rather than overwrite it each time.
You may find you don't want the "@" on the dr access - unless you have a column named @COOP_NOMBRE you shouldn't need it. (It's also good practice to explicitly nam eteh columns you want returned in teh SELECT statement rather than using "SELECT * FROM..." as the latter wastes bandwidth and time).
 
Share this answer
 
v2
Comments
TesterJSR 21-Mar-13 13:26pm    
I am getting now IndexOutOfRangeException
OriginalGriff 21-Mar-13 13:36pm    
Where? Which line?
TesterJSR 21-Mar-13 14:13pm    
I fix it but they are now without the @ am I vulnerable to sqlInjection?
also the new error I get is after searchin,inserting or updating one time after I do it for second time it tells me The variable name '@COOP_ID' has already been declared. Variable names must be unique within a query batch or stored procedure.
OriginalGriff 21-Mar-13 15:04pm    
Answer updated
TesterJSR 21-Mar-13 16:15pm    
Ok thanks, and do you know how to make this into Visual Basic? converters are not working

// Button click event handler
//

private void btnLoad_Click(object sender, EventArgs e)
{
ArrayList ColorList = new ArrayList();
Type colorType = typeof(System.Drawing.Color);
PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static |
BindingFlags.DeclaredOnly | BindingFlags.Public);
foreach (PropertyInfo c in propInfoList)
{
this.cmbboxClr.Items.Add(c.Name);
}

// DrawItem event handler
//

private void cmbboxClr_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Rectangle rect = e.Bounds;
if (e.Index >= 0)
{
string n = ((ComboBox)sender).Items[e.Index].ToString();
Font f = new Font("Arial", 9, FontStyle.Regular);
Color c = Color.FromName(n);
Brush b = new SolidBrush(c);
g.DrawString(n, f, Brushes.Black, rect.X, rect.Top);
g.FillRectangle(b, rect.X + 110, rect.Y + 5,
rect.Width -10, rect.Height - 10);
}

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