简体   繁体   English

'System.Data.SqlClient.SqlException'

[英]'System.Data.SqlClient.SqlException'

I'm currently receiving this error:'System.Data.SqlClient.SqlException' on this line: 我目前在此行上收到此错误:“ System.Data.SqlClient.SqlException”:

int MemberExist = (int)check_Member.ExecuteScalar();

Here's my exception: http://imgur.com/bnp7OcT 这是我的例外: http : //imgur.com/bnp7OcT

I'm trying to open a connection to a database and retrieve data from a text box and see if ID matches a first name and last name in the database. 我正在尝试打开与数据库的连接,并从文本框中检索数据,并查看ID是否与数据库中的名字和姓氏匹配。 If it does then continues to execute code that writes to a PDF file, and if not it enters certain code from the text boxes into the database. 如果是这样,那么它将继续执行写入PDF文件的代码,如果不是,它将从文本框中输入某些代码到数据库中。 I'm sure there are other errors in my code and it would be great if somebody could help with that but currently I'm only asking for help on the error above, here is what I have so far: 我敢肯定我的代码中还有其他错误,如果有人可以提供帮助,那将是很好的选择,但是目前我只是在寻求有关上述错误的帮助,这就是我到目前为止所拥有的:

       protected void btnSubmit_Click(object sender, EventArgs e)
        {


            string PostCode = txtPostcode.Text;
            string PostCode2 = txtDestinationPostcode.Text;

            // Get the connection 
            SqlConnection DBConnection = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=""E:\DS\Prac5\Part1\App_Data\MyDatabase.mdf"";Integrated Security=True");
            SqlDataReader Reader = null;



            DBConnection.Open();
            SqlCommand check_Member = new SqlCommand("SELECT * FROM Members WHERE MembershipID = FirstName AND LastName = @Txtnput)", DBConnection);
            check_Member.Parameters.AddWithValue("@Txtnput", txtMembershipid.Text);

          //THIS IS THE LINE BELOW I'm HAVING ISSUES WITH

            int MemberExist = (int)check_Member.ExecuteScalar();

            if (MemberExist > 0)
            {
                // if Member exists

                PostCodeInfo MatchingPostCode = Find(PostCodeList, PostCode);
                if (MatchingPostCode == null)
                {
                    lblOutput.Text = "Your postcode could not be found!";

                }
                else
                {
                    lblOutput.Text = string.Format("({0},{1})", MatchingPostCode.Locality, MatchingPostCode.State);

                }


                PostCodeInfo MatchingPostCode2 = Findtwo(PostCodeList, PostCode2);
                if (MatchingPostCode2 == null)
                {
                    lblOutput2.Text = "Your postcode could not be found!";

                }
                else
                {
                    lblOutput2.Text = string.Format("({0},{1})", MatchingPostCode2.Locality, MatchingPostCode2.State);

                }

                // Put Code here if member text = to member ID 
                // If not insert member into database


                //We keep track of our invoice number by storing it in a plain text file. Using only a variable in memory means that it resets every time our web server turns off!
                string NewInvoiceNumber = GenerateNextInvoiceNumber(InvoiceFilePath);

                //Creating a document that will be stored on the web server's hard drive
                Document MyDoc = new Document();
                string PDFPath = Server.MapPath("PDFs");
                //Name each of our PDF files uniquely using the invoice number so we don't overwrite them.
                FileStream LocalStream = new FileStream(string.Format(@"{0}\Receipt-{1}.pdf", PDFPath, NewInvoiceNumber), FileMode.Create);
                PdfWriter.GetInstance(MyDoc, LocalStream);

                //Also store the document in memory such that we can send it to the client as an array of bytes
                MemoryStream HTTPStream = new MemoryStream();
                PdfWriter.GetInstance(MyDoc, HTTPStream);

                //Creating the PDF
                MyDoc.Open();
                //Create the custom fonts we'll use in the PDF
                Font Blue = new Font(Font.FontFamily.TIMES_ROMAN, 20f, Font.NORMAL, new BaseColor(System.Drawing.Color.Blue));
                Font Grey = new Font(Font.FontFamily.TIMES_ROMAN, 12f, Font.NORMAL, new BaseColor(System.Drawing.Color.Gray));
                Font Black = new Font(Font.FontFamily.TIMES_ROMAN, 14f, Font.NORMAL, new BaseColor(System.Drawing.Color.Black));

                //We create our Title paragraph seperately because we need to specify the text alignment in order to put it in the center.
                Paragraph Title = new Paragraph("Truck delivery receipt\n", Blue);
                Title.Alignment = Element.ALIGN_CENTER;
                MyDoc.Add(Title);

                MyDoc.Add(new Paragraph(string.Format("\nProccessed on " + DateTime.Now.ToString("dd/MM/yyyy h:mm:tt."), Black)));
                MyDoc.Add(new Paragraph(string.Format("\nThank you for doing business with Truck Deliveries Co., {0} {1}", txtName.Text, txtLastName.Text)));
                MyDoc.Add(new Paragraph(string.Format("Your invoice number is {0}. ", NewInvoiceNumber, Black)));
                MyDoc.Add(new Paragraph(string.Format("Please quote this number if you contact support.\n")));
                MyDoc.Add(new Paragraph(string.Format("\n")));
                Paragraph Heading = new Paragraph("Delivery details\n", Blue);
                MyDoc.Add(Heading);
                MyDoc.Add(new Paragraph(string.Format("\n")));
                MyDoc.Add(new Paragraph(string.Format("Your delivery will arrive at {0}, after leaving for delivery at {1} ", txtDeliveryDate.Text, DateTime.Now.ToString("dd/MM/yyyy h:mm:tt."))));
                MyDoc.Add(new Paragraph(string.Format("\n")));
                MyDoc.Add(new Paragraph(string.Format("{0} truck delivery from {1}, {2} {3} to {4}, {5} {6}.", txtNumberoftrucks.Text, txtDeliveryAddress.Text, txtPostcode.Text, lblOutput.Text, txtDestinationDeliveryAddress.Text, txtDestinationPostcode.Text, lblOutput2.Text)));
                MyDoc.Add(new Paragraph(string.Format("\n")));
                MyDoc.Add(new Paragraph(string.Format("Cost of delivery:$19,830.00.")));
                MyDoc.Add(new Paragraph(string.Format("Cost of insurance:$289.87.")));
                MyDoc.Add(new Paragraph(string.Format("Cost charged (incl. GST):$22102.87. ")));
                MyDoc.Add(new Paragraph(string.Format("\n")));
                MyDoc.Add(new Paragraph(string.Format("Any queries regarding your delivery can be sent to TruckDeliver@Trucks.com, or alternatively you can call 03 9876 5432.")));
                MyDoc.Add(new Paragraph(string.Format("\n")));
                MyDoc.Add(new Paragraph(string.Format("Our business is located at 72 Kaolin Street, VIC, AU. The Fees from this truck delivery will be charged to {0}. We will contact you via the phone number 03 9876 1234 if we experience any issues with yourt order.", txtBillingAddress.Text)));

                MyDoc.Add(new Paragraph(string.Format("\n")));

                MyDoc.Close();

                //Send PDF to client via a HTTP 
                Response.ContentType = "application/pdf";
                Response.AddHeader("Content-Disposition", string.Format(@"attachment; filename=Receipt-{0}.pdf", NewInvoiceNumber));
                Response.BinaryWrite(HTTPStream.ToArray());

            }
            else
            {

                //Member doesn't exist.
                string firstname = txtName.Text;
                string lastname = txtLastName.Text;
                string query = "INSERT INTO Wines(firstname, lastname) " +
                               "Values('" + firstname + "', '" + lastname + "')";

            }

You have an extra parenthesis at the end of your query, right after @Txtnput . 在查询的末尾,在@Txtnput之后,您有一个额外的括号。 It should be: 它应该是:

        SqlCommand check_Member = new SqlCommand("SELECT * FROM Members WHERE MembershipID = FirstName AND LastName = @Txtnput", DBConnection);

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

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