Search
MVC Online Training
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Forums Author
Wed, 17-Sep-2014 Authors
Vpmanoharan
20
Pohon
10
Raj.Trivedi
10
All Time Authors
Jayakumars
4170
Karthikanbarasan
3455
Chvrsri
3140

Latest members | More ...


(Statistics delayed by 5 minutes)
 Forum Post

how to add questions into database for an online examination? [Resolved]

Tejamanimala
Posted by Tejamanimala under ASP.NET on 3/15/2013 8:21:02 AM | Points: 10 | Views : 3705 | Status : [Member] | Replies : 40


Can any one please suggest me how to add questions into database,i need the source code and c# code,because am new to the .net

manimala

Posted by: Karthikreddy on: 3/15/2013 [Member] Starter | Points: 25

http://www.dotnetfunda.com/codes/code4261-online-test-and-instant-results.aspx

find the attachment

 Download source file

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/18/2013 [Member] Starter | Points: 25

hi karthik thank you for your reply its so help full for me,and your code is for question paper and results na,but i want to add questions first into database.
actually exam instructor will be going to add the questions into database,when student will come for exam,then instructor retrieve that questions from database,so can you please tell me how to add questions into database,and when instructor add these questions,if he want to see the questions he could see that questions with view questions hyperlink,and in view questions hyperlink there are some domains like .net,java,testing tools,sql,oracle,when he clicks on the domain it would be display that domain questions.I am requesting you to please tell me the code.

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/18/2013 [Member] Starter | Points: 25

Find the attachment

 Download source file

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/19/2013 [Member] Starter | Points: 25

Hi,thank you,but it is not adding questions na,please tell me the code for adding questions

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/19/2013 [Member] Starter | Points: 25

Find the attachment

you can add directly with user interface

 Download source file

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/19/2013 [Member] Starter | Points: 25

i didn't get your point

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/19/2013 [Member] Starter | Points: 25

we can insert the question using insert command into database (Like registration ) download the attached image in the previous replay

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Brahmam on: 3/19/2013 [Member] Starter | Points: 25

Once check it

objclsQuestion = new clsQuestion();

if (FileUpload1.FileName != "")
{
FileUpload1.PostedFile.SaveAs(ConfigurationManager.AppSettings["ImgQuestionPath"].ToString() + FileUpload1.FileName);
}


//To validate dynamic controls
string[] choice = new string[countControl];
int flagEmpty = 0;
for (int i = 1; i <= countControl; i++)
{
//choice[i] = text.Text;
text = PlaceHolder1.FindControl("txtChoice" + i.ToString()) as TextBox;
label = PlaceHolder1.FindControl("Choice" + i.ToString()) as Label;
if (text.Text.Trim() == "")
{
flagEmpty += 1;
lblReturnValue.Text = "Choices should not empty. and ";
text.Text = "";
text.Focus();
}
//added on 27th march
else
{
choice[i - 1] = text.Text.Trim();
}
}



flagSameChoice = 0;
for (int i = 0; i <= countControl; i++)
{
for (int j = i + 1; j <= countControl - 1; j++)
{
if (choice[i] == choice[j])
{
lblReturnValue.Text = "Choices should not same";
flagSameChoice = 1;
}
if ((choice[i] != "") || (choice[j]) != "") //Added on 11th July, 2008
flagSameChoice = 0;
}
}

