my sql table is not updated, when i do???!!, the codes are as folow:
string connectionStringid = ConfigurationManager.ConnectionStrings["CustomerDB"].ConnectionString;
connad = new SqlConnection(connectionStringad);
commandad = new SqlCommand("", connad);
which are class variables. the folowing codes for insert and update, insert works, but not update???!!:
public static string AddAd(Customerad customerad, int Id)
{
string result;
string q = string.Format("select * from customerad1 where Email='{0}' and Title='{1}'", customerad.Email, customerad.Title);
try
{
connad.Open();
commandad.CommandText = q;
int amountOfUsers = Convert.ToInt32(commandad.ExecuteScalar());
if (amountOfUsers < 1) //new user
{
commandad.CommandText = @"INSERT INTO customerad1 (Name, Email, Phone, Country, ZipCode, Category, Title, Price, Images, Date_Time, Description, Bussiness, Payment) VALUES
('" + customerad.Name + "','" + customerad.Email + "','" + customerad.Phone + "','" + customerad.Country + "','" + customerad.ZipCode + "','" + customerad.Category + "','" + customerad.Title + "','" + customerad.Price + "','" + customerad.Images + "','" + customerad.Date_Time + "','" + customerad.Description + "','" + customerad.Bussiness + "','" + customerad.Payment + "')";
commandad.ExecuteNonQuery();
result = "Ad registered!";
}
else //user exist
{
commandad.CommandText = @"UPDATE customerad1 SET ([Name]='" + customerad.Name + "', [Email]='" + customerad.Email + "', " +
"[Phone]='" + customerad.Phone + "', " +
"[Country]='" + customerad.Country + "', [ZipCode]='" + customerad.ZipCode + "', [Category]='" + customerad.Category + "', " +
"[Title]='" + customerad.Title + "', [Price]='" + customerad.Price + "', [Images]='" + customerad.Images + "', " +
"[Date_Time]='" + customerad.Date_Time + "', [Description]='" + customerad.Description + "', [Bussiness]='" + customerad.Bussiness + "', [Payment]='" + customerad.Payment + "') where [Id]='" + Id + "' ";
commandad.ExecuteNonQuery();
result = "Ad updated!";
}
}
}
i cannot find the problem... please help me, i appreciate... kind regards
Jaimin ShethiyaPosted Nov 3, 2023, 12:48 PM
Hello Venus
Please update the below mention code and verify it.
commandad.CommandText = @"UPDATE customerad1 SET [Name]='" + customerad.Name + "', [Email]='" + customerad.Email + "', " +
"[Phone]='" + customerad.Phone + "', " +
"[Country]='" + customerad.Country + "', [ZipCode]='" + customerad.ZipCode + "', [Category]='" + customerad.Category + "', " +
"[Title]='" + customerad.Title + "', [Price]='" + customerad.Price + "', [Images]='" + customerad.Images + "', " +
"[Date_Time]='" + customerad.Date_Time + "', [Description]='" + customerad.Description + "', [Bussiness]='" + customerad.Bussiness + "', [Payment]='" + customerad.Payment + "' where [Id]='" + Id + "' ";
venus najadPosted Nov 5, 2023, 12:06 PM
hello everyone and thnaks for your responds to my update sql table issue. it is solved but i have another issure, which is how to conver my codes to base 64? my codes are as follow:
protected void ddlImage_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedValue = ddlImage.SelectedItem.Value;
ShowImages(txtEmail.Text);
}
private void ShowImages(string email)
{
//get all filepath
string[] images = Directory.GetFiles(Server.MapPath("~/images/customers/" + email));
//get all files and add them to an arraylist
ArrayList imageList = new ArrayList();
foreach (string image in images)
{
string imageName = image.Substring(image.LastIndexOf(@"\") + 1);
imageList.Add(imageName);
}
// //set the arraylist as the dropdownlist's datasource and refresh
ddlImage.DataSource = imageList;
ddlImage.DataBind();
}
protected void btnuploadimage_Click(object sender, EventArgs e)
{
try
{
string folder = Server.MapPath("~/images/customers/" + txtEmail.Text);
if (!Directory.Exists(folder))
{
Directory.CreateDirectory(folder);
}
string filename = Path.GetFileName(FileUpload1.FileName);
FileUpload1.SaveAs(Server.MapPath("~/images/customers/" + txtEmail.Text + "/" + filename));
lblImage.Text = "image " + FileUpload1.FileName.ToString() + " successfully uploaded!";
Image1.ImageUrl = "~/images/customers/" + txtEmail.Text + "/" + filename;
Page_Load(sender, e);
}
catch (Exception)
{
lblImage.Text = "uppload Failed!";
}
}
i have found base 64 codes, but i ned help to use them on my codes.... i appreciate your help, thanks... the base 64 codes are:
for getting the image path
string DefaultImagePath = HttpContext.Current.Server.MapPath("~/NoImage.jpg");
byte[] imageArray = System.IO.File.ReadAllBytes(DefaultImagePath);
string base64ImageRepresentation = Convert.ToBase64String(imageArray);
now i for converting the base 64 string to byte array that converts to memorystream:
byte[] bytes = Convert.FromBase64String(base64ImageRepresentation);
using (MemoryStream ms = new MemoryStream(bytes))
{ pic.Image = Image.FromStream(ms); }
i appreciate any help... specialy if you give me the codes, thanks nd very kind regards
Prasad RaveendranPosted Nov 4, 2023, 8:39 PM
The issue with the update operation in your code seems to be with the syntax of the SQL update statement and the parameterization of the query. The
UPDATEstatement should not have([column]='value')syntax for each column. Instead, it should beSET column1 = value1, column2 = value2, ....Here's an adjusted version of your code using parameterized queries to prevent SQL injection and fix the update operation:
This code uses parameterized queries to prevent SQL injection and constructs the UPDATE query with proper SET column = value syntax. Make sure to handle exceptions and close connections appropriately in your code.
Sam HobbsPosted Nov 4, 2023, 4:47 PM
Also understand the importance of using the
try-catchstatement. Note that the sample code Amit Mohanty posted does. You can use thetry-catchstatement for each relevant statement, such as each call tocommandad.ExecuteNonQuery().Mehmet FatihPosted Nov 4, 2023, 9:05 AM
If your id value is numeric then modify your code like that where [Id]=" + Id + "
Jignesh KumarPosted Nov 4, 2023, 8:24 AM
Yes, in update statement () not required.
Syntax:
Update tablename set columname = @newvalue where Id = @Id
venus najadPosted Nov 3, 2023, 5:37 PM
hello everyone and thnks for your replaies... mr *Amit i had used similar your codes, but it tells that Parameters.AddWithValue is old or @Email (and so on) not recognized... thanks for putting your time
mr jaimin and Jignesh thnks for your responds.. it worked. the problem was paranteses...
mr Jignesh, Convert.ToInt32(commandad.ExecuteScalar()) gives a value less than 1 if the item is not in the sql table and bigger than or equal 1 if the item is in the sql table...
thank your all and kind regards
Jignesh KumarPosted Nov 3, 2023, 3:28 PM
Hi Venus,
Have you tried to debug your code.
One suggession, You can write your code in different way which is best practice as per software guide line, as part of coding standard we should use parameter rather then writing inline query you can conver your code to single store procedure like below and call from .net code,
Please refer this article which will help you out.
https://www.c-sharpcorner.com/blogs/insert-delete-and-update-using-stored-procedure-in-asp-net1
https://www.aspsnippets.com/Articles/GridView-CRUD-Select-Insert-Edit-Update-Delete-using-Single-Stored-Procedure-in-ASPNet.aspx
Amit MohantyPosted Nov 3, 2023, 12:50 PM
Firstly your code is vulnerable to SQL injection. You should use parameterized queries instead of directly embedding values into your SQL statements. This will also make your code more secure and also consider using the
usingstatement for your SqlConnection and SqlCommand objects to ensure they are properly disposed of after use Check this: