Retrieving Data From Sql Server 2008r2 Using Winforms
I am creating a c# windows form app, which will retrieve the data from a DB that is already created in sql server2008r2. There are 2 win forms in my app, 1st one is for getting th
Solution 1:
Why not not change your constructor in your welcome form to receive parameters.
public Welcome(String usr, String pword)
{
InitializeComponent();
this.Username=usr;
this.Password=pword; // you should have a form of encryption for your password
}
So, when you call, you do:
Welcome f1 = new Welcome(txtUsername.Text,txtPassword.Text);
and then you add properties in welcome form like:
private String Username { get; set; }
private String Password { get; set; }
then add the 2 parameters in welcome form load:
cmd.Parameters.Add("@x", SqlDbType.NVarChar).Value = Username;
cmd.Parameters.Add("@y", SqlDbType.NVarChar).Value = Password;
Solution 2:
You could use constructors to solve your problem
When you hit the "login" button (supposing that there's one) you could:
privatevoidlogin_Click(object sender, System.EventArgs e)
{
Form2 frm=newForm2(userName.text,password.tex);
frm.Show();
}
Your second form constructor could be:
publicForm2(string user, string pass)
{
InitializeComponent();
//Save your parameters here so you can use them with the query
}
There are other ways to do this. http://www.codeproject.com/Articles/14122/Passing-Data-Between-Forms
Solution 3:
Of course you cannot retrieve data from database in second form since you are not
1- you didn't send the x and y parameters to to the second Form
Form2frm=newForm2(userName.text,password.tex);
frm.Show();
2- must pass x and y to the query in SqlCommand
and execute the query
query = "SELECT * FROM Users WHERE UserName='"+ x +"'AND Password='"+y+"'";
Final code
in Form one
if (count > 0)
{
MessageBox.Show("Valid Username and Password");
Welcomef1=newWelcome( txtUserName.Text,txtPassword.Text);
f1.Show();
}
in Form Two
string x, y;
publicWelcome(String usr, String pword)
{
InitializeComponent();
x = usr; y = pword;
}
privatevoidWelcome_Load(object sender, EventArgs e)
{
query = "SELECT * FROM Users WHERE UserName='"+ x +"'AND Password='"+y+"'";
cmd.SelectCommand = new SqlCommand(query ,con);
ds.Clear();
cmd.Fill(ds);
userTable.DataSource = ds.Tables[0];
txtFristName.DataBindings.Add(new Binding("Text", userTable, "FirstName"));
txtLastName.DataBindings.Add(new Binding("Text", userTable, "LastName"));
txtAddress.DataBindings.Add(new Binding("Text", userTable, "Address"));
txtTelephone.DataBindings.Add(new Binding("Text", userTable, "Telephone"));
txtEmail.DataBindings.Add(new Binding("Text", userTable, "Email"));
txtFax.DataBindings.Add(new Binding("Text", userTable, "Fax"));
txtSection.DataBindings.Add(new Binding("Text", userTable, "Section"));
txtPosition.DataBindings.Add(new Binding("Text", userTable, "Position"));
}
Post a Comment for "Retrieving Data From Sql Server 2008r2 Using Winforms"