I can't save on my database a picture. I receive this message when try ro run my webpage:
No overload for method 'execution' takes '7' arguments
Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class Default2 : System.Web.UI.Page
{
public string GetConnectionString()
{
return System.Configuration.ConfigurationManager.ConnectionStrings
["divin"].ConnectionString;
}
private void execution(string fname, string lname, string dob, string gender, string fathername, string contact, string address, Image photo)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
string sql = "INSERT INTO onlineapplication (fname, lname, dob, gender, fathername, contact, address,photo) VALUES "
+ " (@fname, @lname, @dob, @gender, @fathername, @contact, @address, @photo)";
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter[] pram = new SqlParameter[8];
pram[0] = new SqlParameter("@fname", SqlDbType.VarChar, 50);
pram[1] = new SqlParameter("@lname", SqlDbType.VarChar, 50);
pram[2] = new SqlParameter("@dob", SqlDbType.VarChar, 50);
pram[3] = new SqlParameter("@gender", SqlDbType.Char, 10);
pram[4] = new SqlParameter("@fathername", SqlDbType.VarChar, 50);
pram[5] = new SqlParameter("@contact", SqlDbType.Int, 20);
pram[6] = new SqlParameter("@address", SqlDbType.VarChar, 100);
pram[7] = new SqlParameter("@photo", SqlDbType.Image);
pram[0].Value = fname;
pram[1].Value = lname;
pram[2].Value = dob;
pram[3].Value = gender;
pram[4].Value = fathername;
pram[5].Value = contact;
pram[6].Value = address;
pram[7].Value = photo;
for (int i = 0; i < pram.Length; i++)
{
cmd.Parameters.Add(pram[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch(System.Data.SqlClient.SqlException ex_msg)
{
string msg = "Error occured while inserting";
msg += ex_msg.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_Click(object sender, EventArgs e)
{
if(fname.Text=="")
{
Response.Write("Please complete the form.");
}
else
{
//bug on this line.
execution(fname.Text,lname.Text,dob.Text,gender.Text,fathername.Text,contact.Text,address.Text);
conform.Visible = true;
fname.Text="";
lname.Text="";
dob.Text="";
gender.Text="";
gender.Text="";
fathername.Text="";
contact.Text="";
address.Text="";
}
}
}
///////////////////////////////////////////////////////////////////////////////////////////////////
default.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Default2" %>
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
http://www.w3.org/1999/xhtml" >
Sanjeeb LenkaPosted Aug 21, 2013, 5:43 AM
private void execution(string fname, string lname, string dob, string gender, string fathername, string contact, string address, byte[] photo)
Divin MfwambaPosted Aug 21, 2013, 5:38 AM
It's sound good. But in runing time I received these messages:
The best overloaded method match for 'Default2.execution(string, string,string,string,string,string, System.Web.UI.WebControls.Image)' has some invalid arguments
Argument '8': cannot convert from 'byte []' to 'System.Web.UI.WebControls.Image'
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
public string GetConnectionString()
{
//we will set up the configuration which will call our
//web.config file to provide the database details because
//in configuration file we have created the
//in the process we draged and droped. It creates automatically.
//We normally put the database details in web.config file or
//machine.config file because it is very sensitive information
//usually there IP address of remote database, passwords and
//user names are stored.
return System.Configuration.ConfigurationManager.ConnectionStrings
["onlineapplicationformConnectionString1"].ConnectionString;
//in above line "onlineapplicationformConnectionString1" is
//our configuration name which is inside the web.config file.
}
//private void execution(string fname, string lname, string dob, string gender, string fathername, string contact, string address)
private void execution(string fname, string lname, string dob, string gender, string fathername, string contact, string address, Image photo)
{
//In above line we declaring different variables same as backend
SqlConnection conn = new SqlConnection(GetConnectionString());
//In above line we are calling connection
//string function which is defined already on top
string sql = "INSERT INTO onlineapplication (fname, lname, dob, gender, fathername, contact, address, photo) VALUES "
//+ " (@fname, @lname, @dob, @gender, @fathername, @contact, @address)";
+" (@fname, @lname, @dob, @gender, @fathername, @contact, @address, @photo)";
//In above lines we are just storing the sql commands which
//will insert value in onlineapplication named table,
//using variable named sql.
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
//In above lines we are opening the connection to work and
//also storing connection name and sql command in cmd variable
//which has 'SqlCommand' type.
SqlParameter[] pram = new SqlParameter[8];
//In above lines we are defining 7 sql parameters will be use
//In below lines we will not disscuss about id column
pram[0] = new SqlParameter("@fname", SqlDbType.VarChar, 50);
pram[1] = new SqlParameter("@lname", SqlDbType.VarChar, 50);
pram[2] = new SqlParameter("@dob", SqlDbType.VarChar, 50);
pram[3] = new SqlParameter("@gender", SqlDbType.Char, 10);
pram[4] = new SqlParameter("@fathername", SqlDbType.VarChar, 50);
pram[5] = new SqlParameter("@contact", SqlDbType.Int, 20);
pram[6] = new SqlParameter("@address", SqlDbType.VarChar, 100);
pram[7] = new SqlParameter("@photo", SqlDbType.Image, 100);
//Now we set-uped all fiels in database in above lines
//Now we will set-up form fields
pram[0].Value = fname;
pram[1].Value = lname;
pram[2].Value = dob;
pram[3].Value = gender;
pram[4].Value = fathername;
pram[5].Value = contact;
pram[6].Value = address;
pram[7].Value = photo;
//Now create loop to insert
for (int i = 0; i < pram.Length; i++)
{
cmd.Parameters.Add(pram[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch(System.Data.SqlClient.SqlException ex_msg)
{
//Here will be catch elements
string msg = "Error occured while inserting";
msg += ex_msg.Message;
throw new Exception(msg);
}
finally
{
//Here will be fially elements
conn.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_Click(object sender, EventArgs e)
{
//Here is the command inside the click event of button
if(fname.Text=="")
{
Response.Write("Please complete the form.");
}
else
{
int length = FileUpload1.PostedFile.ContentLength;
//create a byte array to store the binary image data
byte[] imgbyte = new byte[length];
//store the currently selected file memory
HttpPostedFile img = FileUpload1.PostedFile;
//set the binary data
img.InputStream.Read(imgbyte, 0, length);
//execution(fname.Text,lname.Text,dob.Text,gender.Text,fathername.Text,contact.Text,address.Text);
execution(fname.Text, lname.Text, dob.Text, gender.Text, fathername.Text, contact.Text, address.Text, imgbyte);
conform.Visible = true;
fname.Text="";
lname.Text="";
dob.Text="";
gender.Text="";
gender.Text="";
fathername.Text="";
contact.Text="";
address.Text="";
}
}
}
Sanjeeb LenkaPosted Aug 21, 2013, 4:57 AM
you can't directly store image from file upload to database. for that you have to convert it to byte format, then store it in database.
try to do like this:
int length = FileUpload1.PostedFile.ContentLength;
//create a byte array to store the binary image data
byte[] imgbyte = new byte[length];
//store the currently selected file in memeory
HttpPostedFile img = FileUpload1.PostedFile;
//set the binary data
img.InputStream.Read(imgbyte, 0, length);
execution(fname.Text, lname.Text, dob.Text, gender.Text, fathername.Text, contact.Text, address.Text, imgbyte);
Divin MfwambaPosted Aug 21, 2013, 4:27 AM
Hi,
Before I coulnot reach the database just because the path was wrong. I recorrect it. Now it's fine. But when I run my webpage it's give these message (I am sure that I putted incorrect values for photo). Then where I colored in red it's where I am trying to correct my code. But unfortunatly nothing:
"The event 'System.Web.UI.Control.Load' can only appear on the left hand side of += or -0"
using System;
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
using System.ComponentModel;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class Default2 : System.Web.UI.Page
{
public string GetConnectionString()
{
//we will set up the configuration which will call our
//web.config file to provide the database details because
//in configuration file we have created the
//in the process we draged and droped. It creates automatically.
//We normally put the database details in web.config file or
//machine.config file because it is very sensitive information
//usually there IP address of remote database, passwords and
//user names are stored.
return System.Configuration.ConfigurationManager.ConnectionStrings
["onlineapplicationformConnectionString1"].ConnectionString;
//in above line "onlineapplicationformConnectionString1" is
//our configuration name which is inside the web.config file.
}
//private void execution(string fname, string lname, string dob, string gender, string fathername, string contact, string address)
private void execution(string fname, string lname, string dob, string gender, string fathername, string contact, string address, Image photo)
{
//In above line we declaring different variables same as backend
SqlConnection conn = new SqlConnection(GetConnectionString());
//In above line we are calling connection
//string function which is defined already on top
string sql = "INSERT INTO onlineapplication (fname, lname, dob, gender, fathername, contact, address, photo) VALUES "
//+ " (@fname, @lname, @dob, @gender, @fathername, @contact, @address)";
+" (@fname, @lname, @dob, @gender, @fathername, @contact, @address, @photo)";
//In above lines we are just storing the sql commands which
//will insert value in onlineapplication named table,
//using variable named sql.
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sql, conn);
//In above lines we are opening the connection to work and
//also storing connection name and sql command in cmd variable
//which has 'SqlCommand' type.
SqlParameter[] pram = new SqlParameter[8];
//In above lines we are defining 7 sql parameters will be use
//In below lines we will not disscuss about id column
pram[0] = new SqlParameter("@fname", SqlDbType.VarChar, 50);
pram[1] = new SqlParameter("@lname", SqlDbType.VarChar, 50);
pram[2] = new SqlParameter("@dob", SqlDbType.VarChar, 50);
pram[3] = new SqlParameter("@gender", SqlDbType.Char, 10);
pram[4] = new SqlParameter("@fathername", SqlDbType.VarChar, 50);
pram[5] = new SqlParameter("@contact", SqlDbType.Int, 20);
pram[6] = new SqlParameter("@address", SqlDbType.VarChar, 100);
pram[7] = new SqlParameter("@photo", SqlDbType.Image, 100);
//Now we set-uped all fiels in database in above lines
//Now we will set-up form fields
pram[0].Value = fname;
pram[1].Value = lname;
pram[2].Value = dob;
pram[3].Value = gender;
pram[4].Value = fathername;
pram[5].Value = contact;
pram[6].Value = address;
pram[7].Value = photo;
//Now create loop to insert
for (int i = 0; i < pram.Length; i++)
{
cmd.Parameters.Add(pram[i]);
}
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
}
catch(System.Data.SqlClient.SqlException ex_msg)
{
//Here will be catch elements
string msg = "Error occured while inserting";
msg += ex_msg.Message;
throw new Exception(msg);
}
finally
{
//Here will be fially elements
conn.Close();
}
}
protected void Page_Load(object sender, EventArgs e)
{
}
protected void submit_Click(object sender, EventArgs e)
{
//Here is the command inside the click event of button
if(fname.Text=="")
{
Response.Write("Please complete the form.");
}
else
{
//execution(fname.Text,lname.Text,dob.Text,gender.Text,fathername.Text,contact.Text,address.Text);
execution(fname.Text, lname.Text, dob.Text, gender.Text, fathername.Text, contact.Text, address.Text, FileUpload1.Load);
conform.Visible = true;
fname.Text="";
lname.Text="";
dob.Text="";
gender.Text="";
gender.Text="";
fathername.Text="";
contact.Text="";
address.Text="";
FileUpload1.Load = "";
}
}
}
Sanjeeb LenkaPosted Aug 16, 2013, 6:07 AM
check this link
http://www.aspdotnet-suresh.com/2011/03/how-to-save-images-into-folder-and.html
Divin MfwambaPosted Aug 16, 2013, 5:41 AM
Sanjeeb LenkaPosted Aug 16, 2013, 4:27 AM
your method is having 8 argument in it but you are passing 7 argument thats why it showing error.
private void execution(string fname, string lname, string dob, string gender, string fathername, string contact, string address, Image photo)
in this calling method only 7 photo is missing.
execution(fname.Text,lname.Text,dob.Text,gender.Text,fathername.Text,
contact.Text,address.Text);