[英]data in not insert in database in c#
我是C#的新手,我正嘗試使用此代碼將數據插入數據庫。
請幫我解決在嘗試插入數據並單擊提交時在cmd.ExecuteNonQuery
上出現的錯誤:
ExecutenonQueryException無法由system.data.dll處理
我該如何解決這個錯誤?
<connectionStrings>
<add name="ConnectionString"
connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\adityaDB.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient"/>
</connectionStrings>
ASP代碼 ..
<%@ Page Title="" Language="C#" MasterPageFile="~/AGD.Master" AutoEventWireup="true" CodeBehind="ConsumerRegistration.aspx.cs" Inherits="Aditya_Gas_Distributor.ConsumerRegistration" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div>
<center>
<br />
<form runat="server">
<table style="border: ridge solid #5D7B9D; background-color: Window; width: 665px;">
<tr>
<td colspan="6" align="center" style="background-color: #C1312F">
<b style="color: White">Gas Connection Registration Form </b>
</td>
</tr>
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td>FirstName<samp style="color:red">*</samp></td>
<td>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
<td>
</td>
<td colspan="3" rowspan="4">
<asp:FileUpload ID="FileUp" runat="server" />
<asp:Image ID="Image1" BorderStyle="Dashed" BorderWidth="1" runat="server" Height="100px" Width="133px"/>
</td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
<tr>
<td>MiddleName<samp style="color:red">*</samp></td>
<td>
<asp:TextBox ID="txtMName" runat="server"></asp:TextBox></td>
<td></td>
</tr>
<tr>
<td colspan="3"></td>
</tr>
<tr>
<td>LastName<samp style="color:red">*</samp></td>
<td>
<asp:TextBox ID="txtLName" runat="server"></asp:TextBox>
</td>
<td></td>
<td>Gender<samp style="color:red">*</samp></td>
<td align="left">
<asp:DropDownList runat="server" ID="ddl1">
<asp:ListItem>--Select One--</asp:ListItem>
<asp:ListItem>Male</asp:ListItem>
<asp:ListItem>Female</asp:ListItem>
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td>DOB<samp style="color:red">*</samp>
</td>
<td>
<asp:TextBox ID="txtDOB" runat="server" TextMode="Date"></asp:TextBox>
</td>
<td>
</td>
<td>Connection Type<samp style="color:red">*</samp></td>
<td valign="middle" align="left">
<asp:DropDownList ID="ddlConnection" runat="server" Height="22px" Width="107px">
<asp:ListItem>--Select One--</asp:ListItem>
<asp:ListItem>Domestic</asp:ListItem>
<asp:ListItem>Commercial</asp:ListItem>
</asp:DropDownList>
</td>
<td> </td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td align="left" valign="middle"> </td>
<td> </td>
</tr>
<tr>
<td>State Name<samp style="color:red">*</samp></td>
<td>
<asp:DropDownList ID="ddlstate" runat="server" Width="157px">
<asp:ListItem Selected="True">--Select One--</asp:ListItem>
<asp:ListItem>Andaman and Nicobar Islands</asp:ListItem>
<asp:ListItem>Andhra Pradesh</asp:ListItem>
<asp:ListItem>Arunachal Pradesh</asp:ListItem>
<asp:ListItem>Assam</asp:ListItem>
<asp:ListItem>Bihar</asp:ListItem>
<asp:ListItem>Chandigarh</asp:ListItem>
<asp:ListItem>Chattisgarh</asp:ListItem>
<asp:ListItem>Dadra and Nagar Haveli</asp:ListItem>
<asp:ListItem>Daman and Diu</asp:ListItem>
<asp:ListItem>Delhi</asp:ListItem>
<asp:ListItem>Goa</asp:ListItem>
<asp:ListItem>Gujarat</asp:ListItem>
<asp:ListItem>Haryana</asp:ListItem>
<asp:ListItem>Himachal Pradesh</asp:ListItem>
<asp:ListItem>Jammu and Kashmir</asp:ListItem>
<asp:ListItem>Jharkhand</asp:ListItem>
<asp:ListItem>Karnataka</asp:ListItem>
<asp:ListItem>Kerala</asp:ListItem>
<asp:ListItem>Lakshadweep</asp:ListItem>
<asp:ListItem>Madhya Pradesh</asp:ListItem>
<asp:ListItem>Maharashtra</asp:ListItem>
<asp:ListItem>Manipur</asp:ListItem>
<asp:ListItem>Meghalaya</asp:ListItem>
<asp:ListItem>Mizoram</asp:ListItem>
<asp:ListItem>Nagaland</asp:ListItem>
<asp:ListItem>Orissa</asp:ListItem>
<asp:ListItem>Pondicherry</asp:ListItem>
<asp:ListItem>Punjab</asp:ListItem>
<asp:ListItem>Rajasthan</asp:ListItem>
<asp:ListItem>Sikkim</asp:ListItem>
<asp:ListItem>Tamil Nadu</asp:ListItem>
<asp:ListItem>Tripura</asp:ListItem>
<asp:ListItem>Uttarakhand</asp:ListItem>
<asp:ListItem>Uttaranchal</asp:ListItem>
<asp:ListItem>Uttar Pradesh</asp:ListItem>
<asp:ListItem>West Bengal</asp:ListItem>
</asp:DropDownList>
</td>
<td> </td>
<td>
<asp:Label ID="lblDistrict" runat="server" Text="District Name"></asp:Label>
</td>
<td align="left"><samp style="color:red">*</samp>
<asp:DropDownList ID="ddldistrict" runat="server">
<asp:ListItem>aa</asp:ListItem>
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td align="left"> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:Label ID="lblCity" runat="server" Text="City Name"></asp:Label>
</td>
<td><samp style="color:red">*</samp>
<asp:DropDownList ID="ddl_city" runat="server">
<asp:ListItem>aa</asp:ListItem>
</asp:DropDownList>
</td>
<td>
</td>
<td>
<asp:Label ID="lblAgency" runat="server" Text="Agency Name"></asp:Label>
</td>
<td align="left">
<samp style="color:red">*</samp>
<asp:DropDownList ID="ddl_agency" runat="server">
<asp:ListItem>aa</asp:ListItem>
</asp:DropDownList>
</td>
<td>
</td>
</tr>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<td align="left"> </td>
<td> </td>
</tr>
<tr>
<td>Email Id<samp style="color:red">*</samp></td>
<td>
<asp:TextBox ID="txtEmail" runat="server"></asp:TextBox></td>
<td>
</td>
<td>PhoneNo<samp style="color:red">*</samp></td>
<td align="left">
<asp:TextBox ID="txtPhone" runat="server"></asp:TextBox></td>
<td>
</td>
</tr>
<tr>
<td colspan="6"></td>
</tr>
<tr>
<td>User Name<samp style="color:red">*</samp></td>
<td>
<asp:TextBox ID="txtUserName" runat="server"></asp:TextBox>
</td>
<td>
</td>
<td rowspan="2">Address<samp style="color:red">*</samp>
</td>
<td rowspan="2" align="left">
<asp:TextBox ID="txtAddress" TextMode="MultiLine" runat="server" Height="80px"
Width="176px"></asp:TextBox>
</td>
<td rowspan="2">
</td>
</tr>
<tr>
<td style="height: 62px">Password<samp style="color:red">*</samp></td>
<td style="height: 62px">
<asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox>
</td>
<td style="height: 62px">
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtPassword" ControlToValidate="cnfrmpass" ErrorMessage="CompareValidator" ForeColor="#CC3300">Password & Confirm Password Must be Same</asp:CompareValidator>
</td>
</tr>
<tr>
<td>Confirm Password<samp style="color:red">*</samp></td>
<td>
<asp:TextBox ID="cnfrmpass" runat="server" TextMode="Password"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="6">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</td>
</tr>
<tr>
<td colspan="3"> </td>
<td colspan="3">
<asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="cancle" />
</td>
</tr>
<tr>
<td colspan="6" style="background-color: #C1312F"></td>
</tr>
</table>
</form>
</center>
</div>
</asp:Content>
C#代碼...
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.Configuration;
using System.Web.Configuration;
namespace Aditya_Gas_Distributor
{
public partial class ConsumerRegistration : System.Web.UI.Page
{
// SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
}
protected void Button1_Click(object sender, EventArgs e)
{
//string cs = ConfigurationManager.ConnectionStrings["adityaDB"].ConnectionString;
Label1.Text = "Connected";
SqlCommand cmd = new SqlCommand("INSERT INTO tbl_UserRegistration (UserName, Password, FirstName, MName, LastName, Gender, DOB, State, District, City, ConnectionType, AgencyName, Address, PhoneNo, Email, Image, FileName)" +
"VALUES(@UserName, @Password, @FirstName, @MName, @LastName, @Gender, @DOB, @State, @District, @City, @ConnectionType, @AgencyName, @Address, @PhoneNo, @Email, @Image, @FileName)", con);
cmd.Parameters.AddWithValue("@UserName",txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
cmd.Parameters.AddWithValue("@FirstName", txtName.Text);
cmd.Parameters.AddWithValue("@LastName", txtMName.Text);
cmd.Parameters.AddWithValue("@Gender", ddl1.SelectedItem.Value);
cmd.Parameters.AddWithValue("@DOB", txtDOB.Text);
cmd.Parameters.AddWithValue("@State", ddlstate.SelectedItem.Value);
cmd.Parameters.AddWithValue("@District", ddldistrict.SelectedItem.Value);
cmd.Parameters.AddWithValue("@City", ddl_city.SelectedItem.Value);
cmd.Parameters.AddWithValue("@ConnectionType", ddlConnection.SelectedItem.Value);
cmd.Parameters.AddWithValue("@AgencyName", ddl_agency.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@PhoneNo", txtPhone.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@Image",Image1.ImageUrl);
cmd.Parameters.AddWithValue("@FileName", FileUp.PostedFile.FileName);
cmd.ExecuteNonQuery();
}
}
}
為了執行SqlCommand
,您需要打開SqlConnection
。 在頁面上打開連接以加載局部變量沒有幫助。 此外,在頁面級別上保留SqlConnection和SqlCommand的實例可能也不是最好的主意。 您的按鈕單擊應該具有自己的SqlConnection,並且由於它是IDisposable,因此應在using語句內使用:
protected void Button1_Click(object sender, EventArgs e)
{
Label1.Text = "Connected";
using (var con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
{
using (var cmd = new SqlCommand("INSERT INTO tbl_UserRegistration (UserName, Password, FirstName, MName, LastName, Gender, DOB, State, District, City, ConnectionType, AgencyName, Address, PhoneNo, Email, Image, FileName)" +
"VALUES(@UserName, @Password, @FirstName, @MName, @LastName, @Gender, @DOB, @State, @District, @City, @ConnectionType, @AgencyName, @Address, @PhoneNo, @Email, @Image, @FileName)", con))
{
cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
cmd.Parameters.AddWithValue("@FirstName", txtName.Text);
cmd.Parameters.AddWithValue("@LastName", txtMName.Text);
cmd.Parameters.AddWithValue("@Gender", ddl1.SelectedItem.Value);
cmd.Parameters.AddWithValue("@DOB", txtDOB.Text);
cmd.Parameters.AddWithValue("@State", ddlstate.SelectedItem.Value);
cmd.Parameters.AddWithValue("@District", ddldistrict.SelectedItem.Value);
cmd.Parameters.AddWithValue("@City", ddl_city.SelectedItem.Value);
cmd.Parameters.AddWithValue("@ConnectionType", ddlConnection.SelectedItem.Value);
cmd.Parameters.AddWithValue("@AgencyName", ddl_agency.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Address", txtAddress.Text);
cmd.Parameters.AddWithValue("@PhoneNo", txtPhone.Text);
cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
cmd.Parameters.AddWithValue("@Image", Image1.ImageUrl);
cmd.Parameters.AddWithValue("@FileName", FileUp.PostedFile.FileName);
con.Open();
cmd.ExecuteNonQuery();
}
}
}
刪除頁面級別SqlCommand
和SqlConnection
,並刪除Page_Load
中的本地SqlConnection
。
我懷疑您的對象con
在下一行中是錯誤的。
SqlCommand cmd = new SqlCommand("INSERT INTO tbl_UserRegistration (UserName, Password, FirstName, MName, LastName, Gender, DOB, State, District, City, ConnectionType, AgencyName, Address, PhoneNo, Email, Image, FileName)" +
"VALUES(@UserName, @Password, @FirstName, @MName, @LastName, @Gender, @DOB, @State, @District, @City, @ConnectionType, @AgencyName, @Address, @PhoneNo, @Email, @Image, @FileName)", con);
確保在創建命令對象之前進行設置。
string con = ConfigurationManager.ConnectionStrings["adityaDB"].ConnectionString;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.