i'm new for programming. I designed simple loging page.code as below.
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand("select * from loginp where username=@u and pasword=@p", con);
if (con.State == ConnectionState.Closed)
con.open();
cmd.Parameters.Add("@u", SqlDbType.VarChar, 50).Value = textBox1_username.Text;
cmd.Parameters.Add("@p", SqlDbType.VarChar, 50).Value = textBox1_passowrd.Text;
SqlDataReader dr = cmd.ExecuteReader();
if ((dr.Read() == true))
{
MessageBox.Show("Welcome User");
Form1 obj = new Form1();
obj.Show();
this.Hide();
}
else
MessageBox.Show("invalid Username and Passowrd");
dr.Close();
But I received error message as below.
Error 1 'System.Data.SqlClient.SqlConnection' does not contain a definition for 'open' G:\pro\new\new\Form1.cs 33 21 new
plz advice me to how to reslove it
Loading
Kunal VaishyaPosted Apr 4, 2013, 6:30 AM
all is well in your code just change the statement
if (con.State == ConnectionState.Closed)
Satyapriya NayakPosted Apr 4, 2013, 1:52 AM
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.OleDb;
namespace Login_page_Msaccess
{
public partial class Form1 : Form
{
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
OleDbCommand com;
string str;
object obj = null;
public Form1()
{
InitializeComponent();
}
private void btn_login_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select count(*) from login where UserName=@UserName and Password =@Password";
com = new OleDbCommand(str, con);
com.CommandType = CommandType.Text;
com.Parameters.AddWithValue("@UserName", TextBox_user_name.Text);
com.Parameters.AddWithValue("@Password", TextBox_password.Text);
obj = com.ExecuteScalar();
if ((int)(obj) != 0)
{
lb1.Text = "WELLCOME :: " + TextBox_user_name.Text;
}
else
{
lb1.Text = "invalid user name and password";
}
con.Close();
}
}
}
Vishal GilbilePosted Apr 4, 2013, 1:40 AM
Replace the con.open() with the following.
con.Open();
and i think the better approach for adding parameter to SqlCommand class is like the following.
cmd.Parameters.AddWithValue("@u",textBox1_username.Text);
cmd.Parameters.AddWithValue("@p", textBox1_passowrd.Text);
Rest all of the things are fine.
Hope that solves your problem.
With Regards,
Vishal Gilbile.