Friday, March 30, 2012

problem with SQLConnection

I am working on a set of webforms that insert user data into a set of db tables.

I set up a test of an approach using northwind and I'm having trouble getting the insert to work. When I open the form, input the name and phone, and submit there is no error, but no record inserted into the Shippers table.

You can see one of my approaches in the ASPX code. I don't like having to do the select in order to do the insert -- so that's commented off.

I'm stuck. Thoughts about what I'm missing appreciated...

Ray

ASPX code.

<%@.PageLanguage="C#"AutoEventWireup="true"CodeFile="Default66a.aspx.cs"Inherits="pages_audit_Default66a" %>

<!DOCTYPEhtmlPUBLIC"-//W3C//DTD XHTML 1.0 Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<htmlxmlns="http://www.w3.org/1999/xhtml">

<headid="Head1"runat="server">

<title>Untitled Page</title>

</head>

<body>

<formid="form1"runat="server">

CompanyName:

<asp:textboxid="txtCompanyName"runat="server"/><br/>

Phone:

<asp:textboxid="txtPhone"runat="server"/><br/>

<br/>

<asp:buttonid="btnSubmit"runat="server"text="Submit"onclick="btnSubmit_Click"/>

<br/>

<br/>

<br/>

<br/>

<asp:LabelID="awesomelbl"runat="server"Text="Label"></asp:Label><br/>

<br/>

<!--

<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:NorthwindConnectionString %>"

insertcommand="INSERT INTO Shippers(CompanyName, Phone) VALUES (@.CompanyName, @.Phone)" ProviderName="System.Data.SqlClient" SelectCommand="SELECT * FROM [Shippers]">

<insertparameters>

<asp:controlparameter controlid="txtCompanyName" name="CompanyName" />

<asp:controlparameter controlid="txtPhone" name="Phone" />

</insertparameters>

</asp:sqldatasource>

-->

</form>

</body>

</html>

c Sharp code

using System;

using System.Data;

using System.Data.SqlClient;

using System.Data.Sql;

using System.Configuration;

using System.Collections;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Web.UI.HtmlControls;

publicpartialclasspages_audit_Default66a : System.Web.UI.Page

{

protectedvoid Page_Load(object sender,EventArgs e)

{

}

protectedvoid btnSubmit_Click(object sender,EventArgs e)

{

SqlConnection con =newSqlConnection("Data Source=Chilibowl;Trusted_Connection=yes;DataBase=Northwind");

SqlCommand cmd =newSqlCommand("INSERT INTO [Shippers] ([CompanyName], [Phone]) VALUES (@.CompanyName, @.Phone)");

SqlParameter cnameparam =newSqlParameter("@.CompanyName", txtCompanyName.Text);SqlParameter phnparam =newSqlParameter("@.Phone", txtPhone.Text);

cmd.Parameters.Add(cnameparam);

cmd.Parameters.Add(phnparam);

try

{

con.Open();

if (cmd.ExecuteNonQuery() > 0)awesomelbl.Text ="successful insert";

}

catch

{

//handel

}

finally

{

con.Close();

}

}

}

rkobs:

SqlCommand cmd =newSqlCommand("INSERT INTO [Shippers] ([CompanyName], [Phone]) VALUES (@.CompanyName, @.Phone)");

Try this:

SqlCommand cmd =newSqlCommand("INSERT INTO [Shippers] ([CompanyName], [Phone]) VALUES (@.CompanyName, @.Phone)",con);

|||

Worked. Thanks.

Ray

sql

No comments:

Post a Comment