Sunday, February 19, 2012

How to use a variable in a SQLCommand?

Hi!

I need to know what can I do to use a variable in theWHEREcondition of the sqlcommand as I show you:

current_user = User.Identity.NameDim cmdAs New SqlCommand("SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = current_user", cn)
 
Obviously it doesn't work and I need your help. Thanks. 

Sorry this is not the question that I have, sorry.

The question is what can I do to use a variable(User.Identity.Name)in a SelectCommand that I use in a GridView, thanks again.

Something like that, but it doesn't work:

SelectCommand="SELECT id_fax, fecha,destino, propietario, contenido, estado, cola FROM fax WHERE(propietario = <%# User.Identity.Name %>)"

|||
try this, I think this is what you are looking for:
Dim cmdAs New SqlCommand("SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = "¤t_user, cn)
|||

Hi rubencdl,

You have to use String Concatination, the term String Concatination is use to combine two different strings. In your case you have two different string

first string containt the query

"SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = ' '"

and other contain the user name

current_user = User.Identity.Name

Now you have to combine these two string in a signle string, and for + operator is use for combination of two string

Dim cmdAs New SqlCommand("SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = ' '"

+ current_user + " ' " , cn)

Hope it works

Thank You,

Best Regards,

Muhammad Akhtar Shiekh

 
 
|||

Thanks to both of you.

But this is the answer for the first question, and I need to know the answer for the second, how can I do to use a variable in the SelectCommand of a GridView.

Thanks.

|||

Hi Dear,

I m really sorry dear, i can't understandt he meaning of "SelectCommand of a GridView."Confused

What do u mean by SelectCommand of a GridView? there is no Select Command Property in GridView...

can u please explain again what you really want? because string concatination will be same in each senario...

Thank You

Best Regards,

Muhammad Akhtar Shiekh

|||Sorry, is the SelectCommand of a SQLDataSourceEmbarrassed|||

rubencdl:

Sorry, is the SelectCommand of a SQLDataSourceEmbarrassed

try this in page_load event

SqlDataSource1.SelectCommand = "SELECT [id_usuario], [nombre], [apellidos], [telefono], [empresa] FROM [usuario] WHERE [id_usuario] = ' " + User.Identity.Name + " ' "

No comments:

Post a Comment