Amarundo
Amarundo

Reputation: 2397

How to Call SQL Server 2008 Stored Procedure from CSHTML page

From the question you may realize I'm not very versed in this. This is actually my first time.

I have an HTML page with a form that asks for a phone number. When the user clicks on the submit button, it goes to a CSHTML page that takes the phone number from the form by doing this:

@{
string pn = Request.Form["pn"]
}

Then it has to call a stored procedure with that value (pn) as a parameter.

Stored procedure name: sp_AddPNtoDNC

Server: SqlSrv

Username: insusr

PW: whatever

Please, don't assume I know anything!

Oh, one more thing. I'm doing all this from a recently installed WebMatrix.

Thanks.

Upvotes: 2

Views: 3202

Answers (2)

Mike Brind
Mike Brind

Reputation: 30110

If you want to do this the "WebMatrix way", you would use the Database helper.

First you need to add a connection string to your web.config file.

<configuration>
    <connectionStrings>
        <add name="myConnection" connectionString="Server=myServerAddress;Database=myDataBase;User Id=myUsername;Password=myPassword;" providerName="System.Data.SqlClient" />
    </connectionStrings>
</configuration>

Then at the top of the cshtml file:

var db = Database.Open("myConnection");
db.Execute("EXEC sp_AddPNtoDNC @0", Request.Form["pn"]);

Upvotes: 2

Veeesss
Veeesss

Reputation: 83

conn = new SqlConnection("CONNECTION STRING");
        conn.Open();

        SqlCommand cmd  = new SqlCommand("sp_AddPNtoDNC", conn);

        cmd.CommandType = CommandType.StoredProcedure;

        cmd.Parameters.Add(new SqlParameter("@pn", pn));

        reader = cmd.ExecuteReader();

You can try something like this.

I hope this help you.

Upvotes: 0

Related Questions