if ((rblChoice.SelectedIndex == -1)) //if Answer was not select or any empty choice is there.
{
lblReturnValue.Text = "Plz Provide data for all the fields";
}
else
if (flagSameChoice == 0) //To check weather any one of the Choices are same.
{
//To Insert in Question Tbl.

ViewState["LevelName"] = ddlLevelName.SelectedItem.Value;
//ViewState["TopicName"] = ddlTopicName.SelectedItem.Value;
ViewState["SubTopicName"] = ddlSubTopicName.SelectedItem.Value;

returnValue = objclsQuestion.InsertQuestion3(txtQuestion.Text, ViewState["LevelName"].ToString(), ViewState["SubTopicName"].ToString(), FileUpload1.FileName.ToString());
if (returnValue == "") //If Question is not duplicate, Added on 10th June, 2008 by Sateesh
{
//To Insert in Choice Tbl.
// get values from dynamic controls
for (int i = 1; i <= countControl; i++)
{
text = PlaceHolder1.FindControl("txtChoice" + i.ToString()) as TextBox;
label = PlaceHolder1.FindControl("Choice" + i.ToString()) as Label;
if (text.Text != "") //if (text != null) //Modified on 12th July,2008 at 2am
{
objclsQuestion.InsertChoice_Details2(label.ID, text.Text);
}
}

//To Insert in Answer Tbl.
rblChoice = PlaceHolder1.FindControl("rblChoice") as RadioButtonList;
objclsQuestion.InsertAnswer22(rblChoice.SelectedItem.Value.ToString());

//Response.Redirect("~/Admin/ViewQuestion.aspx");
Server.Transfer("ViewQuestion.aspx");
}
else
{
lblReturnValue.Text = returnValue;
}
}
else
{
lblReturnValue.Text = "Choices should not same";
}
}
catch (Exception ex)
{
lblReturnValue.Text = ex.Message;
}
}

brahmam

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

Hi karthik,I didn't understand second image 2.png,and what is the use if username here,because,username is not unique right,then what is the use,if you don't mind can you explain me theoretically,because am new to programming.

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

hi
img 2.png is Result table

in this table we are storing user selected answer(i.e ans ,0 as false ans and 1 as true ans) in the exam
while showing the result we count number of 1's with respect the userName


UserName: it might be any thing userName or UserID Its a foregen Key to user Registration table

if we want get results of particular user.we need userID or Username in result table that's why i used UserName filed

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

Hi,
in c# coding,there is SQL connection na,in that data source=" ",and initial catalog=" " is what,in my system where should i find the data source and initial catalog?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

Data Source is sever name or else 'localhost' Initial Catalog is our database name

can you replay your connection string

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

hi,thank you this is my connection string
SqlConnection con = new SqlConnection("user id=sa;password=Ektha@2013;Data Source=EKTHA-3D34;Initial Catalog=OnlineExam.dbo");

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

Hi Karthik,i want to display 10 questions in my page,i inserted 10 questions into database,but when i executing it,it didn't display 10 questions,it displaying only 2 questions?what should i do?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

you need to add 10 labels for question and respective radio buttons
in my code i was added only two labels only .if you used my code please make changes in code behind (add more 8 labels and radio buttons )

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

k Thank you so much.and i have another doubt that is i want to add sql,oracle,.net java questions,i mean for separate domains,i have idea that,i have to use dropdownlist which is containing domains,when i select that how should i display questions,can you please tell me how it is possible?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

same concept like userName in result table

add a domain name field in the table while displaying question use where condition in sql query (where domain='java')

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

Hi karthik,here what is the use of submit,when i click on submit,it doesn't showing any action,and how to display question no's?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

Hi Karthik,i want to store the total score of a exam in results page,i don't want to store the each and every answer,just i want to store the total score with user id,with date and time,can you please tell me the code for this

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

hi karthik,when i entered 10 questions,and i tick the correct answers,but,the score is only getting 2,why,i tick the taotal 10 questions correctly?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

when we click submit button the result will be stored in result table

you can assign question number manually before the question label it would be better rather than retrieving from db

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

under submit_button click i was written only for two question you can write for reaming 8 questions

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

k,if i want to use 30 questions,then also i need to write that 30 times ha?
and i want to store total score in results table,i don't want to use question and ans,how should create it?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

Yes,if you want retrieve question number from database you need to another label before that question label to display question number and question number may vary for domain

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

Add this code under button click


