[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1922
  • Last Modified:

Urgently?? Cannot use empty object or column names. Use a single space if necessary.

What can I do?

Function.

Public Function consultarpor(ByVal strsql As String) As DataSet
        Dim da As New SqlDataAdapter(strsql, dbsiabconn.dbsiabconnection)
        Dim ds As New DataSet
        da.Fill(ds)
        Return ds
    End Function

Caller of the function

   Private Sub btnconsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconsultar.Click
        Dim strsql As String
        If c1.Checked Then
            strsql = "select * from vistacorrespondencia where remitente like '%" & txtremitente.Text & "%'"""
            Dim ds As DataSet
            ds = objconsultas.consultarpor(strsql)
            dgconsultas.DataSource = ds
            dgconsultas.DataBind()
        End If
        If c2.Checked Then
            strsql = "select * from vistacorrespondencia where radicara like '%" & txtdestinatario.Text & "%'"""
            Dim ds As DataSet
            ds = objconsultas.consultarpor(strsql)
            dgconsultas.DataSource = ds
            dgconsultas.DataBind()
        End If
        If c3.Checked Then
            strsql = "select * from vistacorrespondencia where asunto  like '%" & txtasunto.Text & "%'"""
            Dim ds As DataSet
            ds = objconsultas.consultarpor(strsql)
            dgconsultas.DataSource = ds
            dgconsultas.DataBind()
        End If
        If c4.Checked Then
            strsql = "select * from vistacorrespondencia where fecha >=  " & fecha.SelectedDate & " "
            Dim ds As DataSet
            ds = objconsultas.consultarpor(strsql)
            dgconsultas.DataSource = ds
            dgconsultas.DataBind()
        End If
   
    End Sub


View.

SELECT     dbo.tblcorrespondencia.num_radi AS Radicado, dbo.tblcorrespondencia.fec_radi AS Fecha, c.remitente, dbo.tblcorrespondencia.Asunto AS Asunto,
                      dbo.tblcorrespondencia.Observacion AS Observacion, dbo.tblFuncionarios.nombre AS Nombre, dbo.tblFuncionarios.radicara AS Radicara,
                      '' AS RecibidoPor, dbo.tblcorrespondencia.nombrearchivo AS Nombrearchivo, dbo.tblcorrespondencia.despachado AS Despachado
FROM         dbo.tblcorrespondencia INNER JOIN
                      dbo.tblremitentesinternos c ON dbo.tblcorrespondencia.Remitente = c.idremitente INNER JOIN
                      dbo.tblFuncionarios ON dbo.tblcorrespondencia.idfuncionario = dbo.tblFuncionarios.idfuncionario
0
djhex
Asked:
djhex
  • 6
  • 4
  • 2
  • +1
1 Solution
 
nauman_ahmedCommented:
Run this query in SQL Query Analyzer and let me know what error it is giving.

Best, Nauman.
0
 
djhexAuthor Commented:
No error.
0
 
TimCotteeCommented:
Hi djhex,
> strsql = "select * from vistacorrespondencia where fecha >=  " & 
> fecha.SelectedDate & " "

Shouldn't this have ' around it if it is a date field?

strsql = "select * from vistacorrespondencia where fecha >=  '" & fecha.SelectedDate & "' "

Especially if the fecha.SelectedDate value is an empty string.

Tim Cottee
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
djhexAuthor Commented:
Maybe You are right but I am trying with the first choice. I havent tried the other choices yet.

0
 
raterusCommented:
I bet it has something to do with the dynamic sql you are using.  Try making a command object in your calling sub, and pass that (use parameters for variables, not dynamic sql -- http://www.knowdotnet.com/articles/dynamisql.html)

            Dim cmd as SqlCommand = new SqlCommand()
            cmd.commandtext = "select * from vistacorrespondencia where fecha >= @fecha"
            cmd.parameters.add(@fecha, fecha.SelectedDate)
            Dim ds As DataSet
            ds = objconsultas.consultarpor(cmd)
            dgconsultas.DataSource = ds
            dgconsultas.DataBind()


Public Function consultarpor(ByVal cmd As SqlCommand) As DataSet
        cmd.connection = dbsiabconn.dbsiabconnection
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds)
        Return ds
    End Function
0
 
nauman_ahmedCommented:
Tim sounds correct :)  There should be single quote in the following SQL query:

strsql = "select * from vistacorrespondencia where fecha >=  '" & fecha.SelectedDate & "' "


HTH, Nauman.
0
 
djhexAuthor Commented:
I tried raterus solution but I still get the same error.

Look at my datagrid code maybe it helps.

