i can able to search with id and mobno
but i also want to search with name i unable to do it when i search with name it showing conversion error bcus it is directly executing
this condition showing conversion error how can i achieve it..
C#
private void BindGrid()
{
int flag;
SqlCommand cmd = new SqlCommand("sp_GridVS", con);
cmd.CommandType = CommandType.StoredProcedure;
if (TextBox3.Text != "")
{
cmd.Parameters.AddWithValue("@mobileno", Convert.ToInt64(TextBox3.Text));
cmd.Parameters.AddWithValue("@id", Convert.ToInt64(TextBox3.Text));
cmd.Parameters.AddWithValue("@flag", 1);
}
else if (TextBox3.Text == "")
{
cmd.Parameters.AddWithValue("@mobileno","");
cmd.Parameters.AddWithValue("@id", 0);
cmd.Parameters.AddWithValue("@flag", 0);
}
DataTable dt = new DataTable();
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}This is my sp
SQL
CREATE procedure sp_GridVS
@flag int,@id bigint,@mobileno bigint
as begin
if(@flag=1)
begin
select * from gridview where mobileno=@mobileno or id=@id
end
if(@flag=0)
begin
select * from gridview
end
endThis is for seach button
C#
protected void Button2_Click(object sender, EventArgs e)
{
if (TextBox3.Text == "")
{
Response.Write("alert('pls enter phone number or id ')");
BindGrid();
}
BindGrid();
}
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 27, 2023, 6:05 AM EST
Try below sp
CREATE PROCEDURE [dbo].[SearchRecords] @search_param VARCHAR(50) AS BEGIN SET NOCOUNT ON; SELECT * FROM YourTableName WHERE id = CONVERT(INT, @search_param) OR mobileno = CONVERT(BIGINT, @search_param) OR name LIKE '%' + @search_param + '%' ENDNaimish MakwanaRank #324 · 7.7K reputationPosted Feb 27, 2023, 5:32 AM EST
What value you are passing in TextBox3.Text? It seems it's not valid long type.
Naimish MakwanaRank #324 · 7.7K reputationPosted Feb 27, 2023, 5:56 AM EST
Hello Jaya,
I would suggest you to change your sp and code as below and try.
CREATE PROCEDURE [dbo].[sp_GridVS] @search_param VARCHAR(50) AS BEGIN SET NOCOUNT ON; SELECT * FROM YourTableName WHERE id = @search_param OR mobileno = @search_param OR name LIKE '%' + @search_param + '%' ENDThen change you code to
private void BindGrid() { SqlCommand cmd = new SqlCommand("sp_GridVS", con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@search_param", TextBox3.Text); DataTable dt = new DataTable(); using (SqlDataAdapter da = new SqlDataAdapter(cmd)) { da.Fill(dt); GridView1.DataSource = dt; GridView1.DataBind(); } }Thanks
Naimish Makwana
Jaya PrakashRank #1,293 · 1.3K reputationPosted Feb 27, 2023, 5:42 AM EST
i passed id and mobno after that i changed my sp to @flag =2 select * from gridview where name=@name
added few line asp but when i pass name as param it is not showing any values throwing an error conversion problem
i think its taking only id or mobile for this how can i make this textbox works for int type and string type
Jaya PrakashRank #1,293 · 1.3K reputationPosted Feb 27, 2023, 6:03 AM EST
Error sir
Jaya PrakashRank #1,293 · 1.3K reputationPosted Feb 27, 2023, 11:03 PM EST
using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace RegistrationForms { public partial class Registration : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { var Path = Server.MapPath("/ima/") + FileUpload1.FileName; } protected void Button1_Click(object sender, EventArgs e) { int i; if (TextUser.Text == "") { Response.Write(" alert('*please Enrter UserName') "); return; } else if (TextPass.Text == "") { Response.Write(" alert('*please Enrter Password') "); return; } else if (TextEmail.Text == "") { Response.Write(" alert('*please Enrter valid Email') "); return; } else if (TextMB.Text == "") { Response.Write(" alert('*please Enter min 10 Numbers') "); return; } string Gender = ""; if (RB1.Checked == true) { Gender = RB1.Text; } else { Gender = RB2.Text; } string Qualification = ""; if (CheckBox1.Checked == true) { Qualification = CheckBox1.Text; } if (CheckBox2.Checked == true) { Qualification = CheckBox1.Text; } if (CheckBox3.Checked == true) { Qualification = CheckBox3.Text; } if (Button1.Text == "Registration") { SqlConnection Con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString()); Con.Open(); HttpPostedFile PostedFile = FileUpload1.PostedFile; string filename = Path.GetFileName(PostedFile.FileName); string fileExtension = Path.GetExtension(filename); int fileSize = PostedFile.ContentLength; // if (fileExtension.ToLower() == "png" || fileExtension.ToLower() == "jpg") // { Stream stream = PostedFile.InputStream; BinaryReader binaryReader = new BinaryReader(stream); byte[] bytes = binaryReader.ReadBytes((int)stream.Length); SqlCommand cmd = new SqlCommand("LogIn", Con); cmd.CommandType = CommandType.StoredProcedure; cmd.Parameters.AddWithValue("@AdharProof", FileUpload1.FileName); cmd.Parameters.AddWithValue("@PancardProof", FileUpload2.FileName); Label1.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded."; Con.Close(); // Label1.Text = Path.GetFileName(FileUpload2.FileName) + " has been uploaded."; } else { Label1.Text = Path.GetFileName(FileUpload1.FileName) + " has been not uploaded."; //Label1.Text = Path.GetFileName(FileUpload2.FileName) + " has been not uploaded."; } SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString()); con.Open(); SqlCommand Cmd = new SqlCommand("AddRegister", con); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Parameters.AddWithValue("@UserName", TextUser.Text); Cmd.Parameters.AddWithValue("@Password", TextPass.Text); Cmd.Parameters.AddWithValue("@Gender", Gender); Cmd.Parameters.AddWithValue("@Email", TextEmail.Text); Cmd.Parameters.AddWithValue("@MobileNumber", TextMB.Text); Cmd.Parameters.AddWithValue("@Qualification", Qualification); Cmd.Parameters.AddWithValue("@AdharProof", FileUpload1.FileName); Cmd.Parameters.AddWithValue("@PancardProof", FileUpload2.FileName); i = Cmd.ExecuteNonQuery(); con.Close(); ----------------------------------------------- profile: using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.IO; using System.Linq; using System.Net; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; using PdfSharp.Pdf; using Syncfusion.Pdf.Graphics; namespace RegistrationForms { public partial class Profile : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString()); con.Open(); string commandText = "select Id,UserName,Password, Gender,MoboleNumber,Qualification,AdharProof,PancardProof from Registration where UserName='" + Session["UserName"] + "'"; SqlCommand com = new SqlCommand(commandText, con); SqlDataReader reader = com.ExecuteReader(); if (reader.Read()) { Label9.Text = reader["Id"].ToString(); Label10.Text = reader["UserName"].ToString(); Label11.Text = reader["Password"].ToString(); Label12.Text = reader["Gender"].ToString(); Label13.Text = reader["MoboleNumber"].ToString(); Label14.Text = reader["Qualification"].ToString(); ImageButton2.ImageUrl = "/ima/" + reader["AdharProof"].ToString(); ImageButton3.ImageUrl = "/ima/" + reader["PancardProof"].ToString(); } } protected void ImageButton2_Click(object sender, ImageClickEventArgs e) { if (ImageButton2.ImageUrl != string.Empty) { if (ImageButton2.ImageUrl.EndsWith(".png")) { Response.ContentType = "application/Downloads"; } else if (ImageButton2.ImageUrl.EndsWith(".pdf")) { Response.ContentType = "application/pdf"; } else if (ImageButton2.ImageUrl.EndsWith(".docx")) { Response.ContentType = "application/docx"; } else { Response.ContentType = "ima/jpg"; } string filePath = ImageButton2.ImageUrl; Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\""); Response.TransmitFile(Server.MapPath(filePath)); Response.End(); } } protected void ImageButton3_Click(object sender, ImageClickEventArgs e) { if (ImageButton3.ImageUrl != string.Empty) { if (ImageButton3.ImageUrl.EndsWith(".png")) { Response.ContentType = "application/Downloads"; } else if (ImageButton3.ImageUrl.EndsWith(".pdf")) { Response.ContentType = "application/pdf"; } else if (ImageButton3.ImageUrl.EndsWith(".docx")) { Response.ContentType = "application/docx"; } else { Response.ContentType = "ima/jpg"; } string filePath = ImageButton3.ImageUrl; Response.AddHeader("Content-Disposition", "attachment;filename=\"" + filePath + "\""); Response.TransmitFile(Server.MapPath(filePath)); Response.End(); } } protected void Button1_Click(object sender, EventArgs e) { string mainPath = "Profile.aspx.cs"; string fileName = "Downloads.pdf"; mainPath = Server.MapPath("~/ima/Downloads.pdf"); FileInfo file = new FileInfo(mainPath); Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.AddHeader("content-disposition", "attachment; filename= ~/Template/Self_Declaration.pdf"); Response.AddHeader("Content-Type", "application/pdf"); Response.ContentType = "application/pdf"; //Response.WriteFile(fileName); Response.End(); } } } ------------------- chnge pswd using System; using System.Collections.Generic; using System.Configuration; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; namespace RegistrationForms { public partial class ChaingePassword : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString); con.Open(); SqlDataAdapter da = new SqlDataAdapter("select Password from Registration where Password='" +TextBox1.Text + "'", con); DataTable dt = new DataTable(); da.Fill(dt); //if (dt.Rows.Count.ToString() == "1") //{ if (TextBox2.Text == TextBox3.Text) { string str = "update Registration set Password='" + TextBox3.Text + "' Where Password='"+TextBox1.Text+"'"; SqlCommand cmd = new SqlCommand(str, con); cmd.ExecuteNonQuery(); con.Close(); Label1.Text = "Password Upadted Successfully"; Label1.ForeColor = System.Drawing.Color.Aqua; } else { Label1.Text = "Successfully Not Upadted"; } } } } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { } } }Jaya PrakashRank #1,293 · 1.3K reputationPosted Feb 27, 2023, 11:35 PM EST
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Windows; using System.Configuration; using System.Data; namespace RegistrationForms { public partial class LogIn : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { if (TextBox1.Text == "") { Response.Write(" alert('*please Enrter UserName') "); return; } else if (TextBox2.Text == "") { Response.Write(" alert('*please Enrter Password') "); return; } Session["UserName"] = TextBox1.Text; SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ToString()); con.Open(); SqlCommand Cmd = new SqlCommand("LogIn", con); Cmd.CommandType = CommandType.StoredProcedure; Cmd.Parameters.AddWithValue("@UserName", TextBox1.Text); Cmd.Parameters.AddWithValue("@Password", TextBox2.Text); Cmd.ExecuteNonQuery(); con.Close(); Response.Redirect("Profile.aspx"); } } }