问题描述:

I'm creating a simple form inserting data into SQL Server 2008 Express. I'm using a master page and make connection string by Ado.net entity data.

Data is not inserting. It just giving me Catch error ..

Please check my code is it any problem how to fix it

Connection string

<connectionStrings>

<add name="dbcheckingEntities"

connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=ABDULSAMAD-PC\SQLEXPRESS;initial catalog=dbchecking;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"

providerName="System.Data.EntityClient" />

<add name="dbcheckingConnectionString"

connectionString="Data Source=ABDULSAMAD-PC\SQLEXPRESS;Initial Catalog=dbchecking;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"

providerName="System.Data.SqlClient" />

</connectionStrings>

Code

protected void BtnSubmit_Click(object sender, EventArgs e)

{

SqlConnection Con = new SqlConnection();

try

{

Con.ConnectionString = ConfigurationManager.ConnectionStrings["dbcheckingEntities"].ToString();

Con.Open();

string insertquery = "insert into userTable (firstname, lastname,email,password,gender,dob,regdatetime)values(@fname ,@lname,@email,@password,@gender,@dob,getdate())";

SqlCommand com = new SqlCommand(insertquery, Con);

com.Parameters.AddWithValue("@fname", txtFirstName.Text);

com.Parameters.AddWithValue("@lname", txtLastName.Text);

com.Parameters.AddWithValue("@email", txtEmail.Text);

com.Parameters.AddWithValue("@password", txtPassword.Text);

com.Parameters.AddWithValue("@gender", RdoGender.SelectedValue);

com.Parameters.AddWithValue("@dob", txtPickDate.Text);

com.ExecuteNonQuery();

Label1.Text = "User is added";

}

catch (Exception ex)

{

Label1.Text = "Error:";

}

finally

{

Con.Close();

}

}

相关阅读:
Top