Beep
Beep

Reputation: 2823

asp.net c# data to a database

I have been at this for two days now with no luck. The problem is I am trying to input data into my sql server database via a web form. every time I try to run I am getting errors .

Bellow is the Error I am Getting when I run the code Error Image

this is the code for the web form

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;

namespace WebApplication1
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            SqlConnection cs = new SqlConnection ("Data Source = SQLEXSPRESS; Initial Catalog = OMS; Integrated Security = true");
            SqlDataAdapter da = new SqlDataAdapter ();
            da.InsertCommand = new SqlCommand("INSERT INTO Customer tbl (FirstName,LastName) Customer VALUES (@FirstName,@LastName)", cs);
            da.InsertCommand.Parameters.Add("@FirstName", SqlDbType.VarChar).Value = firstname.Text;
            da.InsertCommand.Parameters.Add("@LastName", SqlDbType.VarChar).Value = lastname.Text;

            cs.Open();
            da.InsertCommand.ExecuteNonQuery(); // Error occurs here
            cs.Close();
        }

        protected void firstname_TextChanged(object sender, EventArgs e)
        {

        }

        protected void lastname_TextChanged(object sender, EventArgs e)
        {

        }
    }
}

This is my database Table code

CREATE TABLE [dbo].[Customer] (
[CustomerID] INT          IDENTITY (1, 1) NOT NULL,
[FirstName]  VARCHAR (50) NULL,
[LastName]   VARCHAR (50) NULL,
[Address]    VARCHAR (50) NOT NULL,
[City]       VARCHAR (25) NOT NULL,
[Postcode]   VARCHAR (10) NOT NULL,
[Country]    VARCHAR (50) NOT NULL,
[Modified]   ROWVERSION   NOT NULL,
PRIMARY KEY CLUSTERED ([CustomerID] ASC)

);

Any help will be greatly appreciated.

Upvotes: 1

Views: 577

Answers (1)

Andrew
Andrew

Reputation: 2335

SqlConnection cs = new SqlConnection ("Data Source = SQLEXSPRESS; Initial Catalog = OMS; Integrated Security = true");

Should be

SqlConnection cs = new SqlConnection ("Data Source = SQLEXPRESS; Initial Catalog = OMS; Integrated Security = true");

You spelt express wrong!

You might also need to use Data Source = .\SQLEXPRESS

Upvotes: 3

Related Questions