using stored procedure

V

vncntj

I have this syntax working perfectly, but I'm trying to apply a stored
procedure instead using
the CommandText (Sql Statement).

// Open connection to the database
string ConnectionString =
@"server=localhost;uid=;pwd=;trusted_connection=true;database=PMAEvents";

con = new SqlConnection(ConnectionString);
con.Open();


// Set up a command with the given query and associate
// this with the current connection.


string CommandText = "SELECT Locations, EventTitle FROM
specialevents WHERE Locations LIKE @Find";


cmd = new SqlCommand(CommandText);
cmd.Connection = con;


// Add LastName to the above defined parameter @Find
//cmd.Parameters.Add(new SqlParameter("@Find",
System.Data.SqlDbType.NVarChar, "Locations")); // The name of the
source column
cmd.Parameters.Add(new SqlParameter("@Find",
System.Data.SqlDbType.VarChar));


// Fill the parameter with the value retrieved
// from the text field
cmd.Parameters["@Find"].Value = txtFind.Text;


// Execute the query
rdr = cmd.ExecuteReader();
 
V

vncntj

I've tried this, (below) but I get 'storedProcCommand' does not exist



// Open connection to the database
string ConnectionString =
@"server=localhost;uid=intranet_content;pwd=content;trusted_connection=true;database=PMAEvents";
con = new SqlConnection(ConnectionString);
con.Open();

// Set up a command with the given query and associate
// this with the current connection.

cmd = new SqlCommand("ps_eventtest", con);
storedProcCommand.CommandType =
CommandType.StoredProcedure;
storedProcCommand.Parameters.Add("@Locations",
txtFind.Text);
cmd.Connection = con;

// Add LastName to the above defined parameter @Find
cmd.Parameters.Add(new SqlParameter("@Find",
System.Data.SqlDbType.NVarChar, "Locations")); // The name of the
source column

cmd.Parameters["@Find"].Value = txtFind.Text;

// Execute the query
rdr = cmd.ExecuteReader();
 
V

vncntj

Ok! I've got the page working but now I'm not getting any records
returned.

// Open connection to the database
string ConnectionString =
@"server=localhost;uid=intranet_content;pwd=content;trusted_connection=true;database=PMAEvents";
con = new SqlConnection(ConnectionString);
con.Open();

// Set up a command with the given query and associate
// this with the current connection.

cmd = new SqlCommand("ps_eventtest", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@Locations", txtFind.Text);
cmd.Connection = con;

// Add LastName to the above defined parameter @Find
cmd.Parameters.Add(new SqlParameter("@Find",
System.Data.SqlDbType.NVarChar)); // The name of the source column

cmd.Parameters["@Find"].Value = txtFind.Text;

// Execute the query
rdr = cmd.ExecuteReader();

I've tried this, (below) but I get 'storedProcCommand' does not exist



// Open connection to the database
string ConnectionString =
@"server=localhost;uid=intranet_content;pwd=content;trusted_connection=true;database=PMAEvents";
con = new SqlConnection(ConnectionString);
con.Open();

// Set up a command with the given query and associate
// this with the current connection.

cmd = new SqlCommand("ps_eventtest", con);
storedProcCommand.CommandType =
CommandType.StoredProcedure;
storedProcCommand.Parameters.Add("@Locations",
txtFind.Text);
cmd.Connection = con;

// Add LastName to the above defined parameter @Find
cmd.Parameters.Add(new SqlParameter("@Find",
System.Data.SqlDbType.NVarChar, "Locations")); // The name of the
source column

cmd.Parameters["@Find"].Value = txtFind.Text;

// Execute the query
rdr = cmd.ExecuteReader();



I have this syntax working perfectly, but I'm trying to apply a stored
procedure instead using
the CommandText (Sql Statement).

// Open connection to the database
string ConnectionString =
@"server=localhost;uid=;pwd=;trusted_connection=true;database=PMAEvents";

con = new SqlConnection(ConnectionString);
con.Open();


// Set up a command with the given query and associate
// this with the current connection.


string CommandText = "SELECT Locations, EventTitle FROM
specialevents WHERE Locations LIKE @Find";


cmd = new SqlCommand(CommandText);
cmd.Connection = con;


// Add LastName to the above defined parameter @Find
//cmd.Parameters.Add(new SqlParameter("@Find",
System.Data.SqlDbType.NVarChar, "Locations")); // The name of the
source column
cmd.Parameters.Add(new SqlParameter("@Find",
System.Data.SqlDbType.VarChar));


// Fill the parameter with the value retrieved
// from the text field
cmd.Parameters["@Find"].Value = txtFind.Text;


// Execute the query
rdr = cmd.ExecuteReader();
 
B

Bobbo

I've tried this, (below) but I get 'storedProcCommand' does not exist
cmd = new SqlCommand("ps_eventtest", con);
storedProcCommand.CommandType = CommandType.StoredProcedure;
storedProcCommand.Parameters.Add("@Locations", txtFind.Text);

That's because storedProcCommand has not been defined anywhere, and
also the parameter adding syntax appears to be incorrect.

Assuming you have 'cmd' defined elsewhere as a SqlCommand object, you
can change the second and third of the above lines to read as follows:

cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@Locations", txtFind.Text));

The following article is a good demonstration of the techniques
involved here.

http://www.codeproject.com/cs/database/albumviewer.asp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top