简体   繁体   English

将数据插入包含asp.net中的外键的表的SQL查询

[英]SQL query to insert data into a table containing foreign key in asp.net

This is the code which contains an add button which when clicked should add the data from textboxes to the bill table here crid , crname , cid , cname and truckid are foreign keys.这是一个包含了被点击时应该从文本框的数据添加到账单表在这里添加按钮的代码cridcrnamecidcnametruckid外键。

When the add button is clicked the data is not added inside the table and it shows no error hence I'm not really sure for what is causing this.单击添加按钮时,数据未添加到表中,并且没有显示错误,因此我不确定是什么原因造成的。

using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI;
    
namespace project
{
    public partial class bill : System.Web.UI.Page
    {
        string strcon = ConfigurationManager.ConnectionStrings["con"].ConnectionString;

        protected void Page_Load(object sender, EventArgs e)
        {
            GridView2.DataBind();
        }

        //add
        protected void Button1_Click(object sender, EventArgs e)
        {
            if (BillCheck())
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage",
                    "alert('This bill already exists. Please generate a new bill.')", true);
            }
            else
            {
                addNewBill();
            }
        }

        //user defined function
        void addNewBill()
        {
            if (TextBox1.Text.Trim().Equals(""))
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage",
                    "alert(GR no cannot be blank')", true);
            }
            else
            {
                try
                {
                    SqlConnection con = new SqlConnection(strcon);
                    if (con.State == ConnectionState.Closed)
                    {
                        con.Open();
                    }

                    SqlCommand cmd = new SqlCommand(
                        "insert into bill (GRNo,Date,from,to,crid,crname,cid,cname,package," +
                        "description,HSNcode,privatemark,invoiceno,value,truckid,paymentmode,actual,charged,amount)" +
                        " (@GRNo,@Date,@from,@to,(select crid from Consignor where crid='" + TextBox4.Text.Trim() +
                        "';)," +
                        "(select crname from Consignor where crname='" + TextBox5.Text.Trim() + "';)," +
                        "(select cid from Consignee where cid='" + TextBox8.Text.Trim() + "';)," +
                        "(select cname from Consignee where cname='" + TextBox9.Text.Trim() +
                        "';),@package,@description,@HSNcode,@privatemark," +
                        "@invoiceno,@value,@paymentmode,@actual,@charged,@amount);", con);
                    cmd.Parameters.AddWithValue("@GRNo", TextBox1.Text.Trim());
                    cmd.Parameters.AddWithValue("@Date", TextBox2.Text.Trim());
                    cmd.Parameters.AddWithValue("@from", TextBox7.Text.Trim());
                    cmd.Parameters.AddWithValue("@to", TextBox3.Text.Trim());
                    cmd.Parameters.AddWithValue("@crid", TextBox4.Text.Trim());
                    cmd.Parameters.AddWithValue("@crname", TextBox5.Text.Trim());
                    cmd.Parameters.AddWithValue("@cid", TextBox8.Text.Trim());
                    cmd.Parameters.AddWithValue("@cname", TextBox9.Text.Trim());
                    cmd.Parameters.AddWithValue("@package", TextBox6.Text.Trim());
                    cmd.Parameters.AddWithValue("@description", TextBox10.Text.Trim());
                    cmd.Parameters.AddWithValue("@HSNcode", TextBox11.Text.Trim());
                    cmd.Parameters.AddWithValue("@privatemark", TextBox12.Text.Trim());
                    cmd.Parameters.AddWithValue("@invoiceno", TextBox13.Text.Trim());
                    cmd.Parameters.AddWithValue("@value", TextBox14.Text.Trim());
                    cmd.Parameters.AddWithValue("@truckid", DropDownList1.SelectedItem.Value);
                    cmd.Parameters.AddWithValue("@paymentmode", DropDownList2.SelectedItem.Value);
                    cmd.Parameters.AddWithValue("@actual", TextBox21.Text.Trim());
                    cmd.Parameters.AddWithValue("@charged", TextBox22.Text.Trim());
                    cmd.Parameters.AddWithValue("@amount", TextBox23.Text.Trim());
                    cmd.ExecuteNonQuery();
                    con.Close();
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage",
                        "alert('New Bill added Successfully!')", true);
                    clearForm();
                    GridView2.DataBind();
                }
                catch (Exception ex)
                {
                    ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage",
                        "alert('" + ex.Message + "')", true);
                }
            }
        }

        public bool BillCheck()
        {
            try
            {
                SqlConnection con = new SqlConnection(strcon);
                if (con.State == ConnectionState.Closed)
                {
                    con.Open();
                }

                SqlCommand cmd = new SqlCommand("Select * from bill where GRNo='" + TextBox1.Text.Trim() + "';", con);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt.Rows.Count >= 1)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch (Exception ex)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage",
                    "alert('" + ex.Message + "')", true);
                return false;
            }
        }

        void clearForm()
        {
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox8.Text = "";
            TextBox9.Text = "";
            TextBox10.Text = "";
            TextBox11.Text = "";
            TextBox12.Text = "";
            TextBox13.Text = "";
            TextBox14.Text = "";
            TextBox21.Text = "";
            TextBox22.Text = "";
            TextBox23.Text = "";
            DropDownList1.SelectedValue = "";
            DropDownList2.SelectedValue = "";
        }
    }
}

