Reputation: 45
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.
Upvotes: 1
Views: 79
Reputation: 2018
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.
Upvotes: 1