none
Why it doesn't work? RRS feed

  • Question

  • When I press start, form1 appears, I enter login and password, form1 disappear, form2 appears, I enter car number, people, comments, date is ready. But when I see to hawk.dbo.transport, there no new date I entered. How to fix?

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    using System.Windows.Forms;
    
    namespace Souvereign
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            public int failureTimes = 0;
            private void btnOK_Click(object sender, EventArgs e)
            {
                if (!string.IsNullOrWhiteSpace(loginTextBox.Text) && !string.IsNullOrWhiteSpace(passwordTextBox.Text))
                { 
                    var ops = new Souvereign("N10468000115\\SQLHUNTER", "Hawk");
                var loginResults = ops.SqlCredentialLogin(loginTextBox.Text, passwordTextBox.Text);
                    if (loginResults)
                    {
                        var successValue = ops.DoWork(loginTextBox.Text, passwordTextBox.Text);
                        var workResult = string.IsNullOrWhiteSpace(successValue);
                        if (workResult)
                        {
                            
                            Hide();                     
                           Form MW = new MainWindow();
                           
                            MW.ShowDialog();
    
                        }
                        else
                        {
                            MessageBox.Show(successValue);
                        }
                    }
    
                }
                else
                {
                    failureTimes++;
                    if(failureTimes>=3)
                    {
                        MessageBox.Show("Yoy have failed 3 times, the account has been locked");
                        Close();
                    }
                    MessageBox.Show($"You have failed {failureTimes} times, there are {3 - failureTimes} chances left");
                }
    
            }
    
            private void checkBox1_CheckedChanged(object sender, EventArgs e)
            {
                if (checkBox1.Checked)
                {
                    passwordTextBox.UseSystemPasswordChar = true;
                }
                else
                {
                    passwordTextBox.UseSystemPasswordChar = false;
                }
                
            
            }
        }
    }
    

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Souvereign
    {
        public partial class MainWindow : Form
        {
            private string serverName;
            private string catalogName;
            
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private bool IsCarNumberValid()
            {
                if (textBoxCarNumber.Text == "")
                {
                    MessageBox.Show("Please, Enter a car number");
                    return false;
    
                }
                else
                {
                    return true;
                }
            }
            private bool numericUpDown()
            {
                if (numericUpDown1.Value < 1)
                {
                    MessageBox.Show("Enter people amount");
                    return false;
                }
                else
                {
                    return true;
                }
            }
    
            private void buttonOK2_Click(object sender, EventArgs e)
            {
                if(IsCarNumberValid())
                {
                    string connectionString = $"Data source={serverName};" +
                        $"Initial catalog={catalogName};";
                    using (var cn = new SqlConnection { ConnectionString = connectionString })
                    {
    
    
                        using (SqlCommand sqlCommand = new SqlCommand("@dbo.uspTrans"))
                        {
                            sqlCommand.CommandType = CommandType.StoredProcedure;
                            sqlCommand.Parameters.Add(new SqlParameter("@tr_numb", SqlDbType.VarChar, 10));
                            sqlCommand.Parameters["@tr_numb"].Value = textBoxCarNumber.Text;
    
                            sqlCommand.Parameters.Add(new SqlParameter("@t_date", SqlDbType.DateTime));
                            sqlCommand.Parameters["@t_date"].Value = dateTimePicker1.Value;
    
                            sqlCommand.Parameters.Add(new SqlParameter("@comment", SqlDbType.VarChar, 1000));
                            sqlCommand.Parameters["@comment"].Value = richTextBox1.Text;
    
                            sqlCommand.Parameters.Add(new SqlParameter("@people", SqlDbType.Int));
                            sqlCommand.Parameters["@people"].Value = numericUpDown1.Value;
    
                            try
                            {
                                cn.Open();
                               
                            }
                            catch 
                            {
                              
                            }
                            finally
                            {
                                cn.Close();
                            }
                        }
                    }
                }
            }
        }
    }
    

    Thursday, November 21, 2019 4:00 PM

Answers

  • Hi pavlob,
    As the official documentation states, the parameter of the method Application.run() is applicationcontext or form, instead of the customized class.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Decompressor Friday, November 29, 2019 10:20 AM
    Friday, November 29, 2019 9:56 AM

All replies

  • Probably you should execute

       sqlCommand.Connection = cn;

       sqlCommand.ExecuteNonQuery( );

    after cn.Open, and treat the possible exceptions.

    Thursday, November 21, 2019 5:29 PM
  • Hi pavlob,
    >>sqlCommand.Parameters["@t_date"].Value = dateTimePicker1.Value;
    I checked your code and found that this statement will cause an error.
    The cause of the error is a data type conversion when insert  into the DATETIME type in the database.
    So you should change dateTimePicker1.Value to dateTimePicker1.Value.ToShortDateString().
    Besides, as Viorel said that you should execute the sql statement after cn.Open.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Friday, November 22, 2019 2:34 AM
  • I took your notes, but anyway it doesn't work
    Friday, November 22, 2019 6:59 AM
  • Friday, November 22, 2019 7:03 AM
  • May be, necessarily to  assign "catalogName" and "serverName"? But how this to write?
    Friday, November 22, 2019 7:06 AM
  • Hi Decompressor,
    About how to get the "catalogName" and "serverName", you can refer to this document.
    [Connection String Properties].

    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Monday, November 25, 2019 7:20 AM
  • using System;
    using System.Collections.Generic;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Security;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace Decompression
    {
        public class DatabaseUser
        {
            public string DoWork(string pUserName, string pPassword)
            {
                string connectionString = $"Data Source={serverName};" + $"Initial catalog={catalogName};";
                var securePassword = new SecureString();
    
                foreach (var character in pPassword)
                {
                    securePassword.AppendChar(character);
                }
                securePassword.MakeReadOnly();
                return "";
            }
            private string serverName;
            private string catalogName;
            public DatabaseUser(string pServerName, string pCatalogName )
            {
                serverName = pServerName;
                catalogName = pCatalogName;
            }
    
    
    
            public bool SqlCredentialLogin(string pUserName, string pPassword)
            {
                string connectionString = $"Data source={serverName};" +
               $"Initial catalog={catalogName};";
    
                var securePassword = new SecureString();
    
                foreach (var character in pPassword)
                {
                    securePassword.AppendChar(character);
                }
                securePassword.MakeReadOnly();
    
                var credentials = new SqlCredential(pUserName, securePassword);
                using (SqlConnection cn = new SqlConnection { ConnectionString = connectionString })
                {
                    try
                    {
                        cn.Credential = credentials;
                        cn.Open();
                        return true;
                    }
                    catch (Exception e)
                    {
                        return true;
                    }
    
                }
    
    
    
            }
        }
    }

    Wednesday, November 27, 2019 3:45 PM
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Decompression
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
            }
           
             
    
                public void btnOK_Click(object sender, EventArgs e)
            {
                    if (!string.IsNullOrWhiteSpace(loginTextBox.Text) && !string.IsNullOrWhiteSpace(passwordTextBox.Text))
                        {
                        var ops = new DatabaseUser("N10468000115\\SQLHUNTER", "demo");
                        var loginResults = ops.SqlCredentialLogin(loginTextBox.Text, passwordTextBox.Text);
                        if (loginResults)
                          {
    
                            var successValue = ops.DoWork(passwordTextBox.Text, loginTextBox.Text);
                            var workResult = string.IsNullOrWhiteSpace(successValue);
                            
                                if (workResult)
                                {
                                    Hide();
                                    Form MW = new MainWindow();
                                    MW.ShowDialog();
                                }
                                else
                                {
                                    MessageBox.Show(successValue);
                                }
                            
                          }
                        }
            }
        }
    }

    Wednesday, November 27, 2019 3:46 PM
  • Severity Code Description Project File Line Suppression State Suppression State
    Error CS7036 There is no argument given that corresponds to the required formal parameter 'pServerName' of 'DatabaseUser.DatabaseUser(string, string)' 19 Active
    Wednesday, November 27, 2019 3:47 PM
  • Hi Decompressor,
    I checked your code and found that the DatabaseUser is your own customized class, and type of the parameters passed in is also string. So it would not cause this error.
    And I tested the related code, this error did not occur.Could you tell me which line of code cause the error?
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    Thursday, November 28, 2019 6:37 AM
  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    
    namespace Decompression
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new DatabaseUser());
            } 
    Line 19 in program.cs - Application.Run(new DatabaseUser());
    Application.Run(new DatabaseUser());
    Thursday, November 28, 2019 7:02 AM

    • Edited by Decompressor Thursday, November 28, 2019 7:04 AM
    Thursday, November 28, 2019 7:03 AM
  • It occures, when I press "start"
    Thursday, November 28, 2019 7:05 AM
  • Hi Decompressor,
    The constructor DatabaseUser that you customized has two parameters, but there is no parameter in the statement "Application.Run(new DatabaseUser())".
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, November 29, 2019 6:08 AM
  • Friday, November 29, 2019 7:02 AM
  • But what I have, if I enter the parameters
    Friday, November 29, 2019 7:03 AM
  • Hi pavlob,
    As the official documentation states, the parameter of the method Application.run() is applicationcontext or form, instead of the customized class.
    Best Regards,
    Daniel Zhang


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Decompressor Friday, November 29, 2019 10:20 AM
    Friday, November 29, 2019 9:56 AM
  • Yeaaa! It works, Thank you!
    Friday, November 29, 2019 10:18 AM
  • Yeeaa! It works! Thank you!
    Friday, November 29, 2019 10:19 AM