Uploading Image in registration form to Local Database












0















I want users to be able to upload an image when registering. I'm not quite getting it right and hope you can help. I'm using asp.net c# and using the local database in Visual Studio.



This is what I have so far, HTML:



<tr>
<td class="auto-style3">Image </td>
<td class="auto-style4">
<asp:FileUpload ID="FileUpload1" runat="server" />


Backend:



protected void Submit_Click(object sender, EventArgs e)
{


SqlConnection conn = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM Users WHERE ([Username] = @user)", conn);
check_User_Name.Parameters.AddWithValue("@user", TextBoxUN.Text);
int UserExist = (int)check_User_Name.ExecuteScalar();

if (UserExist > 0)
{
Response.Write("Username already exists");

}
else
{

string insertQuery = "insert into Users (FirstName,SecondName,UserName,Password,Email,ProductOwner,ScrumMaster,Developer,ConfirmPassword, Image) values(@FName,@SName,@UserName,@Password,@Email,@ProductOwner,@ScrumMaster,@Developer,@ConfirmPassword,@Image)";
SqlCommand com = new SqlCommand(insertQuery, conn);

com.Parameters.AddWithValue("@FName", TextBoxFN.Text);
com.Parameters.AddWithValue("@SName", TextBoxSN.Text);
com.Parameters.AddWithValue("@UserName", TextBoxUN.Text);
com.Parameters.AddWithValue("@Password", TextBoxPass.Text);
com.Parameters.AddWithValue("@ConfirmPassword", TextBoxConPass.Text);
com.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@ProductOwner", CheckBoxProduct.Checked.ToString());
com.Parameters.AddWithValue("@ScrumMaster", CheckBoxScrum.Checked.ToString());

com.Parameters.AddWithValue("@Developer", CheckBoxDeveloper.Checked.ToString());
com.Parameters.Add("@Image");
</td>
</tr>









share|improve this question

























  • what problem errors are you getting?

    – Simon Price
    Nov 22 '18 at 13:17
















0















I want users to be able to upload an image when registering. I'm not quite getting it right and hope you can help. I'm using asp.net c# and using the local database in Visual Studio.



This is what I have so far, HTML:



<tr>
<td class="auto-style3">Image </td>
<td class="auto-style4">
<asp:FileUpload ID="FileUpload1" runat="server" />


Backend:



protected void Submit_Click(object sender, EventArgs e)
{


SqlConnection conn = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM Users WHERE ([Username] = @user)", conn);
check_User_Name.Parameters.AddWithValue("@user", TextBoxUN.Text);
int UserExist = (int)check_User_Name.ExecuteScalar();

if (UserExist > 0)
{
Response.Write("Username already exists");

}
else
{

string insertQuery = "insert into Users (FirstName,SecondName,UserName,Password,Email,ProductOwner,ScrumMaster,Developer,ConfirmPassword, Image) values(@FName,@SName,@UserName,@Password,@Email,@ProductOwner,@ScrumMaster,@Developer,@ConfirmPassword,@Image)";
SqlCommand com = new SqlCommand(insertQuery, conn);

com.Parameters.AddWithValue("@FName", TextBoxFN.Text);
com.Parameters.AddWithValue("@SName", TextBoxSN.Text);
com.Parameters.AddWithValue("@UserName", TextBoxUN.Text);
com.Parameters.AddWithValue("@Password", TextBoxPass.Text);
com.Parameters.AddWithValue("@ConfirmPassword", TextBoxConPass.Text);
com.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@ProductOwner", CheckBoxProduct.Checked.ToString());
com.Parameters.AddWithValue("@ScrumMaster", CheckBoxScrum.Checked.ToString());

com.Parameters.AddWithValue("@Developer", CheckBoxDeveloper.Checked.ToString());
com.Parameters.Add("@Image");
</td>
</tr>









share|improve this question

























  • what problem errors are you getting?

    – Simon Price
    Nov 22 '18 at 13:17














0












0








0








I want users to be able to upload an image when registering. I'm not quite getting it right and hope you can help. I'm using asp.net c# and using the local database in Visual Studio.



This is what I have so far, HTML:



<tr>
<td class="auto-style3">Image </td>
<td class="auto-style4">
<asp:FileUpload ID="FileUpload1" runat="server" />


Backend:



protected void Submit_Click(object sender, EventArgs e)
{


SqlConnection conn = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM Users WHERE ([Username] = @user)", conn);
check_User_Name.Parameters.AddWithValue("@user", TextBoxUN.Text);
int UserExist = (int)check_User_Name.ExecuteScalar();

if (UserExist > 0)
{
Response.Write("Username already exists");

}
else
{

string insertQuery = "insert into Users (FirstName,SecondName,UserName,Password,Email,ProductOwner,ScrumMaster,Developer,ConfirmPassword, Image) values(@FName,@SName,@UserName,@Password,@Email,@ProductOwner,@ScrumMaster,@Developer,@ConfirmPassword,@Image)";
SqlCommand com = new SqlCommand(insertQuery, conn);

com.Parameters.AddWithValue("@FName", TextBoxFN.Text);
com.Parameters.AddWithValue("@SName", TextBoxSN.Text);
com.Parameters.AddWithValue("@UserName", TextBoxUN.Text);
com.Parameters.AddWithValue("@Password", TextBoxPass.Text);
com.Parameters.AddWithValue("@ConfirmPassword", TextBoxConPass.Text);
com.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@ProductOwner", CheckBoxProduct.Checked.ToString());
com.Parameters.AddWithValue("@ScrumMaster", CheckBoxScrum.Checked.ToString());

com.Parameters.AddWithValue("@Developer", CheckBoxDeveloper.Checked.ToString());
com.Parameters.Add("@Image");
</td>
</tr>









share|improve this question
















I want users to be able to upload an image when registering. I'm not quite getting it right and hope you can help. I'm using asp.net c# and using the local database in Visual Studio.



This is what I have so far, HTML:



<tr>
<td class="auto-style3">Image </td>
<td class="auto-style4">
<asp:FileUpload ID="FileUpload1" runat="server" />


Backend:



protected void Submit_Click(object sender, EventArgs e)
{


SqlConnection conn = new SqlConnection (ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
conn.Open();
SqlCommand check_User_Name = new SqlCommand("SELECT COUNT(*) FROM Users WHERE ([Username] = @user)", conn);
check_User_Name.Parameters.AddWithValue("@user", TextBoxUN.Text);
int UserExist = (int)check_User_Name.ExecuteScalar();

if (UserExist > 0)
{
Response.Write("Username already exists");

}
else
{

string insertQuery = "insert into Users (FirstName,SecondName,UserName,Password,Email,ProductOwner,ScrumMaster,Developer,ConfirmPassword, Image) values(@FName,@SName,@UserName,@Password,@Email,@ProductOwner,@ScrumMaster,@Developer,@ConfirmPassword,@Image)";
SqlCommand com = new SqlCommand(insertQuery, conn);

com.Parameters.AddWithValue("@FName", TextBoxFN.Text);
com.Parameters.AddWithValue("@SName", TextBoxSN.Text);
com.Parameters.AddWithValue("@UserName", TextBoxUN.Text);
com.Parameters.AddWithValue("@Password", TextBoxPass.Text);
com.Parameters.AddWithValue("@ConfirmPassword", TextBoxConPass.Text);
com.Parameters.AddWithValue("@Email", TextBoxEmail.Text);
com.Parameters.AddWithValue("@ProductOwner", CheckBoxProduct.Checked.ToString());
com.Parameters.AddWithValue("@ScrumMaster", CheckBoxScrum.Checked.ToString());

com.Parameters.AddWithValue("@Developer", CheckBoxDeveloper.Checked.ToString());
com.Parameters.Add("@Image");
</td>
</tr>






c# asp.net webforms






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 13:16









John

13k32242




13k32242










asked Nov 22 '18 at 13:15









monkey232monkey232

326




326













  • what problem errors are you getting?

    – Simon Price
    Nov 22 '18 at 13:17



















  • what problem errors are you getting?

    – Simon Price
    Nov 22 '18 at 13:17

















what problem errors are you getting?

– Simon Price
Nov 22 '18 at 13:17





what problem errors are you getting?

– Simon Price
Nov 22 '18 at 13:17












1 Answer
1






active

oldest

votes


















2














you need to save the image on disk:



if (FileUpload1.HasFile)
{
// you have to create a folder in which to store the images
var savePath = Path.Combine(Server.MapPath("~/UserImages"), FileUpload1.FileName;
FileUpload1.SaveAs(savePath);
}


you need to save the file name in the database:



com.Parameters.Add("@Image", FileUpload1.FileName);


You have to make sure that the folder does not contain a file with the same name.






share|improve this answer


























  • Thank you! I think I'm getting somewhere although I'm getting this error when clicking submit: System.Data.SqlClient.SqlException: 'Operand type clash: nvarchar is incompatible with image'

    – monkey232
    Nov 22 '18 at 14:16








  • 1





    You get this error because you specified in SQL that the column is of "image" type, this means that you store the actual image in the database which is not recommended. A best practice is to store the image files on disk and in the database a reference to the file (like the filename). For this to work you need to alter the column Image and change the type from "image" to "nvarchar"

    – Sergiu Muresan
    Nov 22 '18 at 14:20











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431851%2fuploading-image-in-registration-form-to-local-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














you need to save the image on disk:



if (FileUpload1.HasFile)
{
// you have to create a folder in which to store the images
var savePath = Path.Combine(Server.MapPath("~/UserImages"), FileUpload1.FileName;
FileUpload1.SaveAs(savePath);
}


you need to save the file name in the database:



com.Parameters.Add("@Image", FileUpload1.FileName);


You have to make sure that the folder does not contain a file with the same name.






share|improve this answer


























  • Thank you! I think I'm getting somewhere although I'm getting this error when clicking submit: System.Data.SqlClient.SqlException: 'Operand type clash: nvarchar is incompatible with image'

    – monkey232
    Nov 22 '18 at 14:16








  • 1





    You get this error because you specified in SQL that the column is of "image" type, this means that you store the actual image in the database which is not recommended. A best practice is to store the image files on disk and in the database a reference to the file (like the filename). For this to work you need to alter the column Image and change the type from "image" to "nvarchar"

    – Sergiu Muresan
    Nov 22 '18 at 14:20
















2














you need to save the image on disk:



if (FileUpload1.HasFile)
{
// you have to create a folder in which to store the images
var savePath = Path.Combine(Server.MapPath("~/UserImages"), FileUpload1.FileName;
FileUpload1.SaveAs(savePath);
}


you need to save the file name in the database:



com.Parameters.Add("@Image", FileUpload1.FileName);


You have to make sure that the folder does not contain a file with the same name.






share|improve this answer


























  • Thank you! I think I'm getting somewhere although I'm getting this error when clicking submit: System.Data.SqlClient.SqlException: 'Operand type clash: nvarchar is incompatible with image'

    – monkey232
    Nov 22 '18 at 14:16








  • 1





    You get this error because you specified in SQL that the column is of "image" type, this means that you store the actual image in the database which is not recommended. A best practice is to store the image files on disk and in the database a reference to the file (like the filename). For this to work you need to alter the column Image and change the type from "image" to "nvarchar"

    – Sergiu Muresan
    Nov 22 '18 at 14:20














2












2








2







you need to save the image on disk:



if (FileUpload1.HasFile)
{
// you have to create a folder in which to store the images
var savePath = Path.Combine(Server.MapPath("~/UserImages"), FileUpload1.FileName;
FileUpload1.SaveAs(savePath);
}


you need to save the file name in the database:



com.Parameters.Add("@Image", FileUpload1.FileName);


You have to make sure that the folder does not contain a file with the same name.






share|improve this answer















you need to save the image on disk:



if (FileUpload1.HasFile)
{
// you have to create a folder in which to store the images
var savePath = Path.Combine(Server.MapPath("~/UserImages"), FileUpload1.FileName;
FileUpload1.SaveAs(savePath);
}


you need to save the file name in the database:



com.Parameters.Add("@Image", FileUpload1.FileName);


You have to make sure that the folder does not contain a file with the same name.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 13:32

























answered Nov 22 '18 at 13:22









Sergiu MuresanSergiu Muresan

3346




3346













  • Thank you! I think I'm getting somewhere although I'm getting this error when clicking submit: System.Data.SqlClient.SqlException: 'Operand type clash: nvarchar is incompatible with image'

    – monkey232
    Nov 22 '18 at 14:16








  • 1





    You get this error because you specified in SQL that the column is of "image" type, this means that you store the actual image in the database which is not recommended. A best practice is to store the image files on disk and in the database a reference to the file (like the filename). For this to work you need to alter the column Image and change the type from "image" to "nvarchar"

    – Sergiu Muresan
    Nov 22 '18 at 14:20



















  • Thank you! I think I'm getting somewhere although I'm getting this error when clicking submit: System.Data.SqlClient.SqlException: 'Operand type clash: nvarchar is incompatible with image'

    – monkey232
    Nov 22 '18 at 14:16








  • 1





    You get this error because you specified in SQL that the column is of "image" type, this means that you store the actual image in the database which is not recommended. A best practice is to store the image files on disk and in the database a reference to the file (like the filename). For this to work you need to alter the column Image and change the type from "image" to "nvarchar"

    – Sergiu Muresan
    Nov 22 '18 at 14:20

















Thank you! I think I'm getting somewhere although I'm getting this error when clicking submit: System.Data.SqlClient.SqlException: 'Operand type clash: nvarchar is incompatible with image'

– monkey232
Nov 22 '18 at 14:16







Thank you! I think I'm getting somewhere although I'm getting this error when clicking submit: System.Data.SqlClient.SqlException: 'Operand type clash: nvarchar is incompatible with image'

– monkey232
Nov 22 '18 at 14:16






1




1





You get this error because you specified in SQL that the column is of "image" type, this means that you store the actual image in the database which is not recommended. A best practice is to store the image files on disk and in the database a reference to the file (like the filename). For this to work you need to alter the column Image and change the type from "image" to "nvarchar"

– Sergiu Muresan
Nov 22 '18 at 14:20





You get this error because you specified in SQL that the column is of "image" type, this means that you store the actual image in the database which is not recommended. A best practice is to store the image files on disk and in the database a reference to the file (like the filename). For this to work you need to alter the column Image and change the type from "image" to "nvarchar"

– Sergiu Muresan
Nov 22 '18 at 14:20




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53431851%2fuploading-image-in-registration-form-to-local-database%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

How to fix TextFormField cause rebuild widget in Flutter

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith