given value exist in database or not in ASP.NET

Posted by Mandlaa under ASP.NET on 12/13/2013 | Points: 10 | Views : 864 | Status : [Member] | Replies : 7
public bool Data()
{
String Tno=102.1.01;
string Con = System.Configuration.ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString;
SqlConnection con1 = new SqlConnection(Con);

SqlDataAdapter da=new SqlDataAdapter("select TagId from TagTable where TagId="+Tno,con1);

DataSet ds = new DataSet();

da.Fill(ds, "TagTable");

return true;
}
return false;
What can i do here is

When TagId==Tno then return method is true,Not match return false

This is my code how can i cheach this one

I want to chech that given value exist in database or not




Responses

Posted by: vishalneeraj-24503 on: 12/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Write code after fill dataset

string tag_no = convert.tostring(ds.tables[0].row[0]["TagId"]);

if(tag_id==tag_no)
{
return true;
}
else
{
return false;
}

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Let me know please.

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Mandlaa on: 12/13/2013 [Member] Starter | Points: 25

Up
0
Down
public bool Data()
{
String Tno=102.1.01;
string Con = System.Configuration.ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString;
SqlConnection con1 = new SqlConnection(Con);

//SqlDataAdapter da = new SqlDataAdapter("SELECT [TagId],[BatchId],[ClientId] FROM TagTable", con1);
SqlDataAdapter da=new SqlDataAdapter("select [TagId] from TagTable where TagId="+Tno,con1);

DataSet ds = new DataSet();

da.Fill(ds, "TagTable");
string tag_no = ds.Tables[0].Rows[0]["TagId"].ToString();

if (Tno == tag_no)
{
return true;
}
else
{
return false;
}
}
I am writting this code getting an error like this
Incorrect syntax near '.01'.

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Learningtorise on: 12/13/2013 [Member] Starter | Points: 25

Up
0
Down
public bool Data()
{
String Tno="102.1.01";//String value!!!
string Con = System.Configuration.ConfigurationManager.ConnectionStrings["stgdbConnectionString"].ConnectionString;
SqlConnection con1 = new SqlConnection(Con);

//SqlDataAdapter da = new SqlDataAdapter("SELECT [TagId],[BatchId],[ClientId] FROM TagTable", con1);
SqlDataAdapter da=new SqlDataAdapter("select [TagId] from TagTable where TagId="+Tno,con1);

DataSet ds = new DataSet();

da.Fill(ds, "TagTable");
string tag_no = ds.Tables[0].Rows[0]["TagId"].ToString();

if (Tno == tag_no)
{
return true;
}
else
{
return false;
}
}

http://hashtagakash.wordpress.com/

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: vishalneeraj-24503 on: 12/13/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
change this in sqldataadapter,:-

select [TagId] from TagTable where TagId = '"+ TagId +"'


Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sheonarayan on: 12/13/2013 [Administrator] HonoraryPlatinum | Points: 25

Up
0
Down
In this case, better to user ExecuteScalar() method of the SqlCommand objcet and also Parameterized statement. ExecuteScalar() gives the first column of first row data, in this case you will get the value of TagId as object if exists otherwise you get null.

Have a look at this example - http://www.dotnetfunda.com/codes/show/965/how-to-check-whether-a-username-exists-in-our-database-or-not.

Thanks

Regards,
Sheo Narayan
http://www.dotnetfunda.com

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Samirbhogayta on: 12/16/2013 [Member] Starter | Points: 25

Up
0
Down
hi..
I have managed to do the following...

string connectionString = "datasource=localhost;username=xxx;password=xxx;database=xxx";
MySqlConnection mySqlConnection = new MySqlConnection(connectionString);

string selectString =
"SELECT username, password " +
"FROM forum_members " +
"WHERE username = '" + frmUsername.Text + "' AND password = '" + frmPassword.Text + "'";

MySqlCommand mySqlCommand = new MySqlCommand(selectString, mySqlConnection);
mySqlConnection.Open();
String strResult = String.Empty;
strResult = (String)mySqlCommand.ExecuteScalar();
mySqlConnection.Close();

if (strResult.Length == 0)
{
Label1.Text = "INCORRECT USER/PASS!"
//could redirect to register page
} else {
Label1.Text = "YOU ARE LOGGED IN!";
//set loggin in sessions variables
}

SAMIR
Sr. Software Engineer

Mandlaa, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response