Saving survey answers to a database - c#

I'm working on a project that requires me to create an evaluation portal (survey) where users login and answer 20 questions and its all rating questions (Strongly Disagree, Disagree, Agree and Strongly Agree).
I'm using ASP.net and C# and radio button list in every page.
I was able to create the login page using sessions.
I wanted to display every question in a separate page but when I do that I start having trouble with saving survey responses to the database. the result for each question get display in a separate row in the database. to be more specific, every user will have 20 rows of data in the database.
My question how to get each user responses in the same line or row in the database??
Any help will be greatly appreciated.
Thanks
Here is my login page:
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Student;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select s.First_Name, s.Last_Name,c.Class_Title, c.Course_ID, c.Instructor_Last, c.Instructor_First, c.Term, c.Section_ID, c.Course_Number,e.Instructor_ID from Student S Join Student_Course e ON (s.Student_ID = e.Student_ID) Join Course c ON(c.Course_ID = e.Course_ID) where UserName =#username and Password=#password",con);
cmd.Parameters.AddWithValue("#username", txtUserName.Text);
cmd.Parameters.AddWithValue("#password", txtPassword.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Session["USER_ID"] = dt;
Response.Redirect("Successful_Login.aspx");
}
Here is my successful login page
This page to get user information base on their login, I'm using a gridview to show students courses base on their login and from there they can click on the course they want to rate and start the survey.
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["USER_ID"];
lblName.Text = dt.Rows[0][0].ToString() + " " + dt.Rows[0][1].ToString();//your cloumn name;
DataTable dt2 = (DataTable)Session["USER_ID"];
GridView1.DataSource = dt2;
GridView1.DataBind();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string instructorName = GridView1.SelectedRow.Cells[4].Text + ", " + GridView1.SelectedRow.Cells[5].Text;
string courseSession= GridView1.SelectedRow.Cells[7].Text + "-" + GridView1.SelectedRow.Cells[8].Text;
string term = GridView1.SelectedRow.Cells[6].Text;
Session["USER_ID"] = instructorName;
Session["USER_ID2"] = courseSession;
Session["USER_ID3"] = term;
Response.Redirect("Q1.aspx");
}
Here is the first question (Q1)
When the user click next button to go the next question, I would like for all response to go to the database and they go to the next page(Q2), I would like to capture all the results in the same row.
protected void btnNext_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into Survey (Q1,Q1_Comments) values (#Q1,#Q1_Comments)", con);
cmd.Parameters.AddWithValue("Q1", radListQ1.SelectedValue);
cmd.Parameters.AddWithValue("Q1_Comments", txtQ1Comments.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}

For starter, if you want to display all 20 responses from the same user in one row, then you should construct a table that has more columns in database.
e.g.
Survey
-----------------------------------------------------
studentID | Q1 | Q1_comments | Q2 | Q2_comments | ...
For the first question Q1, you can still use your original insert statement above:
SqlCommand cmd = new SqlCommand("insert into Survey (Q1,Q1_Comments) values (#Q1,#Q1_Comments)", con);
cmd.Parameters.AddWithValue("Q1", radListQ1.SelectedValue);
cmd.Parameters.AddWithValue("Q1_Comments", txtQ1Comments.Text);
For the second question and onwards, you need to perform update instead of inserting to database. e.g.
SqlCommand cmd = new SqlCommand("UPDATE Survey SET (Q2=#Q2,Q2_comment=#Q2_Comments)", con);
cmd.Parameters.AddWithValue("Q2", radListQ2.SelectedValue);
cmd.Parameters.AddWithValue("Q2_Comments", txtQ2Comments.Text);
As a little bit of advise, you might want to use transaction to do insert,update or delete, and surround them using try ... catch ... detect/handle potential error. More reading from MSDN
Hope this helps.

I strongly suggest you store in 20 rows... But if you insist using just one row.. I would suggest UPDATE command instead of INSERT...

Just to provide the complete code. this is Q1
protected void btnNext_Click(object sender, EventArgs e)
{
if (Session["USER_ID"] != null )
{
SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into Survey (Q1,Q1_Comments) values (#Q1,#Q1_Comments)", con);
cmd.Parameters.AddWithValue("Q1", radListQ1.SelectedValue);
cmd.Parameters.AddWithValue("Q1_Comments", txtQ1Comments.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
And this is Q2
protected void btnQ2Next_Click(object sender, EventArgs e)
{
if (Session["USER_ID"] != null)
{
SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("UPDATE Survey SET (Q2 = #Q2, Q2_Comments = #Q2_Comments)", con);
cmd.Parameters.AddWithValue("Q2", radListQ2.SelectedValue);
cmd.Parameters.AddWithValue("Q2_Comments", txtQ2Comments.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Q3.aspx");
}
}

Related

How to stop duplicating the data in database everytime the form loads?

I am currently making a system where one of the requirements is to generate a report for the total number of products available in the inventory.
I've been using this code to get the sum of the quantity of each variety in the database table.
public void ShowData()
{
con = new SqlConnection(#"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=JAPoultry;Integrated Security=True");
con.Open();
da = new SqlDataAdapter("SELECT * FROM Reports", con);
dt = new DataTable();
da.Fill(dt);
dgv_Reports.DataSource = dt;
}
private void frmReports_Load(object sender, EventArgs e)
{
con = new SqlConnection(#"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=JAPoultry;Integrated Security=True");
con.Open();
cmd = new SqlCommand("INSERT INTO Reports (Variety, Quantity) SELECT Variety, SUM(Quantity) FROM Inventory GROUP BY Variety", con);
cmd.ExecuteNonQuery();
ShowData();
}
but my problem is every time the form loads, the data is duplicating or keeps on adding onto the database.
I don't understand the need of your "reports" table,... If I was you I'll use only ShowData() with the query, like this
public void ShowData()
{
con = new SqlConnection(#"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=JAPoultry;Integrated Security=True");
con.Open();
da = new SqlDataAdapter("SELECT Variety, SUM(Quantity) FROM Inventory GROUP BY Variety", con);
dt = new DataTable();
da.Fill(dt);
dgv_Reports.DataSource = dt;
}
private void frmReports_Load(object sender, EventArgs e)
{
ShowData();
}
Move the INSERT command into a new InsertData() method and call this method when required rather than each time frmReports_Load is called.
Just make the frmReports_Load method run SELECT Variety, SUM(Quantity) AS Quantity FROM Inventory GROUP BY Variety only, and display the result directly on the screen each time the form loads, if that's what you need.
This is what I mean - you might just need to add an alias to the SUM column so it maps into your gridview correctly:
public void ShowData()
{
con = new SqlConnection(#"Data Source=LAPTOP-KA7UGSG3;Initial Catalog=JAPoultry;Integrated Security=True");
con.Open();
da = new SqlDataAdapter("SELECT Variety, SUM(Quantity) AS Quantity FROM Inventory GROUP BY Variety", con);
dt = new DataTable();
da.Fill(dt);
dgv_Reports.DataSource = dt;
}
private void frmReports_Load(object sender, EventArgs e)
{
ShowData();
}
P.S. It's unclear why you're inserting that data into another table - unless you need to keep a log of every single time someone loaded a form? That seems a little unlikely as a requirement, to be honest.

Inserting New Record to The Database Overwrites Existing Record C#

I have designed a student survey to evaluate instructors. The survey consist of 20 questions. what I want to do have each student to login and record their answer to a database. I was able to design the whole thing however, every time I take a survey the New Record Overwrites Existing Record.I would like to be able for all answers to be saved in one row for each student.
I'm really under time pressure and any help on this will be greatly appreciate it.
Here is the code on the Q1 page
if (Session["USER_ID"] != null )
{
SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into Survey (Q1,Q1_Comments) values (#Q1,#Q1_Comments)", con);
cmd.Parameters.AddWithValue("Q1", radListQ1.SelectedValue);
cmd.Parameters.AddWithValue("Q1_Comments", txtQ1Comments.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Q2.aspx");
}
and here is the code in Q2 question
Protected void btnQ2Next_Click(object sender, EventArgs e)
{
if (Session["USER_ID"] != null)
{
SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("UPDATE Survey SET Q2 = #Q2, Q2_Comments = #Q2_Comments ", con);
cmd.Parameters.AddWithValue("Q2", radListQ2.SelectedValue);
cmd.Parameters.AddWithValue("Q2_Comments", txtQ2Comments.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Q3.aspx");
}
}
Please help, I'm suspecting the insert/Update statement is what causing that.
I aslo, would like to mention that each question is in a separate page and that is why I'm inserting the update.
Edit
Currently, My DB Structure is like this
Survey Table
Survey_ID (PK)
Username (Fk)
Student_ID (fK)
Course_ID (FK)
Q1
Q1_Comments
Q2
Q2_Comments
Q3
Q3_Comments
Q4
Q4_Comments
...... to Q20
Student Table
Student_ID (PK)
Last_Name
First_Name
Username
Password
Course_student Table
Student_ID (PK)
Course_ID (PK)
Instructor_ID (PK)
Survey_ID (PK)
Course Table
Course_ID
Class_Title
Instructor_Last
Instructor_First
Term
Section_ID
Course_Number
Instructor Table
Instructor_ID (PK)
Last_Name
First_Name
Username
Password
Login Page code
protected void btnLogin_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Student;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select s.Student_ID, c.Course_ID,s.First_Name, s.Last_Name, c.Class_Title, c.Course_ID, c.Instructor_Last, c.Instructor_First, c.Term, c.Section_ID, c.Course_Number,e.Instructor_ID from Student S Join Course_Student e ON (s.Student_ID = e.Student_ID) Join Course c ON(c.Course_ID = e.Course_ID) where UserName =#username and Password=#password", con);
//SqlCommand cmd = new SqlCommand("select * from UserTable where UserName =#username and Password=#password", con);
cmd.Parameters.AddWithValue("#username", txtUserName.Text);
cmd.Parameters.AddWithValue("#password", txtPassword.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
Session["USER_ID"] = dt;
Response.Redirect("Successful_Login.aspx");
}
Successful_Login.aspx
public partial class Successful_Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
dt = (DataTable)Session["USER_ID"];
lblName.Text = dt.Rows[0][0].ToString() + " " + dt.Rows[0][1].ToString();//your cloumn name;
DataTable dt2 = (DataTable)Session["USER_ID"];
GridView1.DataSource = dt2;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Session.Remove("USER_ID");
Session.RemoveAll();
Session["USER_ID"] = null;
Response.Redirect("Loggedout.aspx");
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
string instructorName = GridView1.SelectedRow.Cells[5].Text + ", " + GridView1.SelectedRow.Cells[4].Text;
string courseSession= GridView1.SelectedRow.Cells[1].Text + "-" + GridView1.SelectedRow.Cells[2].Text;
string term = GridView1.SelectedRow.Cells[8].Text;
string studentID = GridView1.SelectedRow.Cells[10].Text;
string CourseID = GridView1.SelectedRow.Cells[11].Text;
Session["USER_ID1"] = instructorName;
Session["USER_ID2"] = courseSession;
Session["USER_ID3"] = term;
Session["USER_ID4"] = studentID;
Session["USER_ID5"] = CourseID;
Response.Redirect("Q1.aspx");
}
Q1 Page
if (Session["USER_ID"] != null )
{
SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into Survey (Q1,Q1_Comments) values (#Q1,#Q1_Comments)", con);
cmd.Parameters.AddWithValue("Q1", radListQ1.SelectedValue);
cmd.Parameters.AddWithValue("Q1_Comments", txtQ1Comments.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Q2.aspx");
}
Q2 Page
if (Session["USER_ID"] != null )
{
SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into Survey (Q1,Q1_Comments) values (#Q1,#Q1_Comments)", con);
cmd.Parameters.AddWithValue("Q1", radListQ1.SelectedValue);
cmd.Parameters.AddWithValue("Q1_Comments", txtQ1Comments.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Q2.aspx");
}
Can you please show me how to write correctly using my variable names.
While it may not directly answer your question it may lead you to better organise your DB, in my eyes you will need to change your DB Design to accomodate multiple users and multiple questions with respective answers so your tables should look something like this
Table Users
-----------
UserId (PK)
FirstName
LastName
...
Table Questions
---------------
QuestionId (PK)
QuestionText
...
Table Answers
-------------
AnswerId (PK, Autonumber)
UserId - Unique constraint
QuestionId - Unique constraint
Answer
...
with such structure you will be able to do something like this then :
if (Session["USER_ID"] != null )
{
SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Student;Integrated Security=True");
SqlCommand cmd = new SqlCommand("insert into Answers (UserId, QuestionId, Answer) values (#p1, #p2, #p3)", con);
cmd.Parameters.AddWithValue("p1", Session["USER_ID"]);
cmd.Parameters.AddWithValue("p2", radListQ1.SelectedValue); //reference question_id field here
cmd.Parameters.AddWithValue("p3", txtQ1Comments.Text); // reference answer field here
con.Open();
cmd.ExecuteNonQuery();
con.Close();
Response.Redirect("Survey.aspx?Q=" + NextQuestionId);
}

how to update 'datetime' in column after login

I have a table name is 'User_tbl' where i am saving data of all registered users and the same table is being used to verify the users during Login.
I want to update only 'LastSeen' column with current datetime after login.
Look at this picture.
code behind
protected void Submit(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
con.Open();
SqlCommand cmd = new SqlCommand("select * from User_tbl where UserName =#username and Password=#password", con);
cmd.Parameters.AddWithValue("#username", txtUserName.Text);
cmd.Parameters.AddWithValue("#password", txtPWD.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
//to define the user seesion (starting user session)
Session["username"] = txtUserName.Text;
Response.Redirect("default2.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "LoginValidate", "<script language='javascript'> document.getElementById('errorMessage').innerHTML = 'Invalid Username or Password'</script>");
}
}
Do you mean something like this?
SqlConnection sqlConn = new SqlConnection(connection string here);
SqlCommand sqlComm = new SqlCommand();
sqlComm = sqlConn.CreateCommand();
sqlComm.CommandText = #"UPDATE User_tbl SET LastSeen=GetDate() WHERE UserName='#userName'";
sqlComm.Parameters.Add("#userName", SqlDbType.VarChar);
sqlComm.Parameters["#userName"].Value = txtUserName.Text;
sqlConn.Open();
sqlComm.ExecuteNonQuery();
sqlConn.Close();
You'd need to place something along those lines in your 'if (dt.Rows.Count > 0)' code.
You may wish to reuse the same connection that you created for your SELECT statement.
Many other options are available. Often this sort of thing is best achieved using a stored procedure, where you can check the login credentials and perform any related updates in a single request to the database server.

How to insert value from gridview into database

I have two tables in a SQL Server database. I select from table ADMS and I need to insert master table by gridview but I dont know how to insert with gridview. Please help. I've tried for many days and I did not pass yet
protected void Button3_Click1(object sender, EventArgs e)
{
if (RadioButton2.Checked)
{
SqlConnection con = new SqlConnection(MyConnectionString);
// con.Open(); // don't need the Open, the Fill will open and close the connection automatically
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM ADMS_Machining where datetime='" + TextBox1.Text + "'", con);
mytable = new DataTable();
da.Fill(mytable);
GridView2.DataSource = mytable;
GridView2.DataBind();
}
else
{
SqlConnection con = new SqlConnection(MyConnectionString);
// con.Open(); // don't need the Open, the Fill will open and close the connection automatically
SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Machining_Master where datetime='" + TextBox1.Text + "'", con);
mytable = new DataTable();
da.Fill(mytable);
GridView2.DataSource = mytable;
GridView2.DataBind();
}
}
protected void Button4_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
String strConnString, strSQL;
strConnString = "Server=kane-pc;UID=sa;PASSWORD=1234;Database=Machining;Max Pool Size=400;Connect Timeout=600;";
//here
conn.ConnectionString = conn;
conn.Open();
cmd.Connection = conn;
cmd.CommandText = strSQL;
}
You can extract values from a grid view depending on what you have placed in the cells...
string value = this.GridView2.Rows[0].Cells[0].Text;
You can also track the selected row event, and get specific controls like the following...
protected void OnSelectedIndexChanged(object sender, EventArgs e)
{
string someValueTakenFromLabel = (GridView2.SelectedRow.FindControl("lblAnyLabelHere") as Label).Text;
// .... do something with value here
}
I suggest you go through some tutorials though to get the hang of how to use GridView.
http://www.asp.net/web-forms/videos/building-20-applications/lesson-8-working-with-the-gridview-and-formview
http://www.aspsnippets.com/Articles/How-to-get-Selected-Row-cell-value-from-GridView-in-ASPNet.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview%28v=vs.110%29.aspx
You have to first read data from cells and then insert them into database using SqlCommand.
Assuming that you have M_ID and M_NAME columns in your Machining_Master table you can insert values to database as below:
//Assuming that your id column is first column and name is second column
//get value of id and name
int mId = Convert.ToInt32(GridView2.SelectedRow.Cells[0].Text);
string mName = GridView2.SelectedRow.Cells[1].Text;
string connectionStrng = "your connection string";
string insertSql = "INSERT INTO Machining_Master (M_ID, M_NAME) VALUES (#mId, #mName)";
using (SqlConnection conn = new SqlConnection(connectionStrng))
{
using (SqlCommand cmd = new SqlCommand(insertSql, conn))
{
try
{
cmd.Parameters.Add(new SqlParameter("mId", mId));
cmd.Parameters.Add(new SqlParameter("mName", mName));
conn.Open();
cmd.ExecuteNonQuery();
}
finally
{
//Close connection
conn.Close();
}
}
}

3 DropDownLists search function

I'm new to c#, as in.
I'm currently working on a search function in c# using 3 DropDownLists and a submit button. When a user select an item on DropDownList and click submit, it will print the table for the respective selection.
There are 3 DropDownLists:
a province,
city,
specialization.
This will search the available doctors that suits the selection. For example I choose province1 on 1st DropDownList, city1 on the 2nd and a psychologist on 3rd, when the submit button is fired, it will print the available doctors that is in province1, city1 and has a specialization of psychologist.
I already have a code, still figuring it out but, when i click the submit button, nothing is happening. Can someone help me?
Here's what I've done so far:
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
if (!IsPostBack)
{
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_province");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_province ORDER BY PROVINCE_NAME ASC", conn);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
}
ddlProvince.DataSource = dt;
ddlProvince.DataTextField = "PROVINCE_NAME";
ddlProvince.DataValueField = "PROVINCE_CODE";
ddlProvince.DataBind();
ddlProvince.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
ddlCity.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
ddlSpec.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
}
}
protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_province");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_city WHERE PROVINCE_CODE =#pcode", conn);
comm.Parameters.AddWithValue("#pcode", ddlProvince.SelectedValue);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
SqlParameter param = new SqlParameter();
param.ParameterName = "#pcode";
param.Value = ddlProvince;
comm.Parameters.Add(param);
}
ddlCity.DataSource = dt;
ddlCity.DataTextField = "CITY_NAME";
ddlCity.DataValueField = "CITY_CODE";
ddlCity.DataBind();
ddlCity.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
}
protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
{
System.Threading.Thread.Sleep(1000);
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_city");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT * FROM emed_specialization", conn);
comm.Parameters.AddWithValue("#ccode", ddlCity.SelectedValue);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
SqlParameter param = new SqlParameter();
param.ParameterName = "#ccode";
param.Value = ddlCity;
comm.Parameters.Add(param);
}
ddlSpec.DataSource = dt;
ddlSpec.DataTextField = "SPEC_NAME";
ddlSpec.DataValueField = "SPEC_CODE";
ddlSpec.DataBind();
ddlSpec.Items.Insert(0, new ListItem("---------------SELECT---------------", "0"));
}
protected void btnSub_Click(object sender, EventArgs e)
{
string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
SqlConnection conn = new SqlConnection(constring);
DataTable dt = new DataTable("emed_doctors");
using (conn)
{
conn.Open();
SqlCommand comm = new SqlCommand("SELECT DOCTOR_NAME FROM emed_doctors where Province = '" + ddlProvince.SelectedItem.ToString() + "'", conn);
SqlDataAdapter adptr = new SqlDataAdapter(comm);
adptr.Fill(dt);
}
}
}
Make change in this line of code which is in submit button click function
Correct code:
SqlCommand comm = new SqlCommand("SELECT DOCTOR_NAME FROM emed_doctors where province = '" + ddlProvince.SelectedItem.ToString() + "'", conn);
because as you see your code query is incorrent have look to your code which is incorrect
Wrong code:
SqlCommand comm = new SqlCommand("SELECT DOCTOR_NAME FROM emed_doctors where (" + ddlProvince.SelectedItem.ToString() + " ", conn);
Edit:
if you want to display record that you got in DataTable you either need loop through the records or you need to use GridView for that...i think you miss that thing
in above one after where clause you miss the name of the filed you need to made filter on... i have written update code by modifying that condition.
See MSDN for GridView.
You said that after clicking submit, nothing happend. But in btnSub_Click I didn't see anything that displaying the result or changing anything on the page. You should bind dt to some control like a gridview etc.

Categories

Resources