<asp:DataGrid id="dgconsultas" runat="server" AutoGenerateColumns="False">
                                                <Columns>
                                                      <asp:BoundColumn DataField="Radicado" HeaderText="Radicado"></asp:BoundColumn>
                                                      <asp:BoundColumn DataField="Fecha" HeaderText="Fecha"></asp:BoundColumn>
                                                      <asp:BoundColumn DataField="Remitente" HeaderText="Remitente"></asp:BoundColumn>
                                                      <asp:BoundColumn DataField="Asunto" HeaderText="Asunto"></asp:BoundColumn>
                                                      <asp:BoundColumn DataField="Observacion" HeaderText="Observacion"></asp:BoundColumn>
                                                      <asp:BoundColumn DataField="Nombre" HeaderText="Nombre"></asp:BoundColumn>
                                                      <asp:BoundColumn DataField="Radicara" HeaderText="Radicara"></asp:BoundColumn>
                                                      <asp:BoundColumn DataField="RecibidoPor" HeaderText="Recibido por"></asp:BoundColumn>
                                                      <asp:BoundColumn DataField="nombrearchivo" HeaderText="Nombre Archivo"></asp:BoundColumn>
                                                </Columns>
                                          </asp:DataGrid>
0
 
djhexAuthor Commented:
Just trying with the first choice.
If it helps I made an step by step and the error is thrown in  the FILL line of the adapter.

    Public Function consultarpor(ByVal cmd As SqlCommand) As DataSet
        cmd.Connection = New SqlConnection(dbsiabconn.dbsiabconnection)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds)
        Return ds
    End Function


Private Sub btnconsultar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnconsultar.Click
        Dim strsql As String
        If c1.Checked Then
            Dim cmd As SqlCommand = New SqlCommand
            cmd.commandtext = "select * from vistacorrespondencia where remitente like '%" & txtremitente.Text & "%'"""
            Dim ds As DataSet
            ds = objconsultas.consultarpor(cmd)
            dgconsultas.DataSource = ds
            dgconsultas.DataBind()
        End If

0
 
raterusCommented:
what is the full error, give stack trace...
0
 
raterusCommented:
stop using dynamic sql (read the link I posted), fix and it you still get error, let me know...
0
 
djhexAuthor Commented:
Error de servidor en la aplicación '/dbsiab2'.
--------------------------------------------------------------------------------

Cannot use empty object or column names. Use a single space if necessary.
Descripción: Excepción no controlada al ejecutar la solicitud Web actual. Revise el seguimiento de la pila para obtener más información acerca del error y dónde se originó en el código.

Detalles de la excepción: System.Data.SqlClient.SqlException: Cannot use empty object or column names. Use a single space if necessary.

Error de código fuente:


Línea 50:             cmd.commandtext = "select * from vistacorrespondencia where remitente like '%" & txtremitente.Text & "%'"""
Línea 51:             Dim ds As DataSet
Línea 52:             ds = objconsultas.consultarpor(cmd)
Línea 53:             dgconsultas.DataSource = ds
Línea 54:             dgconsultas.DataBind()
 

Archivo de origen: C:\AplicacionesWeb\DbSiab\consultarcorrespondencia.aspx.vb    Línea: 52

Seguimiento de la pila:


[SqlException: Cannot use empty object or column names. Use a single space if necessary.]
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior cmdBehavior, RunBehavior runBehavior, Boolean returnStream)
   System.Data.SqlClient.SqlCommand.ExecuteReader(CommandBehavior behavior)
   System.Data.SqlClient.SqlCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
   System.Data.Common.DbDataAdapter.FillFromCommand(Object data, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)
   System.Data.Common.DbDataAdapter.Fill(DataSet dataSet)
   CsDatos.Consultas.consultarpor(SqlCommand cmd) in C:\Documents and Settings\luisvalen\Mis documentos\Visual Studio Projects\DbSiab\CsDatos\Consultas.vb:322
   DbSiab.consultarcorrespondencia.btnconsultar_Click(Object sender, EventArgs e) in C:\AplicacionesWeb\DbSiab\consultarcorrespondencia.aspx.vb:52
   System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   System.Web.UI.Page.ProcessRequestMain()

 


--------------------------------------------------------------------------------
Información de versión: Versión de Microsoft .NET Framework:1.1.4322.573; Versión de ASP.NET:1.1.4322.573
0
 
raterusCommented:
txtremitente.Text have a ' in it?  response.write the sql right before it hits sql server and let me know what it looks like.
0
 
djhexAuthor Commented:
I found my stupid mistake

'%" & txtremitente.Text & "%'"""

Loot at all those '"" at the end

it should end in  "%' "
and no more.
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 6
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now