I am working with ASP.NET and using C# to build a website project. I am having problem with connecting to database(inserting)/SQL database while making a register and login form.
I am giving my code below:
Register form:
<div id="Sign_Up_Form" style="display: none">
<form class="modal-content animate" method="post" action="">
<div class="input">
<div>
<div>Name</div>
<asp:TextBox CssClass="signUp_input" ID="First_Name" runat="server" placeholder="First Name" style="height: 20px; padding-left: 1px;"></asp:TextBox>
<asp:TextBox CssClass="signUp_input" ID="Last_Name" runat="server" placeholder="Last Name" style="height: 20px; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Email</div>
<asp:TextBox CssClass="signUp_input" ID="Regi_Email" runat="server" placeholder="Your email here" style="height: 20px; width: 70%; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Password</div>
<asp:TextBox CssClass="signUp_input" ID="Regi_Password" type="password" runat="server" placeholder="**********" style="height: 20px; width: 70%; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Confirm Password</div>
<asp:TextBox CssClass="signUp_input" ID="Confirm_Regi_Password" type="password" runat="server" placeholder="**********" style="height: 20px; width: 70%; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Age</div>
<asp:TextBox CssClass="signUp_input" ID="Age" type="number" runat="server" placeholder="e.g. 21" style="height: 20px; width: 70%; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Gender:</div>
<asp:RadioButtonList CssClass="radio" ID="Gender" runat="server">
<asp:ListItem Value="1">Male</asp:ListItem>
<asp:ListItem Value="2">Female</asp:ListItem>
<asp:ListItem Value="3">Other</asp:ListItem>
</asp:RadioButtonList>
</br>
</div>
</div>
<asp:Button ID="Register_Button" runat="server" value="Join" OnClick="userRegister" CssClass="button_join" Text="Join" />
</form>
I am not sure if I should give the post method here in the form tags.
The userRegister method is (The edited one, after I got suggestions):
protected void userRegister(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["Khulna_website"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
con.Open();
string insertQuery = "insert into dbo.users(user_f_name,user_l_name,user_password,user_email,user_age, user_gender) values (@First_Name, @Last_Name, @Regi_Password, @Regi_Email, @Age, @Gender);";
SqlCommand com = con.CreateCommand();
com.CommandText= insertQuery;
com.Parameters.AddWithValue("@First_Name", First_Name.Text);
com.Parameters.AddWithValue("@Last_Name", Last_Name.Text);
com.Parameters.AddWithValue("@Regi_Password", Regi_Password.Text);
com.Parameters.AddWithValue("@Regi_Email", Regi_Email.Text);
com.Parameters.AddWithValue("@Age", Age.Text);
com.Parameters.AddWithValue("@Gender", First_Name.Text);
com.ExecuteNonQuery();
con.Close();
Response.Write("Registration is Successful!");
}
My database table code is:
CREATE TABLE [dbo].[users] (
[user_id] INT IDENTITY (1, 1) NOT NULL,
[user_f_name] VARCHAR (15) NOT NULL,
[user_l_name] VARCHAR (15) NOT NULL,
[user_password] VARCHAR (20) NOT NULL,
[user_email] VARCHAR (30) NOT NULL,
[user_age] INT NOT NULL,
[user_gender] INT NOT NULL,
[user_profession] VARCHAR (20) NULL,
[user_about] VARCHAR (200) NULL,
PRIMARY KEY CLUSTERED ([user_email] ASC)
);
Fact#1 I created the database with add new item-> SQL database. It is created under AppData. Then I again inserted a connectionString under Web.config file. When I fill up the form and submit it, then I see the green color of the connection beside my database (naming dabase.mdf) is gone, and it is red, and I need to refresh it to be green again.
Fact#2 I tried to write the response.write() code in the beginning at the start. But there was no change.
Problem is: Data is not getting inserted. Is either of the facts the reason? Or is there any other problem here that I am missing out?
P.S. I am completely new on this topic and any help is highly appreciated.
edit: I actually have one problem with two facts.. I found out the facts while I was trying to see the root of the problem.
1 Answer 1
In web.config
, your connection string should be something like:
<connectionStrings>
<add name="Khulna_website" connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|Database1.mdf;Integrated Security=True"/>
</connectionStrings>
WebForm:
<form id="form1" runat="server">
<div class="input">
<div>
<div>Name</div>
<asp:TextBox CssClass="signUp_input" ID="First_Name" runat="server" placeholder="First Name" Style="height: 20px; padding-left: 1px;"></asp:TextBox>
<asp:TextBox CssClass="signUp_input" ID="Last_Name" runat="server" placeholder="Last Name" Style="height: 20px; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Email</div>
<asp:TextBox CssClass="signUp_input" ID="Regi_Email" runat="server" placeholder="Your email here" Style="height: 20px; width: 70%; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Password</div>
<asp:TextBox CssClass="signUp_input" ID="Regi_Password" type="password" runat="server" placeholder="**********" Style="height: 20px; width: 70%; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Confirm Password</div>
<asp:TextBox CssClass="signUp_input" ID="Confirm_Regi_Password" type="password" runat="server" placeholder="**********" Style="height: 20px; width: 70%; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Age</div>
<asp:TextBox CssClass="signUp_input" ID="Age" type="number" runat="server" placeholder="e.g. 21" Style="height: 20px; width: 70%; padding-left: 1px;"></asp:TextBox>
</div>
<div>
<div>Gender:</div>
<asp:RadioButtonList CssClass="radio" ID="Gender" runat="server">
<asp:ListItem Value="1">Male</asp:ListItem>
<asp:ListItem Value="2">Female</asp:ListItem>
<asp:ListItem Value="3">Other</asp:ListItem>
</asp:RadioButtonList>
<br/>
</div>
</div>
<asp:Button ID="Register_Button" runat="server" value="Join" OnClick="userRegister" CssClass="button_join" Text="Join" />
</form>
Code-behind:
protected void userRegister(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["Khulna_website"].ConnectionString;
using (SqlConnection connection = new SqlConnection(constr))
{
string insertQuery = "insert into dbo.users(user_f_name,user_l_name,user_password,user_email,user_age, user_gender) values (@First_Name, @Last_Name, @Regi_Password, @Regi_Email, @Age, @Gender);";
SqlCommand com = new SqlCommand(insertQuery, connection);
connection.Open();
com.Parameters.AddWithValue("@First_Name", First_Name.Text);
com.Parameters.AddWithValue("@Last_Name", Last_Name.Text);
com.Parameters.AddWithValue("@Regi_Password", Regi_Password.Text);
com.Parameters.AddWithValue("@Regi_Email", Regi_Email.Text);
com.Parameters.AddWithValue("@Age", Age.Text);
com.Parameters.AddWithValue("@Gender", Gender.SelectedValue);
com.ExecuteNonQuery();
}
}
It should work.
-
my web.config file is <connectionStrings> <add name="Khulna_website" connectionString= "Server=(localDB)\\v11.0;Integrated Security=SSPI; Database=Database.mdf;" providerName="System.Data.SqlClient" /> </connectionStrings>JigJagJoe– JigJagJoe2018年04月21日 15:07:02 +00:00Commented Apr 21, 2018 at 15:07
-
If I give runat: server at form it says, it cannot have to runats on same formJigJagJoe– JigJagJoe2018年04月21日 15:10:42 +00:00Commented Apr 21, 2018 at 15:10
-
I am even doubting that if it is even entering the method. If it did, at least it would respond "Registration is successful"JigJagJoe– JigJagJoe2018年04月21日 15:18:13 +00:00Commented Apr 21, 2018 at 15:18
-
@JigJagJoe Please, replace "Database=Database.mdf" with "AttachDBFilename=|DataDirectory|\Database.mdf" in your connection string (without the quotes). Please, add a new WebForm to your project and copy and paste the code from my answer. Run and then open the database table to confirm data have been inserted. If everything works fine, go back to your original WebForm and make sure you have only one form tag with "runat=server" and you have the code I provided in your "userRegister" button event handler. Take ONE step at a time to rule errors out.dpant– dpant2018年04月21日 15:30:06 +00:00Commented Apr 21, 2018 at 15:30
-
1@JigJagJoe If the name of the .mdf file in the App_Data folder is "database1.mdf" then your localdb connection string should contain: "AttachDBFilename=|DataDirectory|database1.mdf". |DataDirectory| is special syntax pointing to the App_Data folder of your project.dpant– dpant2018年04月21日 15:33:16 +00:00Commented Apr 21, 2018 at 15:33
CommandType
isText
by default. Get used to reading documentation to see what properties and methods are available.First_name
is different thanFirst_Name
. Again there should be an exception.