int i=0;
if ((RadioButtonList1.SelectedValue) == ds.Tables[0].Rows[0]["CurrectOption"].ToString())
{
i++;
// Response.Write("Correct ");
//SqlCommand cmd1 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[0]["Quetion"].ToString() + "','"+1+"')", con);
//cmd1.ExecuteNonQuery();

}
else
{


//SqlCommand cmd1 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[0]["Quetion"].ToString() + "','" + 0 + "')", con);
//cmd1.ExecuteNonQuery();
}
if ((RadioButtonList2.SelectedValue) == ds.Tables[0].Rows[1]["CurrectOption"].ToString())
{
i++;
//SqlCommand cmd2 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[1]["Quetion"].ToString() + "','" + 1 + "')", con);
//cmd2.ExecuteNonQuery();
}
else
{
//SqlCommand cmd2 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[1]["Quetion"].ToString() + "','" + 0 + "')", con);
//cmd2.ExecuteNonQuery();

}
if ((RadioButtonList3.SelectedValue) == ds.Tables[0].Rows[2]["CurrectOption"].ToString())
{
i++;
// Response.Write("Correct ");
//SqlCommand cmd3 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[2]["Quetion"].ToString() + "','" + 1 + "')", con);
//cmd3.ExecuteNonQuery();

}
else
{

//SqlCommand cmd3 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[2]["Quetion"].ToString() + "','" + 0 + "')", con);
//cmd3.ExecuteNonQuery();
}
if ((RadioButtonList4.SelectedValue) == ds.Tables[0].Rows[3]["CurrectOption"].ToString())
{
i++;
// Response.Write("Correct ");
//SqlCommand cmd4 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[3]["Quetion"].ToString() + "','" + 1 + "')", con);
//cmd4.ExecuteNonQuery();


}
else
{

//SqlCommand cmd4 = new SqlCommand("insert into Result (UserName,Question,ans) values('" + TextBox1.Text + "','" + ds.Tables[0].Rows[3]["Quetion"].ToString() + "','" + 0 + "')", con);
//cmd4.ExecuteNonQuery();
}
SqlCommand cmd1 = new SqlCommand("insert into Result (UserName,date,ans) values('" + TextBox1.Text + "','"+DateTime.Now.ToLongTimeString()+"','"+i+"')", con);
cmd1.ExecuteNonQuery();
//ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Congratulations " + TextBox1.Text + " Your Score is " + i + "')", true);
con.Close();


instead of inserting question and correct and we are increasing i value by one when user select correct ans (make changes in table question as Date)

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

SqlCommand cmd1 = new SqlCommand("insert into Result (UserName,date,ans) values('" + TextBox1.Text + "','"+DateTime.Now.ToLongTimeString()+"','"+i+"')", con);

cmd1.ExecuteNonQuery();
as this we need to write 10 cmd's ha?for 10 questions,and here,i showing an error

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

hi,
again am getting result 0

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 50

Resolved

if you want to display 10 questions in one page mean you need to add 10 querys

there is an another way to use single query .u can display single question after selecting that question u need to display another question using for loop
it may solve u r problem

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

add under result button
user textbox to get userName


if (TextBox1.Text != "")
{
con.Open();
SqlCommand cmd3 = new SqlCommand("select ans from Result where UserName='" + TextBox1.Text + "'", con);
cmd3.ExecuteNonQuery();
SqlDataAdapter da = new SqlDataAdapter(cmd3);
DataSet ds = new DataSet();
da.Fill(ds);
int count=Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());
if (count > 0)
{
ScriptManager.RegisterStartupScript(Button2, typeof(Button), "alert", "alert('Congratulations " + TextBox1.Text + " Your Score is " + ds.Tables[0].Rows[0][0] + " !')", true);
}
else
{
ScriptManager.RegisterStartupScript(Button2, typeof(Button), "alert", "alert('Oops ! Sorry " + TextBox1.Text + " Your Score is " + ds.Tables[0].Rows[0][0] + " You are not qualified !')", true);
}
con.Close();
}
else
{
ScriptManager.RegisterStartupScript(Button2, typeof(Button), "alert", "alert('Please Enter UserName!')", true);
}




}

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

thank you for your valuable answers,i got it

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/20/2013 [Member] Starter | Points: 25

ok congrats !!!!!

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/20/2013 [Member] Starter | Points: 25

Thank You karthik,but
i have another doubt,that is i want to get the result when i click on the submit button,that means,when i click on the submit button,i want to get the score and i want to store that score into database,how can i do it,can you please tell me?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/21/2013 [Member] Starter | Points: 25

