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 stp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=ex;Integrated Security=True;Pooling=False");

            SqlCommand sqlcmd = new SqlCommand("dbo.AddUser", con);
            sqlcmd.CommandType = CommandType.StoredProcedure;
            sqlcmd.Parameters.AddWithValue("@Username", textBox1.Text);
            sqlcmd.Parameters.AddWithValue("@Password", textBox2.Text);
            try
            {
                con.Open();
                sqlcmd.ExecuteNonQuery();
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message.ToString());
            }
            finally
            {
                con.Close();
                /*
                 ochcharai neda?
                 * 
                 
                 */
            }
        }
    }
}

this is the code that i have made using stored procedure to add to text box values to a database.And now i need to retreav them one by one .(it means in a another form if i enter the user name and pres a button i must get the user name and the password to another new 2 texboxes)thanx

Recommended Answers

All 23 Replies

First you do not need to retrieve the username because is given as parameter. base on what you said.
Under the button search click event you will have a code like this.

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=ex;Integrated Security=True;Pooling=False");
/*This only work if you are going to retrieve just the password otherwise you will need a dataset or datareader.*/
SqlCommand sqlcmd = new SqlCommand("Select Password from YourTable Where UserName = '"+textBox1.Text+"'", con);
con.Open();
textBox2.Text = Convert.ToString(sqlcmd.ExecuteScalar());
con.Close();

thanxxxxxx

First you do not need to retrieve the username because is given as parameter. base on what you said.
Under the button search click event you will have a code like this.

SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=ex;Integrated Security=True;Pooling=False");
/*This only work if you are going to retrieve just the password otherwise you will need a dataset or datareader.*/
SqlCommand sqlcmd = new SqlCommand("Select Password from YourTable Where UserName = '"+textBox1.Text+"'", con);
con.Open();
textBox2.Text = Convert.ToString(sqlcmd.ExecuteScalar());
con.Close();

if i need to retreave mor than one valu (lets say pasword and the first name ) is this code correct.

SqlCommand sqlcmd = new SqlCommand("Select Password,firstname from <Table name> Where UserName = '"+textBox1.Text+"' first name = '"+textBox2.Text+", con);

More or less, but you should be using parameterized queries.

if i need to retreave mor than one valu (lets say pasword and the first name ) is this code correct.

SqlCommand sqlcmd = new SqlCommand("Select Password,firstname from <Table name> Where UserName = '"+textBox1.Text+"' first name = '"+textBox2.Text+", con);

You are fetching password and first name based on user name. Therefore the WHERE condition need not to have first name.

It should look like SqlCommand sqlcmd = new SqlCommand("Select Password,firstname from <Table name> Where UserName = '"+textBox1.Text+"' , con);

Yes the above select statement is correct, but remember you will need a dataset or datareader to fetch more than one column as follow.

SqlCommand sqlcmd = new SqlCommand("Select Password,firstname from <Table name> Where UserName = '"+textBox1.Text+"'", con);
con.Open();
SqlDataReader dr = sqlcmd.ExecuteReader();
While(dr.Read())
{
TextBox2.Text = dr.GetString(0);
TextBox3.Text = dr.GetString(1);
}
dr.Close();
con.Close();

Regards

thanxx

and now i need to create a simple login form from these detail.

In a buttton click i need to check whether textbox1's(user name) value is in the data base 's user name field.
and text box2's valu (password) is in the same database's password field.

if the user name and password are maching textbx.show().

so how can i do this

private void button3_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=ex;Integrated Security=True;Pooling=False");
            
               
                   SqlCommand sqlcmd = new SqlCommand("select * from Users", con);
                 
                   con.Open();

                   SqlDataReader reader = sqlcmd.ExecuteReader();

                   reader.Read();

                   if ((reader(0) = textBox7.Text) && (reader(1) = textBox8.Text))
                   {
                       Form2 fr2 = new Form2();
                       fr2.Show();
                   }

                   else
                       MessageBox.Show("a");

by looking at the postes topic i tryed for this kind of method.but it gives me errors

Please use code tags when posting code -- and what errors are you getting?

Something like this.

OleDbDataReader dr;
OleDbCommand cmd = New OleDbCommand("select * from login where id= '"+txtUser.Text+"' and pass='"+txtPwd.Text+"'", conn);
        dr = cmd.ExecuteReader();
        if(dr.HasRows)
            Form2.Show();
        Else
{
            txtUser.Clear();
            txtPwd.Clear();
            txtUser.Focus();
}

regards

I suppose this is your problem.

if ((reader(0) = textBox7.Text) && (reader(1) = textBox8.Text))
                   {
                       Form2 fr2 = new Form2();
                       fr2.Show();
                   }

Which should have been.

if ((reader[0].ToString().Equals(textBox7.Text) && (reader[1].ToString().Equals(textBox8.Text)))
                   {
                       Form2 fr2 = new Form2();
                       fr2.Show();
                   }

And you might want to use

if(reader.Read())
{
//code to read here.
}
private void button3_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=ex;Integrated Security=True;Pooling=False");
            
               
                   SqlCommand sqlcmd = new SqlCommand("select * from Users", con);
                 
                   con.Open();

                   SqlDataReader reader = sqlcmd.ExecuteReader();

                   reader.Read();

                   if ((reader(0) = textBox7.Text) && (reader(1) = textBox8.Text))
                   {
                       Form2 fr2 = new Form2();
                       fr2.Show();
                   }

                   else
                       MessageBox.Show("a");

by looking at the postes topic i tryed for this kind of method.but it gives me errors

NO ANY comments..

private void button3_Click(object sender, EventArgs e)
        {

            SqlConnection con = new SqlConnection("Data Source=.\\SQLEXPRESS;Initial Catalog=ex;Integrated Security=True;Pooling=False");
            SqlCommand sqlcmd = new SqlCommand("select * from Users", con);
                 
            con.Open();
            SqlDataReader reader = sqlcmd.ExecuteReader();
            reader.Read();

 
              if ((reader[0].ToString().Equals(textBox7.Text) && (reader[1].ToString().Equals(textBox8.Text))))
                    {
                Form2 fr2 = new Form2();
                fr2.Show();
                    }
              else
              {
                  MessageBox.Show("QAW");
              }
       
            
        }

I TRYED for this ,,,,but not workin.and when de bug the if condition i ckek that the texbox values are catch by the if condition.but the fr2.show() is not working

Are you getting error or are you getting a different result. Post some details so that we can help.

no i didnt get errors.only thing hapen is wen i click the button the form is doing nothing..
i wll upload the application to some place and publish the link

Your code is working fine. When you login the second form is being shown.
Make the uid unique in the database. As you have have two entries in your database for same uid it might be producing problems.

NOOP...this code is not working.i need to do is when i press the log in buttton on the bottom a new form 2 window must be open.
but always i get the "QAW" message box.that mesage box is on the else part of the if condition.

Ya, its is working . Use a break in else part see the values of the data returned. And did you correct the problem mentioned in my earlier post.

why dont you pass the login parameters in the select statement and if datareader has any rows it means that user exist.

YOU PPL ARE TELING THE LOGIN BUTTON SHOWS THE 2nd formm..????let me rebuild againn

that is not eorking guys.......the form2 is not shown to me when i click the login button.only i can see is the message box saying "QAW"

..........what must i do

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.