|
I just don't get it why is it so complex to make a simple update...
I want trough a procedure to modify all the rows in a table like so: suppose I have a table with colums a, b, and c. c is identified by a and b. The procedure has the parameters a,b and c.
a,b,c
Example: UpdateazaCantCont(1,3,5) must add 5 to the old value of c. So if one row in the database table values are 1,3,8 the new values must be 1,3,13.
Please help me, because I'm in an death stop in my project!
The procedure i've wrote gives me no error, but it doesnt do anything.
Here's the code I have written:
private void UpdateazaCantCont(string aVal, string bVal, int cVal)
{
DataSet mds = new DataSet();
OleDbDataAdapter mda = new OleDbDataAdapter("select * from table where a='" + aVal + "'and b='" + bVal + "'", sirConex);
mda.Fill(mds,"continut_locatie");
DataTable t = mds.Tables[0];
DataRow[] r = t.Select("a='"+aVal+"b='"+ bVal + "'");
for (int i=0; i<r.length;>
string sirUpdate = "Update table set c=c+'"+cVal+"'where a='" + aVal + " and b='" + bVal + "'";
OleDbConnection myConex = new OleDbConnection(sirConex);
OleDbCommand cmd = new OleDbCommand(sirUpdate, myConex);
cmd.CommandText = sirUpdate;
mda.UpdateCommand = cmd;
try
{
mda.Update(mds, "table");
}
catch (Exception ex)
{
MessageBox.Show("Eroare: " + ex);
}
}
|
|
|
|
|
First of all, because aVal and bVal are strings, it's possible I can erase your database. Do you know about SQL Injection ?
Second, why use a dataset to get back a single value ?
Third, NEVER assume that an array has any values in it, without checking.
Fourth, what's the point of doing a select when you already have only those values in the table ?
you seem to be some of the way along, although you could do all of this in a stored proc and with 3 lines of code on the C# side.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Thanks for your reply. Can you tell me those 3 lines?
|
|
|
|
|
http://www.codeproject.com/KB/cs/simplecodeasp.aspx
[^]
Probably more than 3 lines, given you need one line for each of your three params. but still, a lot more readable. Of course, you also need to write the proc...
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hi,
Currently i am doing a project related to file encryption using c#. I new to the c# programming actually. I used microsoft visual c# express 2008. I had combine some of the code from internet reference, but i am facing a problem where program highlight "cs.Close();" and show
IndexOutOfRangeException was unhandled when decrypt button is clicked. The file decryption is unsuccessful due to this exception i think. Is there anybody know how to solve for this problem? Any suggestion on how to change the code? The code of my project is as below:
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.Security.Cryptography;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox2.Text = filename;
}
else
{
return;
}
}
private void button2_Click(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Encrypt(@"filename", @"C:\Encrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Encrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize / 8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
}
private void button3_Click(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox3.Text = filename;
}
else
{
return;
}
}
private void button4_Click(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Decrypt(@"filename", @"C:\Decrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Decrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize /8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
cs.Close();~IndexOutOfRangeException was unhandled
fsIn.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
fsCrypt = null;
}
}
}
The source code can be download from the link below:
http://cid-1caedb18b83acfa1.skydrive...Encryption.rar
If I am not disturbing,please guide. Thanks a lot!
Regards,
sf
|
|
|
|
|
I recommend you learn some basics before trying to copy and paste code you don't understand. Assuming someone wants to trawl through this and find the error, how does that help you learn ?
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Yaya..Thanks for your advice. I am trying hard to find out what problem with my code but the result is still the same. Is there any idea on which part do i need to focus with?
Regards,
cat ang
|
|
|
|
|
cat ang wrote: I am trying hard to find out what problem with my code
There is no obvious logical structure for a start. I don't know where to start reading to determine your problem.
|
|
|
|
|
Decrypt button? Which one is that? All the buttons are named Button1, Button2, etc. That isn't descriptive and while you might know which is the "Decrypt button", I do not. Please, for the love of all things, learn how to name things properly.
|
|
|
|
|
Sorry about that. This is my first time to post a thread, it is my careless.
i had rename my code and it is as below:
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.Security.Cryptography;
using System.IO;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void browseForFileToEncrypt_Click_1(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox2.Text = filename;
}
else
{
return;
}
}
private void btnFileEncryption_Click_1(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Encrypt(@"filename", @"C:\Encrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Encrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize / 8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
}
private void browseForFileToDecrypt_Click_1(object sender, EventArgs e)
{
openFD.Title = "Insert a File";
openFD.InitialDirectory = "F://";
openFD.FileName = "";
DialogResult result = openFD.ShowDialog();
string filename;
filename = openFD.FileName;
if (result == DialogResult.OK)
{
textBox3.Text = filename;
}
else
{
return;
}
}
private void btnFileDecryption_Click_1(object sender, EventArgs e)
{
string password = textBox1.Text;
string salt_value = "abcdefg";
string hash = "sha1";
int passwordIterations = 10;
string initVector = "@1A2C5D1F286AB54";
int keySize = 128;
Decrypt(@"filename", @"C:\Decrypted.ppt", password, salt_value, hash, passwordIterations, initVector, keySize);
}
public static void Decrypt(string _inputFile, string _outputFile, string password, string salt_value, string hash, int passwordIteration, string initVector, int keySize)
{
if (password.Length > 8)
password = password.Substring(0, 8);
else if (password.Length < 8)
{
int add = 8 - password.Length;
for (int i = 0; i < add; i++)
password = password + i;
}
byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
byte[] salt_valueBytes = Encoding.ASCII.GetBytes(salt_value);
PasswordDeriveBytes key = new PasswordDeriveBytes(password, salt_valueBytes, hash, passwordIteration);
byte[] keyBytes = key.GetBytes(keySize / 8);
FileStream fsCrypt = new FileStream(_outputFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
RMCrypto.Mode = CipherMode.CBC;
CryptoStream cs = new CryptoStream(fsCrypt, RMCrypto.CreateDecryptor(keyBytes, initVectorBytes), CryptoStreamMode.Write);
FileStream fsIn = new FileStream(_inputFile, FileMode.OpenOrCreate);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
cs.Close();
fsIn.Close();
fsCrypt.Close();
cs = null;
fsIn = null;
fsCrypt = null;
}
}
}
The link for this program is:
http://cid-1caedb18b83acfa1.skydrive.live.com/self.aspx/Public/File%20Encryption.rar[^]
Please give me some advice on it. Thanks ya
Regards,
cat ang
|
|
|
|
|
how to display X axis values on top of graph while the y axis displayed top to bottom values?
|
|
|
|
|
|
in my form i have several buttons(say 5)that call the same method, in the method i want to identify wich button is clicked and depending on that i decide what to do.how can i identify the button that is clicked
|
|
|
|
|
All those 5 buttons share same event handler? If yes, you can use parameter sender in that event handler. If you have a separate method, you need to add a parameter to it and pass the sender to there.
|
|
|
|
|
To expand on Navaneeth's answer/
void MyClick(object sender, eventargs ea)
{
Button button = sender as Button;
if ( button != null) // should always be the case
{
// here you can use if (button == button1, or you can use switch(button.Name), or check the Tag property, or whatever.
}
}
The way I tend to do this is to use an enum to identify the different actions, and then stuff that into the tag property, pull that out and switch on it. This means that it's typesafe instead of switching on strings.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
how can i split particular part from the image based on the pixel coordinates?
|
|
|
|
|
By using a DrawImage overload to draw just that section of the image onto a new image that's the right size to receive it.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Hi,
I am developing an web application, in which an windows application dll is run when the user click a button. From that application user communicate with other clients using socket.
In the server path there is an image folder.
My issue is that, how can i access the folder from the windows application dll.
Regards
YPKI
|
|
|
|
|
When you say 'windows applicaiton dll', it doesn'th ave windows UI, right ? It's just a dll ? Won't it be in the bin directory ? So, you need to either pass it the folder, or know what it will be, relative to where the dll is.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
The dll have a UI. From that UI user communicate. After opening the UI the browser will close and communication only thru the DLL UI. I need to access the folder from server.. ie same Server.MapPath in asp.net, but i need it from DLL UI. Socket is used to connect diffrent clients.
Regards
YPKI
|
|
|
|
|
No No
How can a DLL have a User interface ?
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/SQL7/2000/2005/2008
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@its.co.za
|
|
|
|
|
i create a windows application and convert to class library and use the dll in the web application.
|
|
|
|
|
Still i cant see the Posiility of what you are saying, mybe its better you explain step by step, because you might be saying the wrong terms. tell me if am wrong , i suspect that you have created a Windows Application(Project) and in the Solution you added another Project of a class type and compiled it and produced a DLL, and now you wanted to use that DLL in another Web Application. Now if am right about this , how did you go about using the DLL in the web Application ?
My be it would be better if you explain step by step what you did
Vuyiswa Maseko,
Few companies that installed computers to reduce the employment of clerks have realized their expectations.... They now need more and more expensive clerks even though they call them "Developers" or "Programmers."
C#/VB.NET/SQL7/2000/2005/2008
http://vuyiswamb.007ihost.com
http://Ecadre.007ihost.com
vuyiswam@its.co.za
|
|
|
|
|
OK, so this dll is downloaded by your web app and runs on the client side ? Then why would it need to know the path to images on the server side ?
Server.MapPath plainly won't do anytying when the code runs on the client.
Christian Graus
Driven to the arms of OSX by Vista.
|
|
|
|
|
Yes, i need to access a image from the server and display in the client application , and need to sent to another client in the network
|
|
|
|
|