Please how can i create login and registration form for many users with c#. Thanks.
4 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Yogesh Sharma123 reputationPosted Feb 25, 2016, 11:22 PM EST
✓ Accepted answer+1
Hi,,,,
You have to create class with insert method like...
datalayer class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
///
/// Summary description for Datalayer
///
///
public class Datalayer
{
SqlConnection con;
SqlCommand cmd;
SqlDataAdapter da;
public Datalayer()
{
con = new SqlConnection(ConfigurationManager.ConnectionStrings["name"].ConnectionString);
}
public void insertdata(String query)
{
cmd = new SqlCommand(query,con);
try
{
con.Open();
cmd.ExecuteNonQuery();
}
catch (SqlException err)
{
throw err;
}
catch (Exception err1)
{
throw err1;
}
finally
{
con.Close();
}
}
finally
{
con.Close();
cmd.Dispose();
con.Dispose();
}
}
public string Bind_sites()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["Drishti"].ConnectionString);
SqlCommand cmd = new SqlCommand("insert into Sites values('" + Acc_Email_ID + "','" + Sites + "')", con);
cmd.CommandType = CommandType.Text;
try
{
con.Open();
cmd.ExecuteNonQuery();
return "Site Stored";
}
catch (SqlException err)
{
throw err;
}
catch (Exception err1)
{
throw err1;
}
finally
{
con.Close();
cmd.Dispose();
con.Dispose();
}
}
}
create your reg. form
on button click event write code :
call datalayer method named insertdate as well :
datalayer dl = new datalayer();
string query;
query = string.format ("insert into table values('0')",'"+txtvalue.text+"');
dl.insertdata(query);
This method is very useful for you because you can create no of methods in it...
and call once on page
methods like inserdata, delete data, get data, get data by reader, etc...
Thank you...
Hope you will enjoy this type of method ...... to insert data in sql table....
+1
Amit Kumar SinghRank #562 · 4.3K reputationPosted Feb 25, 2016, 11:11 PM EST
+1
using System; using System.Collections; using System.Configuration; using System.Data; using System.Linq; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.HtmlControls; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Xml.Linq; using System.Data.SqlClient; namespace Registration { public partial class _Default : System.Web.UI.Page { string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString; SqlCommand com; string str;
Yogesh Sharma123 reputationPosted Feb 25, 2016, 11:22 PM EST
Amit Kumar SinghRank #562 · 4.3K reputationPosted Feb 25, 2016, 11:11 PM EST
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Registration
{
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
string str;
protected void btnadd_Click(object sender, EventArgs e)
{
ExecuteInsert(RegistrationText.Text, MakeText.Text, ModelText.Text, FuelTypeText.Text, CapacityText.Text, BhpText.Text, MileageText.Text);
Response.Write("Record was successfully added!");
clear();
}
private void ExecuteInsert(string RegistrationNumber, string Make, string Model, string FuelType, string Capacity, string Power, string Mileage)
{
SqlConnection con = new SqlConnection(connStr);
str = "INSERT INTO Vehicles (RegistrationNumber, Make, Model, FuelType, Capacity, Power, Mileage) VALUES (@RegistrationNumber, @Make, @Model, @FuelType, @Capacity, @Power, @Mileage)";
try
{
con.Open();
com = new SqlCommand(str, con);
SqlParameter[] param = new SqlParameter[7];
param[0] = new SqlParameter("@RegistrationNumber", SqlDbType.NVarChar, 10);
param[1] = new SqlParameter("@Make", SqlDbType.NVarChar, 30);
param[2] = new SqlParameter("@Model", SqlDbType.NVarChar, 30);
param[3] = new SqlParameter("@FuelType", SqlDbType.NVarChar, 10);
param[4] = new SqlParameter("@Capacity", SqlDbType.Int, 4);
param[5] = new SqlParameter("@Power", SqlDbType.Int, 4);
param[6] = new SqlParameter("@Mileage", SqlDbType.Int, 8);
param[0].Value = RegistrationNumber;
param[1].Value = Make;
param[2].Value = Model;
param[3].Value = FuelType;
param[4].Value = Capacity;
param[5].Value = Power;
param[6].Value = Mileage;
for (int i = 0; i < param.Length; i++)
{
com.Parameters.Add(param[i]);
}
com.CommandType = CommandType.Text;
com.ExecuteNonQuery();
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
con.Close();
}
}
void clear()
{
RegistrationText.Text = "";
MakeText.Text = "";
ModelText.Text = "";
FuelTypeText.Text = "";
CapacityText.Text = "";
BhpText.Text = "";
MileageText.Text = "";
}
}
Shubham KumarRank #621 · 3.8K reputationPosted Feb 25, 2016, 11:09 PM EST
Krishna Rajput SinghRank #605 · 4K reputationPosted Feb 25, 2016, 11:39 PM EST
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questionsTop experts