1

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.

asked Apr 21, 2018 at 11:31
18
  • Please note that on Stack Overflow you should ask only one thing per Question. Someone who knows the answer to one part might not know the answer to another part. Then they may not answer or may not get the credit they deserve for their time and trouble for what they do answer. You can use the edit link to fix up your question so that it is only one topic and clear. You're very welcome to open a second Question for your other problem. Commented Apr 21, 2018 at 11:38
  • 2
    You’re not executing the sql command you build up. Commented Apr 21, 2018 at 11:46
  • 1
    You have not assigned the connection to the command. You should be getting an Exception and it is customary to include that exception detail in your question. Commented Apr 21, 2018 at 12:09
  • 1
    @JigJagJoe CommandType is Text by default. Get used to reading documentation to see what properties and methods are available. Commented Apr 21, 2018 at 12:30
  • 1
    @JigJagJoe Parameter names are case sensitive. First_name is different than First_Name. Again there should be an exception. Commented Apr 21, 2018 at 12:33

1 Answer 1

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.

answered Apr 21, 2018 at 14:33
22
  • 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> Commented Apr 21, 2018 at 15:07
  • If I give runat: server at form it says, it cannot have to runats on same form Commented 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" Commented 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. Commented 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. Commented Apr 21, 2018 at 15:33

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.