if u want to place another button (result) to get the result add following code

if (TextBox1.Text != "")

{
con.Open();

SqlCommand cmd3 = new SqlCommand("select ans from Result where UserName='" + TextBox1.Text + "'", con);

cmd3.ExecuteNonQuery();

SqlDataAdapter da = new SqlDataAdapter(cmd3);

DataSet ds = new DataSet();

da.Fill(ds);

int count=Convert.ToInt32(ds.Tables[0].Rows[0][0].ToString());

if (count > 0)

{

ScriptManager.RegisterStartupScript(Button2, typeof(Button), "alert", "alert('Congratulations " + TextBox1.Text + " Your Score is " + ds.Tables[0].Rows[0][0] + " !')", true);

}

else

{

ScriptManager.RegisterStartupScript(Button2, typeof(Button), "alert", "alert('Oops ! Sorry " + TextBox1.Text + " Your Score is " + ds.Tables[0].Rows[0][0] + " You are not qualified !')", true);

}

con.Close();

}

else

{

ScriptManager.RegisterStartupScript(Button2, typeof(Button), "alert", "alert('Please Enter UserName!')", true);

}


}



if want display under submit button (same button )
[code]
ScriptManager.RegisterStartupScript(this, GetType(), "alert", "alert('Congratulations " + TextBox1.Text + " Your Score is " + j + "')", true);
[code]
the above code display the result
if you want insert into db
[code]
qlCommand cmd1 = new SqlCommand("insert into Result (UserName,date,ans) values('" + TextBox1.Text + "','" + DateTime.Now.ToLongTimeString() + "','" + j + "')", con);
[/code]

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/21/2013 [Member] Starter | Points: 25

Hi karthik,thank you,but date&Time is Displaying like this 1900-01-01 10:12:59.000
what is this 1900-01-01?can you please tell me,and how to display Date?where is the mistake?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/21/2013 [Member] Starter | Points: 25

use this

DateTime.Now.ToString("dd-MM-yyyy:hh:mm:ss");

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/21/2013 [Member] Starter | Points: 25

i use it as
SqlCommand cmd1 = new SqlCommand("insert into Resultsforstudents (UserID,UserName,Date,Ans) values('" + TextBox1.Text + "','"+ TextBox2.Text +"','" + DateTime.Now.ToString("dd-MM-yyyy:hh:mm:ss") + "','" + i + "')", con);
am getting this error
Conversion failed when converting datetime from character string.what should i do?

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/21/2013 [Member] Starter | Points: 25

change data type date to nvarchar(max) in databse table

sorry its not i its j in my code j store total number of correct ans into db table

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Tejamanimala on: 3/21/2013 [Member] Starter | Points: 25

hi karthik,thank you for your reply,it's so helping me,and i have another problem.i.e,
i have one registration page, contain columns as UserID,FirstName,LastName,Address,ContactNo,DOB,Gender,EmailID.
when i entering all the data into textboxes,then clicki on submit button button it is going to next page,but the values are not storing into database,can you please check my code,and tell me where the mistake.
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
namespace newonlineexam
{
public partial class Register1 : System.Web.UI.Page
{
SqlConnection con = new SqlConnection("user id=sa;password=Ektha@2013;Data Source=EKTHA-3D34;Initial Catalog=OnlineExam");
Random rn = new Random();
// SqlCommand cmd = new SqlCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Label17.Text = rn.Next(1000).ToString();
}
}

