Click here to Skip to main content
15,884,628 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to write if exist command in ado.net?

string s = "select * from " + U_name.Text + "where Image=@Image IF EXIST ";

ADO.NET
not working above command ...
help me
Posted
Comments
DamithSL 20-Dec-14 6:31am    
anyway why you need if exist?
is you receive any record then you it is exist :)
otherwise not exist
Tushar sangani 20-Dec-14 6:38am    
i Think You use case When for check Exist or not
Tushar sangani 20-Dec-14 6:39am    
OR You Use Like This

SELECT *
FROM suppliers
WHERE EXISTS (SELECT *
FROM orders
WHERE suppliers.supplier_id = orders.supplier_id);

1 solution

Firstly, do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead - just like you do in the WHERE clause.

Secondly, try:
SQL
WHERE [Image] IS NOT NULL AND [Image]=@Image

("Image" is an SQL datatype, and thus a keyword: you need to use brackets round it to use it as a column name.)
 
Share this answer
 
v2
Comments
iamvinod34 20-Dec-14 6:57am    
try
{
string[] validFileTypes={"bmp","gif","png","jpg","jpeg","JPG","BMP","GIF","PNG","JPEG"};
string ext = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
bool isValidFile = false;
for (int i = 0; i < validFileTypes.Length; i++)
{
if (ext == "." + validFileTypes[i] )
{
isValidFile = true;
break;
}
}
if (!isValidFile)
{
Response.Write("<script language='javascript'>alert('Invailed File')</script>");
}

else if(FileUpload1.PostedFile.ContentLength<40000)
{
string s = "select * from " + U_name.Text + "where EXISTS(select * from "+U_name.Text+"Image=@Image";
SqlConnection conimage = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
SqlCommand cmdimage = new SqlCommand(s, conimage);
Byte[] bytes = null;
if (FileUpload1.HasFile)
{
string filename = FileUpload1.PostedFile.FileName;
string filePath = Path.GetFileName(filename);
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
bytes = br.ReadBytes((Int32)fs.Length);
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
SqlCommand command = new SqlCommand("UPDATE " + U_name.Text + " SET Image=@Image " + "", con);
command.Parameters.Add("@Image", SqlDbType.Binary).Value = bytes;
con.Open();
command.ExecuteNonQuery();
Label2.ForeColor = System.Drawing.Color.Green;
Label2.Text = "UPDATE Successfully";
}
else
{
Response.Write("<script language='javascript'>alert('Maximum 4MB Allowed')</script>");
}

if(FileUpload1.PostedFile.ContentLength<40000)
{
Byte[] bytes = null;
if (FileUpload1.HasFile)
{

string filename = FileUpload1.PostedFile.FileName;
string filePath = Path.GetFileName(filename);
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
bytes = br.ReadBytes((Int32)fs.Length);

}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
SqlCommand command = new SqlCommand(
"INSERT INTO " + U_name.Text + " (U_name, Image) " +
"Values(@U_name, @Image)", con);
command.Parameters.Add("@U_name",
SqlDbType.NVarChar, 20).Value = U_name.Text.Trim();
command.Parameters.Add("@Image",
SqlDbType.Binary).Value = bytes;
con.Open();
command.ExecuteNonQuery();
Label2.ForeColor = System.Drawing.Color.Green;
Label2.Text = "Successfully";
}
else
{
Response.Write("<script language='javascript'>alert('Maximum 4MB Allowed')</script>");
}
}
catch (Exception ex)
{
throw new Exception("Error" + ex.Message);
}
this my file upload button click event


i want user first time upload insert command.
user second time upload update command executed.
two commands only one button tell me
plz help me
OriginalGriff 20-Dec-14 7:10am    
Use a query which returns the number of entries with that name:
SELECT COUNT(*) FROM...
Return the count as an integer using ExecuteScalar and check it. If it is zero, you need an INSERT. If it's one, you need an UPDATE. If it isn't either of those, you have a database problem...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900