Click here to Skip to main content
15,921,959 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Dear Expert,


I facing error when want to pass my data which insert at Form1(frmLogin) to Form2(frmMain) SQL Query.

Form1(frmLogin)coding

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SytelineAutoGenerateDocNum
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            SqlConnection sqlcon = new SqlConnection(@"Data Source=P13L3CFN756\SQLEXPRESS;Initial Catalog=Syteline_Misc_DocNum;Persist Security Info=True;User ID=sa;Password=1234;");
            string query = "SELECT *FROM [Syteline_Misc_DocNum].[dbo].[users] Where username = '" + txtUsername.Text.Trim() + "' and password = '" + txtPassword.Text.Trim() + "'";
            SqlDataAdapter sda = new SqlDataAdapter(query, sqlcon);
            DataTable dtbl = new DataTable();
            sda.Fill(dtbl);


            if (dtbl.Rows.Count == 1)
            {
                frmMain objFrmMain = new frmMain();
                
                //objFrmMain.Value = txtUsername.Text;
               //objFrmMain.ShowDialog();
                objFrmMain.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Check your username and password");
            }
            
        }

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}


Form2(frmMain)Coding

private void buttonGDN_Click(object sender, EventArgs e)
       {
           con.Open();
           SqlCommand cmd = new SqlCommand("Insert into DocNum (Date,UserID,BusinessUnit,RunningNumber,DocumentNumber)values('" + txtUsername.Text + "')");
       }


My Error is (The Name "txtUsername" does not exist in the current context).

Appreciated for you help. Thanks

What I have tried:

I have no idea on the Error. and I have tried to enable the System Configuration on the reference. But also could not solve the problem.
Posted
Updated 6-Jul-20 0:02am
Comments
Richard Deeming 6-Jul-20 8:21am    
In addition to the SQL Injection[^] vulnerability in your code, you're also storing passwords in plain text. Don't do that.

Secure Password Authentication Explained Simply[^]
Salted Password Hashing - Doing it Right[^]

Exactly how depends on the "relationship" between the two forms.
Have a look at these, one of them will fit your circumstances.
The form that creates an instance of another:
C#
MyForm mf = new MyForm();
mf.Show();
Is the "parent", the other form is the "child".
(This doesn't imply any formal MDI relationship)

Transferring information between two forms, Part 1: Parent to Child[^]
Transferring information between two forms, Part 2: Child to Parent[^]
Transferring information between two forms, Part 3: Child to Child[^]
You need Part 2.
 
Share this answer
 
By default, one form doesn't have access to controls on another form. You started resolving the issue with your code
C#
//objFrmMain.Value = txtUsername.Text
but you commented it out. Instead of referring to frmMain's .Value property, you can create a public variable or property of frmMain and set it at this point; then reference it within frmMain's buttonGDN_Click event.
An alternative method would be to qualify your reference to txtUsername when you refer to it; however, you'd need to locate the right form in order to get a reference to it. (Suppose there was some situation where buttonGDN_Click was invoked, but frmLogin was no longer open, or there were multiple instances of it). This would be relatively tricky, and you'd still need to deal with cases where the form wasn't open or there were multiple instances (in your app, it *might* be a safe assumption that there would always be one instance of frmLogin open, but I can't assume that, and it might change in the future).
So your best bet is to create either a public property of frmMain, or a publicly-accessible variable:
C#
public string Username = string.Empty;
Then your frmMain opening code becomes
C#
frmMain objFrmMain = new frmMain();                
objFrmMain.Username = txtUsername.Text;
objFrmMain.Show();
Finally, as so often, your application is wide open to a SQL Injection attack (or just accidental error / damage) as a result of just in-lining your form variables when building your SQL to check username. Anyone entering a single apostrophe in their username either breaks your code, or breaks into your database. Always parameterise any SQL query that includes user-entered text.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900