protected void Button1_Click1(object sender, EventArgs e)
{
con.Open();
//Session["User ID"] = TextBox4.Text;
SqlCommand cmd = new SqlCommand("insert into Registration(UserID,FirstName,LastName,Address,ContactNo,DOB,Gender,EmailID) values('" + Label17.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + RadioButtonList1.SelectedItem.Text + "','" + TextBox7.Text + "')", con);
cmd.ExecuteNonQuery();
//string s1 = "insert into Registration(UserID,FirstName,LastName,Address,ContactNo,DOB,Gender,EmailID) values('" + Label17 + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + RadioButtonList1.SelectedItem.Text + "','" + TextBox7.Text + "')";

//SqlCommand cmd = new SqlCommand(s1, con);

cmd.ExecuteNonQuery(); cmd.Connection = con;
cmd.CommandText = "select count(*) from Registration where FirstName='" + TextBox2.Text + "'";
int i = int.Parse(cmd.ExecuteScalar().ToString());
if (i == 0)
{
Session["id"] = Label17.Text;
Session["FirstName"] = TextBox2.Text;

TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
TextBox7.Text = "";
RadioButtonList1.Items[0].Selected = false;
RadioButtonList1.Items[1].Selected = false;
Label17.Text = rn.Next(1000).ToString();
Response.Redirect("RegAck.aspx");
}
else
{
Label1.Text = "UserName already exists...";
}
con.Close();
}

protected void Button2_Click1(object sender, EventArgs e)
{
TextBox2.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
TextBox7.Text = "";
RadioButtonList1.Items[0].Selected = false;
RadioButtonList1.Items[1].Selected = false;
}

}

}

manimala

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Karthikreddy on: 3/21/2013 [Member] Starter | Points: 25

changed code
dont use two cmds change the name to cmd2


protected void Button1_Click(object sender, EventArgs e)
{
con.Open();

SqlCommand cmd = new SqlCommand("insert into Registration1(UserID,FirstName,LastName,Address,ContactNo,DOB,Gender,EmailID) values('" + Label1.Text + "','" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "')", con);

//cmd.ExecuteNonQuery(); cmd.Connection = con;
SqlCommand cmd1 = new SqlCommand("select count(*) from Registration1 where FirstName='" + TextBox1.Text + "'",con);
//cmd1.CommandText = "select count(*) from Registration where FirstName='" + TextBox1.Text + "'";
int i = int.Parse(cmd1.ExecuteScalar().ToString());
if (i==0)
{
cmd.ExecuteNonQuery();
Session["id"] = Label1.Text;
Session["FirstName"] = TextBox1.Text;

TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
TextBox7.Text = "";
Label1.Text = rn.Next(1000).ToString();
Response.Redirect("Default.aspx");
}
else
{

Label2.Text = "UserName already exists...";
}

}

k@rth!k
karthikreddy08a50@gmail.com

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 3/21/2013 [Member] Bronze | Points: 25

try this

hi
Tejamanimala
what is ur issue? working this code in my machine otherwise send me full source code zip format.


Notes:
1.You Put Multiple ExecuteNonQuery Remove One.
2.Then when execute the Record return value 1 so check i=1 like this

con.Open();
//Session["User ID"] = TextBox4.Text;
SqlCommand cmd = new SqlCommand("insert into Registration(UserID,FirstName,LastName,Address,ContactNo,DOB,Gender,EmailID) values('" + Label17.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + RadioButtonList1.SelectedItem.Text + "','" + TextBox7.Text + "')", con);
cmd.ExecuteNonQuery();
//string s1 = "insert into Registration(UserID,FirstName,LastName,Address,ContactNo,DOB,Gender,EmailID) values('" + Label17 + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + RadioButtonList1.SelectedItem.Text + "','" + TextBox7.Text + "')";

//SqlCommand cmd = new SqlCommand(s1, con);

//cmd.ExecuteNonQuery();
cmd.Connection = con;
cmd.CommandText = "select count(*) from Registration where FirstName='" + TextBox2.Text + "'";
int i = int.Parse(cmd.ExecuteScalar().ToString());



if (i == 1)
{
Session["id"] = Label17.Text;
Session["FirstName"] = TextBox2.Text;

TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
TextBox5.Text = "";
TextBox6.Text = "";
TextBox7.Text = "";
RadioButtonList1.Items[0].Selected = false;
RadioButtonList1.Items[1].Selected = false;
Label17.Text = rn.Next(1000).ToString();
Response.Redirect("RegAck.aspx");
}
else
{
Label1.Text = "Username already exists...";
}
con.Close();

Mark as Answer if its helpful to you

Tejamanimala, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response


Found interesting? Add this to: