Hi all,
I have a textbox, 2-dropdownlists and a button in my webform.
1.On Page load , if any user is selected then these textbox,dropdownlist values for that user should display from the database .I have done this and is working perfectly fine.
2. If there is no user in the database then the textbox,dropdownlist should display default values.
Please can anyone help me in doing this. I tried the 2 bit but no success.
When i tried with no values in the database its throwing error.
Thanks
protected void Page_Load(object sender, EventArgs e)
{
Loaddetails();
}
private void Loaddetails()
{
string connectionString2 = ConfigurationManager.AppSettings["Services"];
SqlConnection myconnection2 = new SqlConnection(connectionString2);
SqlCommand cmd2 = new SqlCommand("select TAnalysis,vendor,Biographies from Auth where Authority=@Authority");
SqlDataReader reader2;
cmd2.Parameters.AddWithValue("@Authority", Session["Sessionuser"]);
cmd2.Connection = myconnection2;
myconnection2.Open();
reader2 = cmd2.ExecuteReader();
reader2.Read();
trendtxtbox.Text = (reader2["Analysis"].ToString());
DropDownList1.Text = (reader2["vendor"].ToString());
splitbioddl.Text = (reader2["Biographies"].ToString());
reader2.Close();
myconnection2.Close();
}
protected void saveauthbtn_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.AppSettings["Services"];
SqlConnection myconnection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("[sp_InsertandupdateAuthority]", myconnection);
cmd.Parameters.AddWithValue("@Authority", Session["Sessionuser"]);
cmd.Parameters.AddWithValue("@AuthorityName", authoritylabel.Text);
cmd.Parameters.AddWithValue("@vendor", DropDownList1.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Biographies", splitbioddl.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Analysis", trendtxtbox.Text);
DropDownList1.Items.Remove(DropDownList1.SelectedItem.Value);
splitbioddl.Items.Remove(splitbioddl.SelectedItem.Value);
trendtxtbox.Text = "";
cmd.CommandType = CommandType.StoredProcedure;
myconnection.Open();
cmd.Connection = myconnection;
cmd.ExecuteNonQuery();
myconnection.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
Loaddetails();
}
private void Loaddetails()
{
string connectionString2 = ConfigurationManager.AppSettings["Services"];
SqlConnection myconnection2 = new SqlConnection(connectionString2);
SqlCommand cmd2 = new SqlCommand("select Analysis,vendor,Biographies from Admin_Authority where Authority=@Authority");
SqlDataReader reader2;
cmd2.Parameters.AddWithValue("@Authority", Session["Sessionuser"]);
cmd2.Connection = myconnection2;
myconnection2.Open();
reader2 = cmd2.ExecuteReader();
if (reader2.HasRows)
{
reader2.Read();
trendtxtbox.Text = (reader2["Analysis"].ToString());
DropDownList1.Text = (reader2["vendor"].ToString());
splitbioddl.Text = (reader2["Biographies"].ToString());
}
else
{
trendtxtbox.Text = "5";
DropDownList1.Text = "M";
splitbioddl.Text = "No";
}
reader2.Close();
myconnection2.Close();
}
protected void saveauthbtn_Click(object sender, EventArgs e)
{
string connectionString = ConfigurationManager.AppSettings["Services"];
SqlConnection myconnection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("[sp_InsertAdmin_Authority]", myconnection);
cmd.Parameters.AddWithValue("@Authority", Session["Sessionuser"]);
cmd.Parameters.AddWithValue("@AuthorityName", authoritylabel.Text);
cmd.Parameters.AddWithValue("@vendor", DropDownList1.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Biographies", splitbioddl.SelectedItem.Value);
cmd.Parameters.AddWithValue("@Analysis", trendtxtbox.Text);
DropDownList1.Items.Remove(DropDownList1.SelectedItem.Value);
splitbioddl.Items.Remove(splitbioddl.SelectedItem.Value);
trendtxtbox.Text = "";
cmd.CommandType = CommandType.StoredProcedure;
myconnection.Open();
cmd.Connection = myconnection;
cmd.ExecuteNonQuery();
myconnection.Close();
}
stored procedure
ALTER PROCEDURE [dbo].[sp_InsertAdmin_Authority]
(
@Authority int ,
@AuthorityName nvarchar(50),
@vendor nvarchar(50),
@Biographies nvarchar(3),
@Analysis nvarchar(10)
)
AS
INSERT INTO Admin_Authority
(
Authority,
AuthorityName,
vendor,
Biographies,
Analysis
)
VALUES
(
@Authority,
@AuthorityName,
@vendor,
@Biographies,
@Analysis
)