I have tried writing the query with values and without it still isn't working.我试过用值编写查询,没有它仍然无法正常工作。 I have tried checking my database to see if rows are added i have tried removing the semi-colon from the select statement its still not working.我试过检查我的数据库以查看是否添加了行我试过从 select 语句中删除分号,但它仍然不起作用。 this is the image of the columns of my bill table.这是我的帐单表列的图像。

我认为你的 sql st 有问题,你不能像你一样使用 param,试试这个

SqlCommand cmd = new SqlCommand("insert into bill (GRNo,Date,from,to,crid,crname,cid,cname,package," +"description,HSNcode,privatemark,invoiceno,value,truckid,paymentmode,actual,charged,amount)" + " (@GRNo,@Date,@from,@to,(select crid from Consignor where crid=@crid;)," +"(select crname from Consignor where crname=@crname;)," +"(select cid from Consignee where cid=@cid;)," +"(select cname from Consignee where cname=@cname;),@package,@description,@HSNcode,@privatemark," + "@invoiceno,@value,@paymentmode,@actual,@charged,@amount);", con);

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

相关问题 带有ASP.Net后端的Azure移动应用程序使用外键将数据发布到SQL Server表 - Azure Mobile App with ASP.Net backend posting data to a SQL Server table with a foreign key 使用Javascript和ASP.NET将数据插入SQL表 - Insert Data into SQL table using Javascript and ASP.NET 如何使用ASP.NET MVC中的Entity Framework将记录插入到具有外键的表中 - How to insert a record into a table with a foreign key using Entity Framework in ASP.NET MVC 使用具有外键的表将数据从asp.net页面插入到我的数据库中 - insert data from asp.net page to my database with tables which have foreign key 如何使用 c# asp.net 将外键数据插入数据库 - How to insert foreign key data into database using c# asp.net ASP.NET插入语句与外键约束冲突 - ASP.NET the insert statement conflicted with the foreign key constraint 使用外键表列显示数据 asp.net core mvc c# - Use foreign key table columns to display data asp.net core mvc c# 如何使用asp.net获取SQL Server外键中的数据? - How to get data in a SQL Server foreign key using asp.net? 如何使用asp.net实体框架在SQL外键中获取数据? - How to get data in an sql foreign key using asp.net entity framework? asp.net EF 核心 MVC 无法访问来自第三个外键表的数据 - asp.net EF core MVC cant access data from third foreign key table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM