简体   繁体   English

数据未插入C#数据库中

[英]data in not insert in database in c#

I'm new to C# and I was trying to insert data into database with this code. 我是C#的新手,我正尝试使用此代码将数据插入数据库。

Please help me how can I fix this error which occurs on cmd.ExecuteNonQuery when i try to insert the data and click to submit: 请帮我解决在尝试插入数据并单击提交时在cmd.ExecuteNonQuery上出现的错误:

ExecutenonQueryexception cant handle by the system.data.dll ExecutenonQueryException无法由system.data.dll处理

How can I solve this error? 我该如何解决这个错误?

<connectionStrings>
    <add name="ConnectionString" 
         connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\adityaDB.mdf;Integrated Security=True;User Instance=True" 
         providerName="System.Data.SqlClient"/>
</connectionStrings>

asp code .. 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>&nbsp;</td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td align="left" valign="middle">&nbsp;</td>
                                <td>&nbsp;</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>&nbsp;</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>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td align="left">&nbsp;</td>
                                <td>&nbsp;</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>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td>&nbsp;</td>
                                <td align="left">&nbsp;</td>
                                <td>&nbsp;</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 &amp; 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">&nbsp;</td>
                                <td colspan="3">
                                    <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="Button1_Click" />
                                    &nbsp;
                            <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# code... 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();
            }
        }
    }

For SqlCommand to execute you need an open SqlConnection . 为了执行SqlCommand ,您需要打开SqlConnection Opening a connection on page load on a local variables does not help. 在页面上打开连接以加载局部变量没有帮助。 Also, keeping an instance of SqlConnection and SqlCommand on the page level is probably not the best idea. 此外,在页面级别上保留SqlConnection和SqlCommand的实例可能也不是最好的主意。 Your button click should have it's own SqlConnection, and since it's an IDisposable it should be used inside a using statement: 您的按钮单击应该具有自己的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();
        }
    }
}

Remove the page level SqlCommand and SqlConnection , and remove the opening of the local SqlConnection in the Page_Load . 删除页面级别SqlCommandSqlConnection ,并删除Page_Load中的本地SqlConnection

I suspect your object con is wrong in below line. 我怀疑您的对象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);

be sure to set it before creating command object. 确保在创建命令对象之前进行设置。

string con = ConfigurationManager.ConnectionStrings["adityaDB"].ConnectionString;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM