I am passing memid value in sql string and getting error. How should I write below query to get the value in memid in sql string
function getdetails(string memid )
{
string cmd = @"SELECT c.contact_gid, c.Last_Name + ', ' + c.first_name as name
FROM dbo.Demo c inner join dbo.contact d
on c.id = d.id
AND e.Issued_Member_ID = memid";
}
function getdetails(string memid )
{
string cmd = @"SELECT c.contact_gid, c.Last_Name + ', ' + c.first_name as name
FROM dbo.Demo c inner join dbo.contact d
on c.id = d.id
AND e.Issued_Member_ID = '" + memid +"'";
}
What is the value of memid? Is it a number represented as a string or really a string? If it is a string, you will need to wrap it in apostrophes in your sql statement like this
e.Issued_Member_ID = '" + memid + "'";
like you did in your second attempt. What specific error are you getting?
"What I hear, I forget; What I see, I remember; What I do, I understand." --Confucius
Remeber to Mark as Answer if this post helped you.
it's bad to use your current way to pass parameter
ref something like below:
using (SqlCommand command = new SqlCommand("SELECT * FROM Dogs1 WHERE Name LIKE @Name", connection))
{
// // Add new SqlParameter to the command. //
command.Parameters.Add(new SqlParameter("Name", dogName));
// // Read in the SELECT results. //
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int weight = reader.GetInt32(0);
string name = reader.GetString(1);
string breed = reader.GetString(2);
Console.WriteLine("Weight = {0}, Name = {1}, Breed = {2}", weight, name, breed);
}
}
function getdetails(string memid)
{
string cmd = @"SELECT c.contact_gid, (c.Last_Name + ', ' + c.first_name) as name
FROM dbo.Demo c inner join dbo.contact d
on c.id = d.id
WHERE d.id = '" + memid +"'";
}
RachelP
Member
1 Points
3 Posts
how to pass value of a varibale in SQL string
Dec 19, 2012 09:13 PM|LINK
I am passing memid value in sql string and getting error. How should I write below query to get the value in memid in sql string
function getdetails(string memid )
{
string cmd = @"SELECT c.contact_gid, c.Last_Name + ', ' + c.first_name as name
FROM dbo.Demo c inner join dbo.contact d
on c.id = d.id
AND e.Issued_Member_ID = memid";
}
function getdetails(string memid )
{
string cmd = @"SELECT c.contact_gid, c.Last_Name + ', ' + c.first_name as name
FROM dbo.Demo c inner join dbo.contact d
on c.id = d.id
AND e.Issued_Member_ID = '" + memid +"'";
}
both doesn't work. Please help.
grundebar
Contributor
4515 Points
726 Posts
Re: how to pass value of a varibale in SQL string
Dec 19, 2012 09:43 PM|LINK
What is the value of memid? Is it a number represented as a string or really a string? If it is a string, you will need to wrap it in apostrophes in your sql statement like this
like you did in your second attempt. What specific error are you getting?
Remeber to Mark as Answer if this post helped you.
kevinstone
Member
236 Points
48 Posts
Re: how to pass value of a varibale in SQL string
Dec 19, 2012 11:21 PM|LINK
it's bad to use your current way to pass parameter
ref something like below:
using (SqlCommand command = new SqlCommand("SELECT * FROM Dogs1 WHERE Name LIKE @Name", connection)) { // // Add new SqlParameter to the command. // command.Parameters.Add(new SqlParameter("Name", dogName)); // // Read in the SELECT results. // SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { int weight = reader.GetInt32(0); string name = reader.GetString(1); string breed = reader.GetString(2); Console.WriteLine("Weight = {0}, Name = {1}, Breed = {2}", weight, name, breed); } }oned_gk
All-Star
31007 Points
6347 Posts
Re: how to pass value of a varibale in SQL string
Dec 19, 2012 11:51 PM|LINK
function getdetails(string memid) { string cmd = @"SELECT c.contact_gid, (c.Last_Name + ', ' + c.first_name) as name FROM dbo.Demo c inner join dbo.contact d on c.id = d.id WHERE d.id = '" + memid +"'"; }