简体   繁体   中英

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.

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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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