protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection conNwind;
SqlCommand cmdSelect, cmdInsert;
SqlDataReader dtrUser;
string connStr = ConfigurationManager.ConnectionStrings["ConnectionNWind"].ConnectionString;
conNwind = new SqlConnection(connStr);
conNwind.Open();
string select = "Select * from UserRegistration where UserName=@UN AND Email=@Email";
cmdSelect = new SqlCommand(select, conNwind);
cmdSelect.Parameters.AddWithValue("@UN", txtUN.Text);
cmdSelect.Parameters.AddWithValue("@Email", txtEmail.Text);
dtrUser = cmdSelect.ExecuteReader();
if (dtrUser.Read())
{
if (Username != "")
{
Label1.Text = "UserName already exists";
}
else if (Email != "")
{
Label1.Text = "Email already exist!";
}
else
{
dtrUser.Close();
string strInsert = "Insert into UserRegistration(FirstName,LastName,UserName,Password,Gender,Address,Email) values(@FN,@LN,@UN,@Pass,@Gender,@Add,@Email)";
cmdInsert = new SqlCommand(strInsert, conNwind);
cmdInsert.Parameters.AddWithValue("@FN", txtFN.Text);
cmdInsert.Parameters.AddWithValue("@LN", txtLN.Text);
cmdInsert.Parameters.AddWithValue("@UN", txtUN.Text);
cmdInsert.Parameters.AddWithValue("@Pass", txtPass.Text);
cmdInsert.Parameters.AddWithValue("@Gender", rdlGender.SelectedItem.ToString());
cmdInsert.Parameters.AddWithValue("@Add", txtAdd.Text);
cmdInsert.Parameters.AddWithValue("@Email", txtEmail.Text);
int intAdd = cmdInsert.ExecuteNonQuery();
if (intAdd != 0)
Label1.Text = "User Registration successful,you can now Login";
else
Label1.Text = "Record not added into database!";
}
}
else
conNwind.Close();
}
I want to check the record weather its exits in the database(which is the username and the email address) before user insert, but i have a problem which highlighted in yellow color which shown above.
kani kaniPosted Nov 3, 2012, 8:39 AM
if (dtrUser["Username"].toString().ToUpper()==txtUN.Text.toString().ToUpper())
Label1.text="Username already exists";
else if (dtuser["email"].tostring().toUpper()==txtEmail.text.tostring().toUpper())
Label1.Text = "Email already exist!";
Cool HengPosted Oct 11, 2012, 11:05 AM
this is my code...Deepak Kumar Verma!
Cool HengPosted Oct 11, 2012, 10:29 AM
Deepak VermaPosted Oct 10, 2012, 11:22 PM
Use the following sql code :
IF EXISTS(SELECT TOP 1 UserName FROM UserRegistration WHERE UserName = @UN)
SET @Status = 1
ELSE IF EXISTS(SELECT TOP 1 Email FROM UserRegistration WHERE Email = @Email)
SET @Status = 2
ELSE
SET @Status = 0
SELECT @Status [Status]
And setup a switch-case statement to handle it, like this:
if()//Check for null
{
switch(status)
{
case 1:
break;
case 2:
break;
case 0:
break;
}
}hope it suits your requirements!
Cool HengPosted Oct 9, 2012, 1:30 PM
Cool HengPosted Oct 9, 2012, 1:25 PM
Santhosh Kumar JayaramanPosted Oct 9, 2012, 1:06 PM
Cool HengPosted Oct 9, 2012, 1:01 PM
Santhosh Kumar JayaramanPosted Oct 9, 2012, 12:46 PM
and here you are checking not equal to empty string.
Cool HengPosted Oct 9, 2012, 12:37 PM
Cool HengPosted Oct 9, 2012, 12:37 PM
Santhosh Kumar JayaramanPosted Oct 9, 2012, 12:13 PM
Satyapriya NayakPosted Oct 9, 2012, 12:10 PM
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.OleDb;
namespace Insert_data_ifnot_exits_windows
{
public partial class Form1 : Form
{
OleDbDataAdapter oledbda;
string ConnectionString = System.Configuration.ConfigurationSettings.AppSettings["dsn"];
string str;
OleDbCommand com;
public Form1()
{
InitializeComponent();
}
private void btn_insert_Click(object sender, EventArgs e)
{
OleDbConnection con = new OleDbConnection(ConnectionString);
con.Open();
str = "select count(*)from student where name='" + textBox1.Text + "' or email='" + textBox2.Text + "'";
com = new OleDbCommand(str, con);
int count = Convert.ToInt32(com.ExecuteScalar());
con.Close();
if (count > 0)
{
lblMessage.Text = "Student name or email exits";
}
else
{
lblMessage.Text = "Student name or email does not exits";
con.Open();
str = "insert into student(name,email,address) values(@name,@email,@address)";
com = new OleDbCommand(str, con);
com.Parameters.AddWithValue("@name", textBox1.Text);
com.Parameters.AddWithValue("@email", textBox2.Text);
com.Parameters.AddWithValue("@address", textBox3.Text);
com.ExecuteNonQuery();
con.Close();
clear();
}
}
void clear()
{
textBox1.Text="";
textBox2.Text="";
textBox3.Text="";
}
}
}
Thanks
Cool HengPosted Oct 9, 2012, 11:51 AM
Santhosh Kumar JayaramanPosted Oct 9, 2012, 11:26 AM