Click here to Skip to main content
15,997,532 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am inserting some information using this code.
but i face problem in inserting "password"
password collummn is a varbinary(MAX) type.
but when I run this code and see the result in my database. every entries correct but in password collumn it is sowing that "< binary data >" not sowing password which is in Binary form.
C#
SqlCommand smd;
 private void saveInformation(string command)
 {
     try
     {
         string connectString = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString();
         sc = new SqlConnection(connectString);
         smd = new SqlCommand(command, sc);
         sc.Open();
         smd.ExecuteNonQuery();
     }
     catch (Exception ex)
     {
         errormsg.Text= ex.Message;
     }

 }

 protected void bemember_Click(object sender, EventArgs e)
 {
     string Name =  name.Text;
     string Email = email.Text;
     string Passtxt = pass.Text;
     byte[] Pass = GetSHA1(Email, Passtxt);
     string command = "Insert into userInfo (username,password,email) values('"+Name+"', CAST('"+Pass+"' AS varbinary(max)),'"+Email+"')";
     saveInformation(command);
 }

 private static byte[] GetSHA1(string userID, string password)
 {
     SHA1CryptoServiceProvider sha = new SHA1CryptoServiceProvider();
     return sha.ComputeHash(System.Text.Encoding.ASCII.GetBytes(userID + password));
 }pre>
Posted
Updated 11-Oct-12 23:18pm
v4

1 solution

Why cant you store procedure for that
1St create a store Procedure like this
Here Pass coloum datatypeis varbinary(MAX) in table
SQL
create procedure [dbo].[SP_Insert]
(@username varchar(20),@password varchar(20),@Pass varbinary(max))
as begin
-- Your Insert Query here
  insert into Table1(username ,password ,Pass ) values (@username,@password,@Pass)
end



/////////////// In C# code you can write code like this
C#
byte[] Pass = GetSHA1(Email, Passtxt);
string connectString = System.Configuration.ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString();


           SqlConnection con=new SqlConnection(connectString );
           cmd.CommandType = CommandType.StoredProcedure;
           cmd.CommandText = "SP_Insert";
           cmd.Connection = con;
           con.open();

           cmd.Parameters.Add("@username",name.Text );
            cmd.Parameters.Add("@password",pass.Text );
             cmd.Parameters.Add("@Pass",Pass  );// your bute[] variable Pass
              cmd.ExecuteReader();
             con.close()



Hope this may help you..........................................
 
Share this answer
 
Comments
jeetveer 12-Oct-12 13:52pm    
ok I applied it but my problem is as well as ...its not solved ......
plz help me
arunrv 17-Oct-12 6:53am    
at list tell me the exact problem your facing in detail ....
devendragohil 31-Oct-12 3:21am    
The query while creating procedure gives following error. Can anyone help?
Msg 257, Level 16, State 3, Procedure Pro_Tbl, Line 19
Implicit conversion from data type varchar to varbinary(max) is not allowed. Use the CONVERT function to run this query.
jeetveer 21-Jan-13 1:57am    
use my question code it works fine

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